nicomp
nicomp

Reputation: 4647

How can the default JRE be overidden when a project is loaded?

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?

Upvotes: 1

Views: 600

Answers (3)

Lii
Lii

Reputation: 12112

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.

Upvotes: 0

howlger
howlger

Reputation: 34135

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.

Upvotes: 1

Mahesh_Loya
Mahesh_Loya

Reputation: 2832

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.

Upvotes: 1

Related Questions