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();
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.
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.)
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.
-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.
Need to pass your compiler options in compiler.getTask. The option is -d
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With