简体   繁体   中英

How to create a java progam to compile and run a list of java programs

如何为java程序构建代码以编译和执行java程序列表,而不是使用.bat文件。

I strongly recommend to use an existing build tool like Ant or Maven 1 for this. These tools exist for years, have been widely used, tested, they are the way to go. Just do not reinvent the wheel.

1 Just in case you wanted to know, internally, these tools use the old and undocumented com.sun.tools.javac.Main class from tools.jar to programmatically invoke javac

On Runtime.exec

Though perhaps not the most ideal solution, you an execute a shell command as a separate Process using Runtime.getRuntime().exec(someCommand) . There are also overloads that takes parameters as a String[] .

This is not an easy solution. Managing a concurrent Process and preventing a deadlock etc is not trivial.

Related questions


On draining Process streams

Generally you can't just waitFor() a Process to terminate; you must also drain its I/O streams to prevent deadlock.

From the API :

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

Related questions


On the Java 6 Compiler API

One option to compiling a Java source code within Java is to use the Java 6 Compiler API. This requires a JDK to be installed (not just a JRE).

See also

Related questions

The java.lang.Runtime class has a method allowing you to execute arbitrary shell commands. So it should look something like this :

List<String> commandsToExecute = ...

for (String cmd : commandsToExecute) {
  Process p = Runtime.getRuntime().exec (cmd);
  p.waitFor(); // If you need to run them all sequentially.
}

There are several other versions of the Runtime.exec() method which are all described in the documentation .

Another issue with using Runtime.getRuntime().exec(someCommand) is that you need to read both the Output stream and Error streams from the spawn process otherwise your process will hang.

There is a limited amount of buffer available for both streams and once they fill the program will wait for you to read from them and be unable to continue. These two buffers must be read in their own separate threads so that one will not deadlock the other.

You can use ANT. instead of running the ANT from Eclipse or whatever, you can also run it from command. This means you can create a java program that executes commands -> ergo that executes ant with parameters.

These parameters can be derived from variables from the list of applications you want to build.

It doesn't directly answer the question, but some librairies can help to use the "Runtime.exec()" method (consuming I/O streams, etc.), to invoke "javac". For example this one , named "Shell" (french article where the library can be downloaded at the end).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM