Sweet Dream
Sweet Dream

Reputation: 245

How can I create a Java class file from a Java file from my program?

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?

Upvotes: 4

Views: 2930

Answers (3)

11684
11684

Reputation: 7517

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

Upvotes: 0

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23349

yes , you can do it using java compiler

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

Check this for further details

Upvotes: 1

Adriano Repetti
Adriano Repetti

Reputation: 67138

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

Upvotes: 3

Related Questions