News »Browse Articles »
How to Compile Java Source within Java Program
0
How to Compile Java Source within Java Program
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
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
Search News
News Categories
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
-
How to stand out from other Java/JEE Professionals?
Published about 14-01-2009 | Rated +3 -
10 reasons IT certification will be important in 2009
Published about 05-01-2009 | Rated +2 -
The 9 hottest skills for `09
Published about 02-01-2009 | Rated +1 -
New Features in Servlets 3.0
Published about 05-01-2009 | Rated +4
Most Recent User Submitted News
- SpringSource Tool Suite 2.0 released
Submitted by Sonal | Rated +1 - Multicore Software Development Kit
Published about 05-11-2009 | Rated 0 - IBM Real Time Application Execution Optimizer for Java
Published about 01-09-2009 | Rated 0 - Which object oriented Concept is achieved by using overloading and overriding?
Submitted by Balamurali | Rated 0







