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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

>> 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.