1st page Java development skills: How to Use ant to manually build a project
The Java language is used to build projects. In this process, compilation, packaging, running, deployment, and other operations are mainly involved. Use a SRC folder in the project to place the source code of the project. There is only one class source code.
The code for creating helloworld. Java in helloworld is as follows:
Public class helloworld {
Public static void main (string [] ARGs ){
System. Out. println ("Hello world! ");
Before ant is used, the helloworld can be constructed manually. The construction project is to compile, package, run, deploy, and perform other operations, for this simple helloworld project, this step is not deployed. The following describes how to manually compile, run, and package the helloworld project in the command line. The following steps are required to build the entire helloworld project.
1. preparations. Create the build \ Classes directory to place the compiled class files, and create the build \ lib directory to place the generated or referenced jar packages. At this time, the directory structure of the helloworld project is: There are two folders SRC and build in the root directory of the helloworld project, where SRC is the source code folder, build is used to place the class files obtained after the project is built, lib is used to place the jar packages generated or referenced during the build process.
2. compile the project. When no auxiliary tool is used, only Java classes are compiled in the doscommand line. For example, the helloworld project file is located in c: \ helloworld. The following command is used to compile the project, the specific compilation process 1 is shown.
When compiling the helloworld project in the doscommand line, you must first switch the path to the project directory and then call javac to compile the source file. In the interface shown in 1,-sourcepath SRC indicates that the Java file to be compiled needs to be found in the SRC folder, and-D indicates to put the generated class file in the build \ Classes directory.
3. Generate the JAR file. In most open-source Java projects, class files are packaged into jar files to facilitate your use. In the following operations, package the class file in the helloworld project into a jar file, as shown in step 2.
When you package and generate a jar file, you need to provide the mainifest file. In this file, you can configure main-class, that is, class containing the main method, as the program portal for calling and executing this jar package, you can configure the information of other referenced class libraries and packages in this file.
4. Run the project. You can use either of the following methods to run the helloworld Java project.
First, execute the compiled Java class directly. Figure 3 shows the execution method and result.
In the preceding method, the Java command is called. The CP parameter indicates the path to the helloworld file. In this line of code, find and execute the helloworld class file in the build \ Classes directory.
Type 2: Execute the JAR file in the doscommand line