Compiling java code from within Java

J

John Morey

Hi

I am working on a program to test java code, my program needs to be
able to know if a certain class (loaded from a .java file) would
compile properly via a java compiler.

What would be the best method of doing this? I have looked at the
Compiler class but this does looks like something intended for use
within the java virtual machine itself...

thanks for any help
 
J

John Harlow

John said:
Hi

I am working on a program to test java code, my program needs to be
able to know if a certain class (loaded from a .java file) would
compile properly via a java compiler.

What would be the best method of doing this? I have looked at the
Compiler class but this does looks like something intended for use
within the java virtual machine itself...

thanks for any help

exec javac?
 
M

Michael Borgwardt

John said:
I am working on a program to test java code, my program needs to be
able to know if a certain class (loaded from a .java file) would
compile properly via a java compiler.

What would be the best method of doing this? I have looked at the
Compiler class but this does looks like something intended for use
within the java virtual machine itself...

A compiler API is being planned (JSR 191), but not currently avilable.
Thus, you have to calling Sun's javac either through Runtime.exec()
or directly via com.sun.tools.javac.Main (it's in tools.jar) and
live with its restrictions (only an integer return code, only files
as input) - or use a separate compiler such as IBM's Jikes or the
one eclipse uses. They probably have a richer API.
 
M

Michael Borgwardt

Chris said:
Oddly, there does seem to be /something/...

http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javac.html#proginterface

But there's no equivalent section in the documentation for the Windows
version of the same tool.

That's what people have been using for years, and it works the same on Windows.

It seems to be an acknowledgement of the status quo, to tell people that Sun is
aware that many people are using these methods, and reassuring them that they
can safely be used - but not anything else.

But if JSR 191 gets implemented, I would expect a much richer interface that
allows programmatical analysis of compilation errors instead of just dumping
it to STDERR.
 
G

Gordon Beaton

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top