4

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?

2
  • 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. Commented May 7, 2012 at 9:59
  • what do you mean with external? Commented May 7, 2012 at 10:00

3 Answers 3

3

The easiest way (for me) is to include tools.jar and to invoke the com.sun.tools.javac.Main.compile() function.

Sign up to request clarification or add additional context in comments.

6 Comments

Note that sun.tools.javac.Main is replaced by javax.tools.JavaCompiler refer here
@PauKiatWee you're right, I just feel more comfortable ("...the easiest way...") with the old quick and dirty interface.
can u please tell me where can I download the tools.jar? And one thing, this codeI have to implement in struts 1 frmawork.
Usually in the lib directory of root Java SDK installation directory.
@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?
|
1

yes , you can do it using java compiler

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null,null,null); 

Check this for further details

Comments

0

It's in here: java.lang.Compiler. This class has the method compileClass(Class class) and compileClasses(String names)

3 Comments

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.
According to the documentation, it does. I updated the link to point to the Java 6 documentation. @boneill
Can you provide a working example of this class compiling Java source code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.