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 that will generate the .class file of this Java file and put the .class file in the same external folder?

+4
source share
3 answers

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

+3
source

yes you can do it with java compiler

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

Check it out for more details.

+1
source

Here: java.lang.Compiler . This class has a compileClass(Class class) and compileClasses(String names) method

0
source

Source: https://habr.com/ru/post/1411076/


All Articles