Home > database >  Java code not recognized, Eclipse error: "Target is not a JDK Root. Java executable was not fou
Java code not recognized, Eclipse error: "Target is not a JDK Root. Java executable was not fou

Time:09-27

Problem: Eclipse is not recognizing my Java code on my Mac (e.g., the main method, String, Object class... none of that is recognized; the pre-built Java classes aren't even there!).

Detail: I have the latest Eclipse (2022-09) and even tried 2022-06. I also have the latest Java version (terminal: "java -version" brings 17.0.4.1). In Eclipse settings, I see the following (see PINK for how I got to these settings, see YELLOW for problem): enter image description hereenter image description here

But I have the following in Finder:

enter image description here

So maybe the JavaSE/SDK file did not get imported auotmatically. So then I manually try to do that (see PINK), but when I select any of the JavaSE/JDK files from Finder, this happens (YELLOW):

enter image description here

Is there any resolution? Thanks!

Note: Java 17 and Eclipse are both mandatory for my college class. In fact, for the current project, only Java 17 is compatible with the .jars file we need to work with, so there's no other choice.

CodePudding user response:

The correct path for Java on macOS is the Contents/Home folder in the xxx.jdk

So in the "Installed JREs" you should have something like:

/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home

or

/Library/Java/JavaVirtualMachines/jdk-18.0.2.jdk/Contents/Home

As mentioned in the comments running the command

/usr/libexec/java_home -V

will list the available Java installs and the correct paths.

CodePudding user response:

I just found the solution! Don't import the entire JDK package. Instead, import the Home folder that is located two folders inside it. See below for details.

First off, there was nothing wrong with the JDK packages (folders) or where they were located (they were correctly located in /Library/Java/JavaVirtualMachines). See below. enter image description here

The problem was that in Eclipse, when we tried importing a Java library (e.g., for Java 17) through Project > Properties > Java Build Path > Modulepath: Add Library... > JRE System Library > Next > Installed JREs... > Add > Standard VM > Next > Directory... (this opens up Finder), we, at this point, will choose one of the packages (e.g., jdk-17.0.4.1.jdk). (See below.) DON'T. Instead, double-click into it. Then you'll see a folder called "Contents." Double-click into that as well. Now, you'll see a couple of folders. Select "Home" and hit "Open" for that one. Eclipse will open it and throw no error. If instead you selected the entire package and opened that, then Eclipse will throw a "Target is not a JDK Root. Java executable was not found" error. It seems that Eclipse was not automatically detecting the Home folder that is embedded inside, so we had to manually go further and select that specifically for Eclipse.

enter image description here enter image description hereenter image description here

Note: I already imported Java 17 before posting this. I went back to import Java 16 using above steps, that's why I'm calling this one Java 16.0.1

CodePudding user response:

  1. Run below commend on your terminal to see where your java is located on your mac

    /usr/libexec/java_home

  2. Use that path on your eclipse JRE location

In your case you need JDK for eclipse to work or recognize your project so add JDK by following below

  • Related