How to compile a java project using shell scripts
Compiling java projects generally uses IDE or Ant, Maven, and other tools directly. Few people use pure shell to compile java projects. This is exactly the case. Use this blog post to record it.
Case: I wrote a java project using eclipse and compiled it into a jar package.
You can use the Export provided by eclipse to Export the jar. However, for the sake of software automation and other reasons, we use the stored shell script for compilation.
The name of the java project is iec104. The src below is the source file directory, and the bin is the referenced jar package directory. Compile the Directory and compress it into a jar. If you have some knowledge about the jar, jar has a MANIFEST. MF file: the content of the MF file of the iec104 project is as follows:
Manifest-Version: 1.0 Main-Class: com.zzh.run.Iec104RecvRun
This indicates that the main-class (class with public static void main (String args []) is developed ).
Copy the file to the root directory of iec104 and execute the script compile. sh. The content is as follows:
#! /Bin/bashcur_dir = $ (pwd) echo $ cur_dirfunction do_compile_iec104 () {# echo $ cur_dir iec104 = $ cur_dir/iec104 iec1__src = $ cur_dir/iec104/src iec1__bin = $ cur_dir/iec104/bin # echo $ iec1__src # echo $ iec1__bin iec1__class = $ cur_dir/ iec104/class # store the names of all java files under the src directory of iec104 to iec104/src/sources. rm-rf $ iec1__src/sources in the list file. list find $ iec104_src-name "*. java "> $ iec104_src/sources. list cat $ iec104_src/ Sources. list # $ iec104_class is the directory for storing compiled class files rm-rf $ iec104_class mkdir $ iec104_class # compile java files here. Note-encoding UTF-8 here, at the beginning, the blogger did not join this, and then reported a bunch of errors. After a long tangle, I found out that I am sorry to everyone. Javac-d $ iec104_class-encoding UTF-8-classpath $ iec%_bin/classes12.jar: $ iec%_bin/junit-4.10.jar: $ iec%_bin/log4j-1.2.17.jar: $ iec%_bin/mysql-connector-java-5.0.5-bin.jar: $ iec%_bin/RXTXcomm. jar-g-sourcepath $ iec1__src @ $ iec1__src/sources. list # Because log4j is used, you need to put the configuration file of log4j together. If not, you can ignore this sentence cp $ iec1__src/log4j. properties $ iec104_class # if there is a jar in the iec104 directory, it will be deleted, because a new rm $ iec104/iec104.jar will be generated. # Here we need to cd to the directory where the class is stored, otherwise, if the absolute path is used for compiling, the compiled jar package contains the absolute path, which leads to a problem # jar-cvfm $ iec104/iec104.jar $ iec104/MANIFEST. MF $ iec104_class/* is incorrect. cd $ iec104_class jar-cvfm $ iec104/iec104.jar $ iec104/MANIFEST. MF * # grant the executable permission sudo chmod a + x $ iec104/iec104.jar} do_compile_iec1_exit 0Run this script (in linux) to compile and view the iec104.jar file in the iec104 directory.
The following describes the basic knowledge of java commands:
Javac is used to compile Java files in the following format:
Java [options] [sourcefiles] [@ files]
Where:
Options: command line options;
Sourcefiles: one or more source files to be compiled;
@ Files: one or more files that list the source files. Sometimes there are many files to be compiled, and it may be very long or inconvenient to modify one by one, you can put the source file to be compiled in the file and add @ before the file name, so that you can compile multiple files, which is useful for compiling a project, convenient and easy-to-use.
There are several important options:
-D is used to specify the storage location of the compiled class file. By default, the storage directory of the class file is not specified. The compiled class file will be in the same directory as the source file;
-Classpath can be abbreviated as-cp, which is used to search for the class files required for compilation and point out the location of the class files used for compilation, such as jar, zip, or other directories containing class files, specifying this option will overwrite the CLASSPATH setting;
-Sourcepath is used to search for the source files (java files) required for compilation. It specifies the location of the source files to be searched, such as jar, zip, or other directories containing java files;
Note the differences between the file path separator and the file list Separator in windows and linux (that is, the file specified by-classpath and-sourcepath:
In windows, the file path separator is \, and the file list separator is a semicolon;
In linux, the file path separator is/, and the file list separator is colon: