3

My project in Eclipse is running WITH SYSTEM JRE 9. When I try to set Java home to JDK 9 to get the system compiler I got null.

System.setProperty("java.home", "C:\\Program Files\\Java\\jdk-9.0.1");
System.out.println(System.getProperty("java.home")); // print C:\Program Files\Java\jdk-9.0.1
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // returns null

This code previously works fine with JDK 8 (running from JRE 8)

System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.8.0_152");
System.out.println(System.getProperty("java.home")); // print C:\Program Files\Java\jdk1.8.0_152
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // returns compiler

Has there been any change to how ToolProvider.getSystemJavaCompiler requirement works?

Update: not sure if it matters but getSystemJavaCompiler() works fine if I run my project from JDK 9 (change to java.home is no longer required) as opposed to JRE 9.

Naman
  • 27,789
  • 26
  • 218
  • 353
vda8888
  • 689
  • 1
  • 8
  • 19
  • Of course with JRE alone it wouldn't work. I said that this works when I launch with JRE 8 and change the `java.home` to JDK 8 directory. What I don't understand is that this stops working for JRE9 - JDK9 pair. – vda8888 Dec 05 '17 at 15:29
  • @vda8888 with JRE9 does setting the path to `...jdk-9.0.1.jdk\\Contents\\Home` work for you? – Naman Dec 05 '17 at 15:33
  • 2
    It was never specified that the system property `java.home` has any relevance to this feature. So you were using an implementation detail of previous Java versions that has been changed. The whole purpose of the `ToolProvider` API is to stop using implementation details for accessing the compiler. And why are you doing this? Why don’t you just run your application with that jdk, by simply specifying that jdk in Eclipse’s Launch Configuration? – Holger Dec 05 '17 at 17:07
  • I'm exporting my project into a .jar and it's often launched by double clicking, which naturally invokes JRE on most computers. The jar would still run, except that the compilation feature is disabled unless the user re-configure the java home to JDK dir. Unless this is impossible now in Java 9, I think it wouldn't make sense to ask the user to launch the jar with JDK when JRE is sufficient (and only ask for JDK if I need compilation feature). – vda8888 Dec 05 '17 at 21:07
  • @nullpointer There is no \\Contents\Home in my JDK installation. I installed mine directly from Oracle. – vda8888 Dec 06 '17 at 00:22
  • 1
    @vda8888 Primarily a directory which includes `lib/modules` is what I meant. Anyway, why would you even need to set the path manually as @Holger points out as well. – Naman Dec 06 '17 at 01:00
  • I thought Java 9 was supposed to get rid of this extra JRE beside the JDK. I didn’t check this, as I never install this additional JRE. – Holger Dec 06 '17 at 07:35
  • @Holger, you're right, there is no separate JRE download for Java 9, nor is there a `jre` folder inside the JDK. That issue is gone for good. – Stephan Herrmann Dec 06 '17 at 23:58
  • 1
    Changing the java.home system property in a running VM is madness, goodness knows what will break. I assume the reason your code worked in JDK 8 is that the javac compiler was in tools.jar and the API loaded it from ${java.home}/tools.jar. As noted in other posts, tools.jar does not exist any more. If you are looking to invoke javac in another run-time image then you'll need to invoke it with ProcessBuilder and launch in its own VM. – Alan Bateman Dec 07 '17 at 08:15

1 Answers1

1

Correspondingly changing the path on Unix to /Contents/Home works for me :

System.setProperty("java.home", 
                   "/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/");

Though there doesn't seem to be a need of setting the property at all. Since the following line of code even independently executes fine as well:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 

with JAVA_HOMEon my system set to point to JDK9 Home directory.

Naman
  • 27,789
  • 26
  • 218
  • 353