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

Bug 334958

Summary: Ant: using Windows, "Run in same JRE as workspace" is not a sane default
Product: [Eclipse Project] Platform Reporter: Sven Köhler <sven.koehler>
Component: AntAssignee: Michael Rennie <Michael_Rennie>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: gro.espilce, Michael_Rennie
Version: 3.6.1   
Target Milestone: 3.7 M6   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
proposed fix none

Description Sven Köhler CLA 2011-01-20 17:58:05 EST
Build Identifier: 

This is probably not a bug, but a usability issue or even a request for enhancement. So here it is:

Assume a typical Windows PC, with a JDK installed. During the install of the JDK, a so-called "public JRE" is installed into another folder. The launcher of Eclipse detects and uses the JRE automatically. It does not detect the installed JDK, but it has been added manually to the "Installed JREs"-list in the preferences. The JRE has been removed from that list.

Now, try to use a build.xml that uses either the javac or the javadoc task. It fails immediatly.

Taking a closer look at created ant run configuration, it says "Run in same JRE as workspace" - which of course typically refers to a real JRE, without javac.exe, tools.jar, or javadoc.exe.

As you can imagine, this is really disturbing. This happens with any build.xml you're trying to run.

Please do something about it.
What if the eclipse launcher would prefer installed JDKs over the JRE? Or how about preferring installed JDKs over installed JREs when running ant? Or how about a new option called "Use same Execution environment as project" (people probably want to run ant with a JDK 1.5 if the project is configured to use JDK 1.5).

Reproducible: Always
Comment 1 Michael Rennie CLA 2011-01-21 10:24:57 EST
(In reply to comment #0)
> Taking a closer look at created ant run configuration, it says "Run in same JRE
> as workspace" - which of course typically refers to a real JRE, without
> javac.exe, tools.jar, or javadoc.exe.

Eclipse has no way of choosing a JRE over a JDK from Eclipses' POV they are one in the same.

> As you can imagine, this is really disturbing. This happens with any build.xml
> you're trying to run.

If you have both of them installed, the JDK and the JRE, why can you not simply choose the one you want when you are creating your launch configuration? 

The default selection is "run in separate VM", so you can choose a different one right then. The other option is that you could choose the installed JDK as the workspace default JRE and then all newly created launch configurations would automatically use the JDK.
Comment 2 Sven Köhler CLA 2011-01-21 11:48:45 EST
(In reply to comment #1)
> > As you can imagine, this is really disturbing. This happens with any build.xml
> > you're trying to run.
> 
> If you have both of them installed, the JDK and the JRE, why can you not simply
> choose the one you want when you are creating your launch configuration? 

Assume, that I'm using the simpliest way to start ant builds possible. I right click on a build.xml and select "Run As -> Ant build" or I drag&drop the build.xml to the Ant View and double click a target.

For me, this creates a launch configuration where "Run in the same JRE as the workspace" is checked. Of course, I can change this single launch configuration. I know that. However, I do not think that "Run in the same JRE as the workspace" is a good default.

> The default selection is "run in separate VM", so you can choose a different
> one right then. The other option is that you could choose the installed JDK as
> the workspace default JRE and then all newly created launch configurations
> would automatically use the JDK.

I just double checked, but for any newly created launch configuration, the item "Run in the same JRE as the workspace" is checked and not the "Seperate JRE".

Again, please understand that this is a complaint about how Eclipse behaves for most users out there. And I assume, that most people don't configure their Eclipse to start with a JDK - instead, they install the JDK, download Eclipse, and by default it uses the default JRE, AFAIK.

Am I wrong about any of that?
Comment 3 Sven Köhler CLA 2011-01-21 12:01:19 EST
(In reply to comment #1)
> The default selection is "run in separate VM"

I think, you might not be quite right about that. Can you take a look at the code, whether this is actually the default? Of my last 15 attempts of creating new Ant launch configuration, I have seen both:
1) new configurations which have "same JRE as workspace"
2) new configurations which have "seperate JRE" 

I think, there is some "remember last thing" logic implemented somewhere. However, it is confusing. I cannot find out, when Eclipse goes for (1) or when it goes for (2). Right now, I get the described behaviour again:
even after deleting all ant-related launch configurations, the result of double clicking a target in the Ant view is a launch configuration with "same JRE as worksapace".

I also wasn't able to find any possibility to change Eclipse's behaviour in the preferences.
Comment 4 Michael Rennie CLA 2011-01-21 12:05:43 EST
(In reply to comment #2)
> Assume, that I'm using the simpliest way to start ant builds possible. I right
> click on a build.xml and select "Run As -> Ant build" or I drag&drop the
> build.xml to the Ant View and double click a target.
> 
> For me, this creates a launch configuration where "Run in the same JRE as the
> workspace" is checked. Of course, I can change this single launch
> configuration. I know that. However, I do not think that "Run in the same JRE
> as the workspace" is a good default.

Right, we did introduce a regression for that option, which was fixed in bug 308645. So the default (again) for Eclipse builds >= 3.6M7 is the separate VM option.

> instead, they install the JDK, download Eclipse,
> and by default it uses the default JRE, AFAIK.

This is correct. Unfortunately the only way to use anything other than the default JRE is to configure Eclipse manually, using the Installed JREs page as I mentioned or to edit the launch configuration.
Comment 5 Sven Köhler CLA 2011-01-21 12:10:11 EST
(In reply to comment #4)
> (In reply to comment #2)
> > Assume, that I'm using the simpliest way to start ant builds possible. I right
> > click on a build.xml and select "Run As -> Ant build" or I drag&drop the
> > build.xml to the Ant View and double click a target.
> > 
> > For me, this creates a launch configuration where "Run in the same JRE as the
> > workspace" is checked. Of course, I can change this single launch
> > configuration. I know that. However, I do not think that "Run in the same JRE
> > as the workspace" is a good default.
> 
> Right, we did introduce a regression for that option, which was fixed in bug
> 308645. So the default (again) for Eclipse builds >= 3.6M7 is the separate VM
> option.

Interesting. I'm using Eclipse SDK, Version: 3.6.1, Build id: M20100909-0800.
Was the bug re-introduced?
Comment 6 Michael Rennie CLA 2011-01-21 12:18:08 EST
(In reply to comment #5)
> Interesting. I'm using Eclipse SDK, Version: 3.6.1, Build id: M20100909-0800.
> Was the bug re-introduced?

I just tried on 3.6.1 (M20101117-1123) and the 'separate VM' option is selected when I create a new configuration from either the launch dialog, editor or Ant view.
Comment 7 Michael Rennie CLA 2011-01-21 12:29:47 EST
I did find something curious though:

When you open the dialog and create a new configuration the JRE selected in the combo is the workspace default JRE. Whereas when you create a configuration from the editor or the Ant view, the JRE from the build path of the enclosing project will be selected in the combo: unless there is no JRE on the build path, then the option to use the same JRE as the workspace in selected!

I would assume that creating the configuration would work the same no matter where it is invoked (dialog, editor, view). 

Is this the behavior you are seeing Sven?
Comment 8 Sven Köhler CLA 2011-01-23 10:23:13 EST
(In reply to comment #7)
> I did find something curious though:
> 
> When you open the dialog and create a new configuration the JRE selected in the
> combo is the workspace default JRE.

In the "External Tools configuration" dialog, when I hit the new Button, it creates a configuration which has the "seperate JRE" is selected - I don't know when. It's completely undeterministic from my point of view.

> Whereas when you create a configuration
> from the editor or the Ant view, the JRE from the build path of the enclosing
> project will be selected in the combo: unless there is no JRE on the build
> path, then the option to use the same JRE as the workspace in selected!
> 
> I would assume that creating the configuration would work the same no matter
> where it is invoked (dialog, editor, view). 
> 
> Is this the behavior you are seeing Sven?

Yes, I think so. I should have noticed, that the project I'm trying to compile does not have a JRE or JDK in its build path. I'm sorry for not telling you. It's a project, that implemenents a small replacement for the class library of a JDK. So it contains an implementation of java.lang.Object and such things, and not having a JDK in the classpath is correct AFAIK.

Also, other projects exists in my workspace, that have this replacement for rt.jar in the classpath, instead of a full JRE/JDK. So the logic will fails for those projects too.

I'm not sure whether Eclipse can do something sensible here. Three things come to my mind: Instead of the workspace JRE, chose
1) the default JRE as configured in the "Installed JREs" preference page
2) use the execution environment (J2SE-x,y) that corresponds to the maximum of class/source version as configured on the projects "Java compiler" preference page.
3) implement a dialog that says "chose a JDK, because I can't computer the right one" instead of silently chosing the workspace JRE
Comment 9 Michael Rennie CLA 2011-01-24 09:41:25 EST
(In reply to comment #8)
> 
> I'm not sure whether Eclipse can do something sensible here. Three things come
> to my mind: Instead of the workspace JRE, chose
> 1) the default JRE as configured in the "Installed JREs" preference page

I was thinking this would be the best option. We should make the JRE selection consistent so users know that they will *always* get the 'separate JRE' option selected with an understandable choice in JRE for it, for new launch configurations.
Comment 10 Michael Rennie CLA 2011-01-31 16:24:45 EST
Created attachment 188011 [details]
proposed fix

The patch tries to resolve the JRE from the project, and failing that chooses the workspace default VM
Comment 11 Michael Rennie CLA 2011-01-31 16:40:41 EST
applied to HEAD
Comment 12 Daniel Sokolowski CLA 2015-02-09 11:46:27 EST
Can we add an option to always use "Run in same JRE as workspace", I move from project to project quiet often and the `build.xml` scripts fail because they leverage Eclipse specific variables that are ONLY available in "Run in same JRE as workspace" mode. 

So the original default to "Run in same JRE as workspace" was the correct option.
Comment 13 Daniel Sokolowski CLA 2015-02-09 11:49:25 EST
(In reply to Daniel Sokolowski from comment #12)
> Can we add an option to always use "Run in same JRE as workspace", I move
> from project to project quiet often and the `build.xml` scripts fail because
> they leverage Eclipse specific variables that are ONLY available in "Run in
> same JRE as workspace" mode. 
> 
> So the original default to "Run in same JRE as workspace" was the correct
> option.

Disregard the above - digging deeper in the settings I found: http://www.codejava.net/images/articles/ides/eclipse/antjre/Ant%20preferences.png