Compiling source files from within an application

I have a Java application which generates Java source files.

Can anyone suggest a good cross-platform way of compiling these sources from within the app?


Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

JodConnect With a Mentor Commented:
Will this not do? Call this method with the path to the class file you wish to compile:

public void runCompiler (String s) {
  try {
    Process p = Runtime.getRuntime().exec("javacc " + s);

  } catch (Exception err) {
    System.out.println("EXEC failed: " + err.toString());

So call it like this...


Ideally you could extend this to read the output from the compiler and explicitly wait till it is finished compiling by waiting for Process p to end (p.waitFor()).

If the Java compiler javac is in the command path for the computer this code is running on then this will always work. If you are running Java code generators then this should usually be true.
or you can play with[] javaFileNameas_and_params);
which is slightly more paltform dependant (it works only in Sun compatible JavaVMs).

if you use java executable, you'd better use the full path (ask the user for it)

finally you can generate .class files directly (read the bytecode specification :)
wagtailAuthor Commented:
Excellent answers, thanks!

Can someone remind me how to split the points between two experts?

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

>> finally you can generate .class files directly (read the bytecode specification :)

And how much fun you will have...

The easy way to split the points is to give them all to me...otherwise, you can reduce the points available for this question to 50 and award the answer to one of us and then post another 50 point question for the other person with a reference to this question and they can then post an answer to that one.

Not as easy as it could be, but there you go.
One sad thing I forgot to mention - check out the first eight bytes of the BYTECODE specification when spelled out in Hex.

Sad, but true...
wagtailAuthor Commented:
Thanks a lot!
>> And how much fun you will have...

much more fun than starting external compiler :) - common practive for generating RMI / Corba stubs / skeltons (and EJB wrappers ?) is to generate directly .class files ... it all depends on your situation

btw. I suppose that you are talking about the first 4 bytes :)
Just checking, heyhey_ :-) My brain always goes numb when starting to read hex...

Cheers, wagtail.
All Courses

From novice to tech pro — start learning today.