Suppose there is a Java file that exists in an external folder. Can I write a Java program which will generate a .class file of that Java file and place the .class file in the same external folder?
-
No Sir. Surely javac can create class file from a .java file. But I have to do that without using javac and to run my own program to create that class.Sweet Dream– Sweet Dream2012-05-07 09:59:08 +00:00Commented May 7, 2012 at 9:59
-
what do you mean with external?11684– 116842012-05-07 10:00:24 +00:00Commented May 7, 2012 at 10:00
Add a comment
|
3 Answers
The easiest way (for me) is to include tools.jar and to invoke the com.sun.tools.javac.Main.compile() function.
6 Comments
Pau Kiat Wee
Adriano Repetti
@PauKiatWee you're right, I just feel more comfortable ("...the easiest way...") with the old quick and dirty interface.
Sweet Dream
can u please tell me where can I download the tools.jar? And one thing, this codeI have to implement in struts 1 frmawork.
Adriano Repetti
Usually in the lib directory of root Java SDK installation directory.
Sweet Dream
@Adriano Thanks all of you for the help. I have placed the tools.jar in my eclipse lib. But still I am not able to use javax.tools.JavaCompiler. Can't I use this code for my sturts application?
|
yes , you can do it using java compiler
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null,null,null);
Comments
It's in here: java.lang.Compiler.
This class has the method compileClass(Class class) and compileClasses(String names)
3 Comments
boneill
The Compiler class should be deprecated. It has nothing to do with compiling Java source code, and in modern JVMs, it doesn't do anything at all.
11684
According to the documentation, it does. I updated the link to point to the Java 6 documentation. @boneill
boneill
Can you provide a working example of this class compiling Java source code?