Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaCompiler - set the compiled class output folder

I'm using Eclipse and therefore my class files are stored in "bin" in the project folder. How can I set the JavaCompiler to output compiled classes into this "bin" folder?

My code:

File fRun = new File("FileToCompile");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compUnits =  fileManager.getJavaFileObjects(fRun);
Boolean compRes = compiler.getTask(null, fileManager, null, null, null, compUnits).call();          

if(compRes == true){
    System.out.println("Compilation has succeeded");
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Class<?> compiledClass = cl.loadClass("data.testcases.TestA");
    cRun = compiledClass;
}else{
    System.out.println("Compilation error");
    fileManager.close();
like image 567
arket Avatar asked Mar 12 '12 10:03

arket


People also ask

How do I specify an output javac?

By default, "javac" will output the class file in the same directory as the source file. But you can change this default behavior by using the "-d" option. It will make "javac" to output the class file into the specified directory.

What is the output of javac?

The output of javac is always classfiles, and the name of the file matches the name of the class contained within it. (A source file with multiple classes in will result in multiple output files.)

Where is Java compiler located?

The value that you want to add is most likely C:\Program Files\Java\jdk-14.0. 2\bin if you are installing "JDK 14.0. 2". This is the location where the Java compiler ('java.exe') was installed in your file system.

What is javac D?

-d is an option for javac which is used to specify where is root directory to store the compiled java binary files. In our tutorial, we will store our Java source files and compiled class files in separate directories.


1 Answers

Need to pass your compiler options in compiler.getTask. The option is -d

like image 127
Chetter Hummin Avatar answered Nov 08 '22 03:11

Chetter Hummin