0

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.

  • Maybe a double click runs the jar in the JRE and in the console you run it in the JDK? See also https://stackoverflow.com/questions/15513330/toolprovider-getsystemjavacompiler-returns-null-usable-with-only-jre-install – keuleJ Nov 06 '19 at 18:56
  • You are probably right @keuleJ and I see that this method actually looks for the compiler in a jar file which is only installed together with a jdk. I'll try to solve the rest myself. Thanks. – BoBoLittleStar Nov 06 '19 at 19:12

0 Answers0