See which of your colleagues or former colleagues are already on Java Link: Check out the Contact Finder
News »Browse Articles » How to Compile Java Source within Java Program
0
Vote Vote

How to Compile Java Source within Java Program

Views 3 Views    Comments 0 Comments    Share Share    Posted 17-04-2009  
This small trick will tell you how you can compile a java source file from another java file on the go. You do not need to run it on a different page and compile it there and then get back to this program. This will be immensely helpful for a program that has some dynamic segments of codes which need to be compiled here within. Basically what we are gonna do is we will use getSystemJavaCompiler() method in the ToolProvider class that returns an object of some class that implements JavaCompiler.

1. The implementation needs tools.jar file usually available in the libtools.jar. So you need to give the proper directory path in the class path so that we can use import javax.tools.*
Or you can use

cp /usr/java/jdk/lib/tools.jar /usr/java/jdk/jre/lib/ext/

this command above to change it from terminal.

2. Now to compile that program from within another Java file, here is the code snippet you need to use

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if(compiler.run(null, null, null, fileName + ".java") != 0) {
// Error message
}

Source:
http://blog.taragana.com/index.php/archive/how-to-compile-java-source-within-jav
0
Vote  Vote
Enter your comment:
No Comments For This News

Search News

What's the News?

Post a link to something interesting from another site, or submit your own original writing for the Java community to read.

Most Popular News

Most Recent User Submitted News