Original: http://blog.csdn.net/u010900574/article/details/50792353 compile multiple jar packages and Java files simultaneously
We often need to compile and run Java files from the command line when we use the code on a regular basis or when we deploy it to a Linux server, and most of the methods on the Web are
javac -cp filePath/jarName.jar javaName.java
This method. But joined in the FilePath folder there are several jar packages, in the SRC folder there are several Java files, this method is very clumsy, I look at the online information is very chaotic, in fact, the following a very simple command can be solved:
javac -Djava.ext.dirs=filePath *.java//注意这里只需要指定到jar包所在的文件夹就可以了
*.java
it means that all Java files can be compiled at once.
Place the compiled file in the specified directory
If you want to put the compiled file into the specified directory, just add it to the Java file -d outputFilePath
, such as the following:
javac -Djava.ext.dirs=filePath *.java -d outputFilePath
Executing Java files
Execute the Java Program page is the same, only need to javac
replace java
, write the main function in the name of the Java file, do not have to add a suffix. For example, below
javac -Djava.ext.dirs=filePath MainJavaName
If you want to put the compiled file into the specified directory, just add it to the Java file -d outputFilePath
.
Example Demo
Let's take a practical example to demonstrate
Under the Serverprogram folder there is a labrary folder, there are several jar files, SRC is the source package, there are three Java files, the output folder is where I want to export compiled files,
Compilation process
//首先进入Src文件夹javac -Djava.ext.dirs=../Library *.java -d ../Output//指定输出编译文件的位置为上级目录的Output文件夹
Execution process
Enter the output folder first
//主函数所在文件是Server.javajava -Djava.ext.dirs=../Library Server
"Java" "Go" compile and run Java on the command line