Bug 371300 - JavaRuntime computeVMInstall method returns the first VM instead of the workspace default VM if no compatible VM version is found for the given launch configuration
Summary: JavaRuntime computeVMInstall method returns the first VM instead of the works...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Debug (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows 7
: P3 minor (vote)
Target Milestone: 3.8 M6   Edit
Assignee: JDT-Debug-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
Depends on:
Blocks: 484026
  Show dependency tree
 
Reported: 2012-02-10 20:19 EST by Samrat Dhillon CLA
Modified: 2015-12-10 03:22 EST (History)
6 users (show)

See Also:


Attachments
simple patch to change JREContainerInitializer (1.00 KB, patch)
2012-02-10 20:27 EST, Samrat Dhillon CLA
no flags Details | Diff
Patch to only use the workspace default vm when it is compatible to the EE. (584 bytes, patch)
2015-12-09 05:17 EST, Jens Schiebel CLA
no flags Details | Diff
Added screenshot showing Execution Environments mapped to incomaptible VM. (45.74 KB, image/png)
2015-12-09 05:22 EST, Jens Schiebel CLA
no flags Details
Screenshot showing installed JREs. (50.17 KB, image/png)
2015-12-09 07:39 EST, Jens Schiebel CLA
no flags Details
Screenshot showing Exe Env list. (70.29 KB, image/png)
2015-12-09 07:40 EST, Jens Schiebel CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Samrat Dhillon CLA 2012-02-10 20:19:25 EST
Build Identifier: 3.7.1

JavaRuntime computeVMInstall(ILaunchConfiguration) method returns the very first VM from {workspace}\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.launching.prefs instead of the workspace default VM if no compatible VM version is found for the given launch configuration. 

This may not seem a serious issue but it creates a lot of confusion to the user. This behaviour was noticed/discovered while using the maven m2e eclipse plugin. 

IMHO JavaRuntime computeVMInstall(ILaunchConfiguration) should return the workspace default JVM and not the first VM from the preference file.

The JREContainerInitializer: public static IVMInstall resolveVM(IExecutionEnvironment environment) method should be modified so that it returns the default workspace VM.

	public static IVMInstall resolveVM(IExecutionEnvironment environment) {
		if (JREContainer.DEBUG_JRE_CONTAINER) {
			System.out.println("<JRE_CONTAINER> resolveVM(IExecutionEnvironment)"); //$NON-NLS-1$
		}
		IVMInstall vm = environment.getDefaultVM();
		if (vm == null) {
			IVMInstall[] installs = environment.getCompatibleVMs();
			// take the first strictly compatible vm if there is no default
			if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) {
				System.out.println("\t*** NO COMPATIBLE VMS ***"); //$NON-NLS-1$
			}
			for (int i = 0; i < installs.length; i++) {
				IVMInstall install = installs[i];
				if (environment.isStrictlyCompatible(install)) {
					vm = install;
					if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) {
						System.out.println("\tPerfect Match: " + vm.getName()); //$NON-NLS-1$
					}
					break;
				}
			}
			// use the first vm failing that
			if (vm == null && installs.length > 0) {
				vm= JavaRuntime.getDefaultVMInstall();
				if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) {
					System.out.println("\tFirst Match: " + vm.getName()); //$NON-NLS-1$
				}
			}
		} else {
			if (JREContainer.DEBUG_JRE_CONTAINER) {
				System.out.println("\tUser Default VM: " + vm.getName()); //$NON-NLS-1$
			}
		}
		return vm;
	}

Reproducible: Always

Steps to Reproduce:
The below instructions are just an example how this behaviour causes confusion with maven. This behaviour has nothing to do with m2e plugin itself!

1. I had two JVM's installed JRE 7 and JDK 1.6. 
2. Eclipse starts up and discovers JRE 7 and automatically adds it as the default JRE for the workspace.
3. I then add the JDK 1.6 VM from the preferences and made it the workspace default VM.

4. I create a project using m2e plugin and the execution environment for the project is set to J2SE 1.5.

5.When I compile the project using maven install for the first time. It works.
6.Now I run maven clean
7.Now if you try to run maven install. Maven complains that JDK is not installed.
The user has already added a JDK VM in the preferences and has set it as the workspace default and it creates a lot of confusion.
Comment 1 Samrat Dhillon CLA 2012-02-10 20:27:38 EST
Created attachment 210880 [details]
simple patch to change JREContainerInitializer
Comment 2 Ayushman Jain CLA 2012-02-11 00:33:39 EST
Moving to JDT/Debug
Comment 3 Michael Rennie CLA 2012-02-21 15:22:26 EST
(In reply to comment #0)

> Steps to Reproduce:
> The below instructions are just an example how this behaviour causes confusion
> with maven. This behaviour has nothing to do with m2e plugin itself!
> 
> 1. I had two JVM's installed JRE 7 and JDK 1.6. 
> 2. Eclipse starts up and discovers JRE 7 and automatically adds it as the
> default JRE for the workspace.
> 3. I then add the JDK 1.6 VM from the preferences and made it the workspace
> default VM.
> 
> 4. I create a project using m2e plugin and the execution environment for the
> project is set to J2SE 1.5.

So at this point you have installed JREs and everything is fine.

> 5.When I compile the project using maven install for the first time. It works.
> 6.Now I run maven clean

But after whatever Maven clean does you now have no installed JREs or the Maven tooling is failing to find them. 

If you check your JRE preferences are they all still installed (on the workspace preference page)?

> 7.Now if you try to run maven install. Maven complains that JDK is not
> installed.
> The user has already added a JDK VM in the preferences and has set it as the
> workspace default and it creates a lot of confusion.

I am going to pass this over to the m2e guys to see what they have to say (since I have no idea how their tooling works)
Comment 4 Samrat Dhillon CLA 2012-02-21 15:42:48 EST
As I wrote in the description maven is just an example here. There is nothing maven specific here. The only reason why I used maven example is because maven fails to compile with a VM created from a JRE. Why it works the first time, frankly I did not investigate that.

I will describe you a scenario without maven.
1. I have a system with JDK 6 and JRE 7.
2. Start up eclipse 
3. Go to preferences ->installed JRE ;and you will notice that JRE 7 is already there, perhaps JNI adds the most recent version.
4. Add JDK 6 to the installed JREs and make it the workspace default but do not remove JRE 7.
5. create a new java project that uses execution env. J2SE5.
6. Run this project from eclipse.

Notice in the console JRE 7 is used to run this project and not JDK 6; although JDK 6 is the workspace default.
Comment 5 Igor Fedorenko CLA 2012-02-21 15:58:59 EST
@Michael judging by comment #4 the issue is not specific to m2e. 

@Samrat you can select JRE in Maven Launch configuration JRE tab
Comment 6 Michael Rennie CLA 2012-02-21 17:03:27 EST
(In reply to comment #5)
> @Michael judging by comment #4 the issue is not specific to m2e. 
> 

I agree. Samrat, you should have used the example from comment 4 at the start :)

Moving back to debug.
Comment 7 Samrat Dhillon CLA 2012-02-21 17:07:28 EST
(In reply to comment #6)
> (In reply to comment #5)
> > @Michael judging by comment #4 the issue is not specific to m2e. 
> > 
> 
> I agree. Samrat, you should have used the example from comment 4 at the start
> :)
> 
> Moving back to debug.

Michael, it doesn't seem a very noticeable thing in such a small example :) but nevertheless in the original description I did mention that this is not a maven specific thing :).

Cheers
Samrat
Comment 8 Michael Rennie CLA 2012-02-21 17:11:31 EST
pushed the patch + minor update to master:

http://git.eclipse.org/c/jdt/eclipse.jdt.debug.git/commit/?id=919e71f91e5624d95d0c135fdb24658caa756d70

I retained the old logic in the case the default VM install is null.

Thanks for the patch Samrat!
Comment 9 Jens Schiebel CLA 2015-12-08 11:55:16 EST
> Notice in the console JRE 7 is used to run this project and not JDK 6;
> although JDK 6 is the workspace default.

Ok, so you want the workspace default vm to be used IF it is compatible to the Execution Environment.

But there are cases where the workspace default vm is NOT compatible to the Execution Environment!
Comment 10 Jens Schiebel CLA 2015-12-08 12:11:17 EST
Sorry guys,

but this issue is not fixed correctly at the moment.

You can only use the workspace default vm if it is part of the list of compatible vms for the examined execution environment!

In the moment the code is like this:

1.
Take the first perfect match.

2.
If no perfect match, use workspace default vm.

3.
If no workspace default vm, use first compatible vm from list.

But what if the workspace default vm is NOT compatible with the exe env?
Step 2 is wrong, because you cannot use the workspace default vm unconditionally.
You can only use the workspace default vm if it is compatible to the EE!


The code must look like this:


if( vm == null &&
    installs.length > 0 && Arrays.asList(env.getCompatibleVMs()).contains(JavaRuntime.getDefaultVMInstall())) {
       vm = JavaRuntime.getDefaultVMInstall();
}
Comment 11 Jens Schiebel CLA 2015-12-09 05:17:46 EST
Created attachment 258533 [details]
Patch to only use the workspace default vm when it is compatible to the EE.

I added a patch that shows you how the code should look.
Would be nice if this is fixed in Eclipse 4.5.2 release.

I stumbled over these lines of code because I am working
for a company that offers a real-time capable VM named JamaicaVM.
We have an Eclipse plug-in so users can add our VM to Eclipse.
I have a screenshot that shows you the effects.
Comment 12 Jens Schiebel CLA 2015-12-09 05:22:41 EST
Created attachment 258534 [details]
Added screenshot showing Execution Environments mapped to incomaptible VM.

EE 'JamaicaVM-6' and 'JavaSE-1.8' are mapped to incompatible VMs because workspace default VM is used even when it is not compatible to EE.
Comment 13 Sarika Sinha CLA 2015-12-09 05:37:19 EST
Jens,
From the screenshot, I am not sure how Java 1.8 can be mapped to 1.7? We can select only from the compatible list of JREs which is provided in the table ?
Comment 14 Jens Schiebel CLA 2015-12-09 07:39:31 EST
Created attachment 258537 [details]
Screenshot showing installed JREs.
Comment 15 Jens Schiebel CLA 2015-12-09 07:40:13 EST
Created attachment 258538 [details]
Screenshot showing Exe Env list.
Comment 16 Jens Schiebel CLA 2015-12-09 07:43:13 EST
(In reply to Sarika Sinha from comment #13)
> Jens,
> From the screenshot, I am not sure how Java 1.8 can be mapped to 1.7? We can
> select only from the compatible list of JREs which is provided in the table ?

Hi Sarika,

I added two more screenshots to explain the situation.
One shows my Installed JREs and the second shows the list
of compatible VMs for EE 'JavaSE-1.8'.

There is no perfect match for 'JavaSE-1.8' Exe Env,
that's why he uses the workspace default VM, which is JDK 1.7!

I hope you can see that this is a wrong behaviour! :)

Kind Regards,
Jens
Comment 17 Sarika Sinha CLA 2015-12-09 07:52:41 EST
Hi Jens,
Thanks for attaching the screenshots, So let me understand this. In the screenshot, For Java SE 1.8, there is no perfect match but there is a compatible VM but it still uses the default VM 1.7. I hope I have stated the problem correctly.

If my understanding is correct, Please create a new bug so that I can work on it.

Thanks.
Comment 18 Jens Schiebel CLA 2015-12-09 09:38:58 EST
(In reply to Sarika Sinha from comment #17)
> Hi Jens,
> Thanks for attaching the screenshots, So let me understand this. In the
> screenshot, For Java SE 1.8, there is no perfect match but there is a
> compatible VM but it still uses the default VM 1.7. I hope I have stated the
> problem correctly.
> 
> If my understanding is correct, Please create a new bug so that I can work
> on it.
> 
> Thanks.

Hi Sarika,

in the moment, when there is no perfect match for an Exe Env,
he just unconditionally takes the workspace default VM,
even when that is NOT compatible with the passed Exe Env.
That's the bug.

I will open a new bug entry as you requested.
Thanks for your help.

Regards,
Jens
Comment 19 Jens Schiebel CLA 2015-12-09 10:18:48 EST
(In reply to Sarika Sinha from comment #17)

> If my understanding is correct, Please create a new bug so that I can work on it.

Sarika,

I created this bug entry for the issue:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=484026
Comment 20 Sarika Sinha CLA 2015-12-10 03:22:17 EST
Thanks Jens