3

Instead of using a .bat file, how the code can be built for java program for compiling and executing a list of java programs.

LGAP
  • 2,365
  • 17
  • 50
  • 71

6 Answers6

6

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

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 2
    @polygenelubricants: Well, I posted this as answer because I *really* think that this is what the OP needs, even if he doesn't know it yet :) – Pascal Thivent Jul 26 '10 at 08:39
3

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

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
0

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.

ZeBlob
  • 11
  • 1
  • `waitFor` is not enough; I/O streams must be drained to prevent deadlock. – polygenelubricants Jul 26 '10 at 07:59
  • There are [many things wrong with this code](http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html). Unfortunately executing external commands is not as simple as that. Read the linked article for the most important pitfalls. – Joachim Sauer Jul 26 '10 at 08:51
0

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.

David Young
  • 4,643
  • 2
  • 21
  • 18
0

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.

Nealv
  • 6,856
  • 8
  • 58
  • 89
0

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

Benoit Courtine
  • 7,014
  • 31
  • 42