Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly set up Java 9 as SDK in Intellij IDEA on Linux due to new folder structure

Before you write me off, please consider that neither of these are answers to my question:

  • How to setup SDK in IntelliJ IDEA?
  • How do I change the IntelliJ IDEA default JDK?

In IntelliJ IDEA 2017.2.6, attempting to add JDK9 as an SDK passes but does not work as the classpaths end up empty. Steps to reproduce:

  1. Open 'Add new SDK dialog'
    (go to Project Settings > Project > Project SDK > New > JDK)
  2. Select JDK 9
    select jdk dialog
  3. Passes, but if you look under SDK > JDK9 classpaths are empty and your code errors out due to base classes not being found. See image:
    no classpaths

In IntelliJ IDEA 2017.3 EAP you get an error about JDK classes not being found. See images below:

  1. Open 'Add new SDK dialog'
    (go to Project Settings > Project > Project SDK > New > JDK)
  2. Select JDK 9
    select jdk dialog
  3. Should pass, but produces error popup
    jdk error popup

Command line compilation of HelloWorld example with jdk9 works as expected.

EDIT: Found an almost-duplicate: Intellij IDEA 2017.2 can't add openjk 9 on Linux Mint 18. Key differences:

  • Linux version: they're using Mint 18, and I'm using Debian Stretch.
  • OpenJDK is the latest from the repo at the moment of writing: 9~b181-4~bpo9+1. Even though is the same version, it still does not work in my case.

EDIT: Another possible duplicate: intellij idea does not see java 9 standard classes

I did not understand the answer from the comments though. Tried setting different names for JDK (9 and 1.9) but it still did not show modules instead of classpaths and classpaths remained empty.

like image 462
anon2234 Avatar asked Nov 14 '17 22:11

anon2234


People also ask

How do I change folder structure in IntelliJ?

From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and click Modules. Select the necessary module and open the Sources tab. to the right of the necessary folder (folder path). Specify the path relative to the output folder root, and click OK.

How do I create a folder path in IntelliJ?

Go to the Project Structure, click on Modules, and click on your Module. Choose the "Dependencies" tab. Click the "+" button on the right-hand side and select "Jars or directories..." Add the directory(ies) you want (note you can multi-select) and click OK.


2 Answers

Current debian binary package openjdk-9-jre-headless 9~b181-4 contains incorrectly compiled lib/jrt-fs.jar file.
There are 2 filed issues separately on both idea youtrack and also ubuntu launchpad.

As it is indicated here:

Probable reason:
Classes in lib/jrt-fs.jar were compiled by Java 9 with options "-source 8"/"target 8". They should be compiled with "--release 8" option instead (or by Java 8)

A temporary workaround may be replacing /usr/lib/jvm/java-9-openjdk-amd64/lib/jrt-fs.jar with the one from Oracle JDK.
You may also try to recompile the openjdk-9 source using the advised option "--release 8".

Anyway I advice to vote up this issue on the above link to attract more attention by dev team.

like image 59
Kayvan Tehrani Avatar answered Sep 23 '22 16:09

Kayvan Tehrani


Use Oracle Java instead of OpenJDK for now. You can pull that in through WebUpd8's repository.

sudo add-apt-repository ppa:webupd8team/java && sudo apt-get update && sudo apt-get install oracle-java9-installer 

It doesn't look like this is going to work with OpenJDK, so if you want to play with the latest and greatest Java, this is going to be the way to do it for now. Otherwise, from what I've seen, you've done this correctly and once this gets actually fixed, it will work just fine.

like image 32
Makoto Avatar answered Sep 22 '22 16:09

Makoto