1

I get Eclipse projects from an ever-changing variety of platforms (students) and sometimes the JRE doesn't match so there are oodles of errors in the source code: "The import java.util cannot be resolved" "Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor" etc., etc. The project is configured to use a JRE that I don't have: enter image description here So, I have to manually edit the build path in each project and reset the JRE. enter image description here

After that, the errors go away. The problem is that I will have 10 or 20 of these on a regular basis. How can I configure Eclipse to use the JRE on my machine rather than the (missing) JRE in the project?

nicomp
  • 4,344
  • 4
  • 27
  • 60
  • A `JavaSE-14` execution environment should work with any specific Java 14 JDK. Your Eclipse looks very outdated and this might be the real reason. Are there reasons for doing retrocomputing? – howlger Oct 28 '20 at 13:49
  • My Eclipse is Eclipse IDE for Enterprise Java Developers. Version: 2019-09 R (4.13.0) Build id: 20190917-1200 – nicomp Oct 29 '20 at 17:14

3 Answers3

1

Try setting it in eclipse config file -

eclipse.ini vm argument

eclipse.ini vm argument is useful when you have multiple JDK installation and you want to make sure that your eclipse runs on a specific JVM, rather than picking system configured jdk path. It must be defined before -vmargs.

eclipse.ini vm argument Mac

My eclipse.ini file snippet showing -vm argument usage to configure eclipse to use JDK8 in Mac OS X.

-vm
/Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/bin
-vmargs

You can configure it similarly for Windows or Linux operating systems. Just change the JDK bin directory path accordingly.

Mahesh_Loya
  • 2,743
  • 3
  • 16
  • 28
  • There was already a setting for the vm argument , It was set to a downlevel VM. I updated it to the VM I want but it still doesn't override the VM for projects I load into the workspace. – nicomp Oct 28 '20 at 13:25
  • The configuration might have been stored in you eclipse config folder. Can you try with new eclipse folder/workspace? This setting should be applicable for all new projects in new workspace. – Mahesh_Loya Oct 28 '20 at 13:28
  • Check .classpath, .project file that might have already have this preference set in your project folder. You can try removing them and reimporting to eclipse or try mvn eclipse : eclipse, if its maven project. – Mahesh_Loya Oct 28 '20 at 13:31
  • The `-vm` flag in `eclipse.ini` controls the JVM which is used to run Eclipse itself, does it not? The flag does not affect which JDK that is used to compile projects inside of Eclipse. So the flag is probably not relevant for this problem. – Lii Oct 30 '20 at 08:38
1

As you say in the comment, you have Eclipse 2019-09 R (4.13.0), which is currently 4 releases behind. Eclipse 2019-09 was released in September 2019, at a time when the development of Java 14 was just starting. Your Eclipse is too old for Java 14 and even too old for Java 13.

Make sure that your Eclipse is always up-to-date to avoid such and other issues in future.

howlger
  • 31,050
  • 11
  • 59
  • 99
  • 1
    I don't think the Eclipse version is relevant for this problem. The problem is that the poster are submitted Eclipse project files which are configured with JRE:s that only exist on their students' computers. – Lii Oct 30 '20 at 09:14
  • By default, the JRE is specified by an execution environment and not by a specific JRE. It will work without doing anything if you have a matching JRE/JDK in _Window > Preferences: Java > Installed JREs_ (matching means here a JRE/JDK of the same Java version or - when using the _release_ option - a JRE/JDK of the same or higher Java version). Everything you describe in your question was only caused by missing to update Eclipse. – howlger Oct 30 '20 at 09:43
  • 1
    Hm. I don't think you have understood the posters problem properly. I think that the problem is that students submit projects files to the poster. Sometimes the students have configured their projects to use a local JRE:s. Those JRE:s are not always available on the posters computer, resulting in the error in the screenshot. – Lii Oct 30 '20 at 10:21
  • Well, I think I understood it and that you have not understood why there are _execution environments_ and how they differ from JREs (nor what's the _release_ option is for). – howlger Oct 30 '20 at 10:50
  • The screenshot in the question clearly shows `JavaSE-14` (from the student). `JavaSE-14` is an execution environment, not a specific JRE. – howlger Oct 30 '20 at 10:57
0

In Eclipse you can override the workspace default JRE with the project JRE. But I'm almost certain you can't do it the other way around, which is what you would need to solve this problem in a pretty way.

But maybe you can solve the problem in the following way (which is a bit of a hack):

The project JRE specification is stored in Eclipse in the .classpath file in the project directory.

.classpath looks like this if the project has a specific JRE configured:

(Note the classpathentry kind="con" entry.)

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-14.0.2+12">
        <attributes>
            <attribute name="module" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="bin"/>
</classpath>

Without a specified JRE .classpath looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
        <attributes>
            <attribute name="module" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="bin"/>
</classpath>

If these files are updated Eclipse picks up the change to the project settings.

  • Maybe you can create a script to update the submitted files to get the classpath entry that you need?
  • Maybe you can simply copy-and-paste the .classpath file from a working project? This might save a couple of manual steps compared to changing the project configuration through the GUI.
Lii
  • 11,553
  • 8
  • 64
  • 88
  • This answer misses the fact that it was not a specific JRE, but an execution environment (`JavaSE-14`). Using a specific JRE or the Workspace default JRE is not necessary here, nor is it recommend here since it might cause other errors. The real problem here is that a too old version of Eclipse was used that does not understand the execution environment `JavaSE-14` (because `JavaSE-14` was specified later than the old Eclipse version was released that is used here). – howlger Oct 30 '20 at 11:54