开发者

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

Instead of using a .bat file, how the code 开发者_StackOverflow社区can be built for java program for compiling and executing a list of java programs.


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

1Just 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

  • Is java Runtime.exec(String[]) platform independent?
  • What is the purpose of Process class in Java?
  • How can I compile and deploy a java class at runtime?
  • Compiling a class using Java code using process
  • Java: Executing a Java application in a separate process
  • Running a program from within Java code..

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

  • Draining Standard Error in Java

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

  • interface JavaCompiler from package javax.tools
    • external tutorial article

Related questions

  • Null Pointer Exception while using Java Compiler API


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜