I was trying a code-generating and self-compiling program using Java, but found some issues about the compiler it provided with ToolProvider.getSystemJavaCompiler(). Problems are following:
When running in Eclipse, Compiler c = ToolProvider.getSystemJavaCompiler()
successfully returns a compiler and c.run(null, null, null, "AValidJavaFile.java")
compiles the file, with a zero value returned.
When I export it to a runnable Jar file and try to double click on it, it no longer works, not even if I add the tools.jar to its referenced libraries.
But, when I use java -jar TheCompiler.jar
in my terminal, it works again even without the tools.jar in the classpath, and now I'm confused with this method. What exactly is this and when exactly will it work?? And is there a way that I can use it in a regular runnable Jar file?
I'm on a Mac, version 10.14.6. Java version is 1.8.0_191.