Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 528752

Summary: need a way to configure java home when using batch compiler Main in-process
Product: [Eclipse Project] JDT Reporter: Jan Sievers <jan.sievers>
Component: CoreAssignee: Jay Arthanareeswaran <jarthana>
Status: VERIFIED FIXED QA Contact:
Severity: enhancement    
Priority: P2 CC: akurtakov, christian.dietrich.opensource, daniel_megert, jarthana, manoj.palat, sarika.sinha, stephan.herrmann
Version: 4.8   
Target Milestone: 4.8 M7   
Hardware: All   
OS: All   
See Also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=528905
Whiteboard:
Bug Depends on:    
Bug Blocks: 514471    

Description Jan Sievers CLA 2017-12-14 03:33:59 EST
this is a follow-up on tycho bug 514471

Tycho is using JDT's batch compiler Main for in-process compilation [1]. We want to be able to support compiling against a JDK different from the JDK currently running (for any JDK version up to and including Java 9) without having to fork a process for compilation.

Alternatively Tycho could also use -bootclasspath CLI argument and do all the scanning for JDK jars/modules by itself but I think this is tedious and highly dependent on JDK file system layout, and JDT knows much better how to do this already.

To me it looks like the easiest way could be to make the setJavaHome(String javaHome) method public[2], but I'm open to other suggestions. Right now I used reflection to set javaHome in a the gerrit proposal [1] just to be able to prove that this would solve the problem in Tycho but of course this cannot be the final solution.
In any case what we want to avoid is to use JDT internals (see also bug 483300	).


[1] https://git.eclipse.org/r/#/c/112949/7/tycho-compiler-jdt/src/main/java/org/eclipse/tycho/compiler/jdt/JDTCompiler.java
[2] https://github.com/eclipse/eclipse.jdt.core/blob/dba3e915437941c34bf2f546367d44bd60a979a2/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java#L3425
Comment 1 Dani Megert CLA 2018-01-16 07:45:04 EST
Manoj or Jay, please work on this.
Comment 2 Jay Arthanareeswaran CLA 2018-01-16 07:48:22 EST
If it's just making the setJavaHome public, the fix should be straight forward, as Tycho already uses this internal class anyway. I will work on this for M5.
Comment 3 Stephan Herrmann CLA 2018-01-16 08:33:27 EST
(In reply to Jay Arthanareeswaran from comment #2)
> If it's just making the setJavaHome public, the fix should be straight
> forward, as Tycho already uses this internal class anyway. I will work on
> this for M5.

I'm afraid there's a little more to it, see bug 514471 comment 52

Elsewhere there was mention that early JDKs don't have the expected "release" file (bug 528905 comment 14 onward), which also may have impact on this API, not sure.

Jan is probably in a better position to describe the current state of the art.
Comment 4 Jan Sievers CLA 2018-01-16 11:32:55 EST
(In reply to Stephan Herrmann from comment #3)
> (In reply to Jay Arthanareeswaran from comment #2)
> > If it's just making the setJavaHome public, the fix should be straight
> > forward, as Tycho already uses this internal class anyway. I will work on
> > this for M5.
> 
> I'm afraid there's a little more to it, see bug 514471 comment 52
> 
> Elsewhere there was mention that early JDKs don't have the expected
> "release" file (bug 528905 comment 14 onward), which also may have impact on
> this API, not sure.
> 
> Jan is probably in a better position to describe the current state of the
> art.

yes unfortunately looks to me like it's not that easy.

Main.setJavaHome() only works for JDKs >= 7 and after having a closer look, there are places where System.getProperty() is used to determine the bootclasspath, so it looks like the code assumes we want to compile against the JDK currently running.

The method org.eclipse.jdt.internal.compiler.util.Util.collectPlatformLibraries(File javaHome) looked promising to me 

https://github.com/eclipse/eclipse.jdt.core/blob/3c7b09999b4c547cf6cdc2269b322f8997d9f385/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java#L1171

but it looks to me like it does not really use the javaHome passed as parameter (and it's internal). 

E.g. using Java 8, it will always collect the bootclasspath of the currently running VM regardless of the javaHome dir passed in as parameter
Using Java 9, it will return a single instance of org.eclipse.jdt.internal.compiler.batch.ClasspathJrt
Comment 5 Jay Arthanareeswaran CLA 2018-01-16 11:41:01 EST
(In reply to Jan Sievers from comment #4)
> but it looks to me like it does not really use the javaHome passed as
> parameter (and it's internal). 
> 
> E.g. using Java 8, it will always collect the bootclasspath of the currently
> running VM regardless of the javaHome dir passed in as parameter

Unfortunately, as you said, the boot classpath is always fetched from the current JRE. For the other libraries, though, you can see java home is being used.
Comment 6 Jan Sievers CLA 2018-03-12 06:00:57 EDT
(In reply to Jan Sievers from comment #0)
> Alternatively Tycho could also use -bootclasspath CLI argument and do all
> the scanning for JDK jars/modules by itself

this is what we ended up doing by spawning a LibraryDetector VM

https://git.eclipse.org/r/#/c/115711/
Comment 7 Jay Arthanareeswaran CLA 2018-04-17 02:32:09 EDT
So, Tycho built the fix in their own code. And the fix in JDT Core looks not easy anyway. Do we still need this bug report?
Comment 8 Jan Sievers CLA 2018-04-17 02:59:34 EDT
problem is solved as far as Tycho is concerned
Comment 9 Sarika Sinha CLA 2018-05-09 04:42:45 EDT
As per comment 8.