How to compile Java projects with shell scripts

Source: Internet
Author: User
Tags log4j

Compiling Java projects is generally straightforward with the IDE or with tools such as Ant and Maven, and few people use pure shells to compile Java projects. Just meet such a should, use this blog post to make a record.
Case: I wrote a Java project with Eclipse and then compiled it into a jar package.

This allows you to export the jar using the export from eclipse. However, for the reasons of Balabala such as software automation, the storage shell script is used to compile.


, the name of Java project is iec104, the following src is the source file directory, bin is the referenced jar package directory, now to compile, and to hit the jar, if you know the jar, Know Jar has a MANIFEST.MF file, iec104 project MF file content is as follows:

    manifest-version:1.0    Main-class:com.zzh.run.iec104recvrun
This means that a class that runs the Main-class (with public static void main (String args[]) is developed.
Copy the file to the root of the iec104, and then execute the script compile.sh, which reads:
#!/bin/bashcur_dir=$ (PWD) echo $cur _dirfunction do_compile_iec104 () {# echo $cur _dir iec104= $cur _dir/iec104 iec104_ src= $cur _dir/iec104/src iec104_bin= $cur _dir/iec104/bin# echo $iec 104_src# echo $iec 104_bin iec104_class= $cur _d ir/iec104/class# the names of all Java files in the iec104 src directory into the iec104/src/sources.list file Rm-rf $iec 104_src/sources.list find $iec 10 4_src-name "*.java" > $iec 104_src/sources.list cat $iec 104_src/sources.list# $iec 104_class is the directory that holds the compiled class file RM-    RF $iec 104_class mkdir $iec 104_class# here to start compiling Java files, note here the-encoding Utf-8, bloggers just started not to join this, and then reported a pile of errors, tangled up a long time to find out, here to you to mention a wake up. javac-d $iec 104_class-encoding utf-8-classpath $iec 104_bin/classes12.jar: $iec 104_bin/junit-4.10.jar: $iec 104_bin/ Log4j-1.2.17.jar: $iec 104_bin/mysql-connector-java-5.0.5-bin.jar: $iec 104_bin/rxtxcomm.jar-g-sourcepath $iec 104_ SRC @ $iec 104_src/sources.list# because of the use of log4j, so the log4j configuration files are put together, if not used, you can ignore the CP $iec 104_src/log4j.properties $iec 104_ class# if the original jar in the iec104 directory is deleted, because to generate a new RM $iec104/iec104.jar# here to CD to store class directory, otherwise if the absolute path compiled, the compiled jar package is absolute path, so there will be problems # JAR-CVFM $iec 104/iec104.jar $iec 104/ MANIFEST. MF $iec 104_class/* This is the wrong CD $iec 104_class jar-cvfm $iec 104/iec104.jar $iec 104/manifest. MF *# gives executable permission sudo chmod a+x $iec 104/iec104.jar}do_compile_iec104exit 0
Running this script (under Linux) can compile and see the Iec104.jar file in the iec104 directory.
Here's a basic knowledge of Java commands:
Javac is used to compile Java files in the following format:
java [options] [SourceFiles] [@files]
which
Options: command line option;
SourceFiles: One or more source files to be compiled;
@files: One or more files to list the source file, sometimes to compile a lot of files, a dozen commands will appear very long, also inconvenient to modify, you can compile the source files listed in the file, before the file name plus @, so that you can compile a number of files, to compile a project is very useful, convenient, easy.
There are several more important options:
-D is used to specify the location of the compiled class file, which by default does not specify a directory for the class file, and the compiled class file will be in the same directory as the source file;
The-classpath can be abbreviated to-CP, which is used to search for the class file required for compiling, to indicate the location of the class file used by the compilation, such as a jar, zip, or other directory containing the class file, specifying that the option overrides the Classpath setting;
-sourcepath is used to search for the source files (i.e. Java files) required for compilation, specifying the location of the source files to be searched, such as jars, zip, or other directories containing Java files;
Note the difference between the file path separators under Windows and Linux and the file list (that is,-classpath and-sourcepath specified files) separators:
Windows under File path delimiter with \, file list delimiter with semicolon;
Linux under File path delimiter with/, file list delimiter with colon:



How to compile Java projects with shell scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.