2

So I have downloaded JDK 15 - OpenJDK .

Running in Intelij the following code

import jdk.incubator.foreign.MemorySegment;  //The problem seems to occur here in this import

public class Application {

    public static void main(String[] args){

      MemorySegment m = MemorySegment.allocateNative(400L);

   }

 }

In inteliJ I have gone to File -> Project Structure -> Project SDK -> selected 15

In Application configurations (Intelij run project) I have declared JRE 15 (java version 15.0.2)

I receive the following error

C:\Users\repositories\java15project\src\main\untitled\src\Application.java:2:21
java: package jdk.incubator.foreign is not visible
(package jdk.incubator.foreign is declared in module jdk.incubator.foreign, which is not in the module graph)

Could someone please help me?

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47

1 Answers1

9

Run with option --add-modules jdk.incubator.foreign

Alternatively, create a module-info.java file, e.g. like this:

module my.module.name.here {
    requires jdk.incubator.foreign;
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 2
    the second option worked like charm. The first though didn't. – Panagiotis Bougioukos Jan 23 '21 at 17:18
  • @PanagiotisBougioukos `java --add-modules jdk.incubator.foreign my.package.Application` didn't work? It did for me. Remember to put VM options *before* the class name. – Andreas Jan 23 '21 at 22:39
  • @Boug you need to add that under `VM options` – elect Jun 02 '21 at 10:48
  • 4
    I found in my IntelliJ that, I not only needed to add --add-modules jdk.incubator.foreign into my VM options under Run, but also needed to add --add-modules jdk.incubator.foreign under Preferences -> Build, Execution, Development -> Compiler -> Java Compiler -> Additional command line parameters – Bob Jul 23 '21 at 13:19
  • On Ubuntu 20.04 with Intellij 2021.2.3, none of the option work, neither the VM options (which work on MacOS+IntelliJ), neither adding --add-module to compiler options, neither adding module.info : all of them separately or all together lead to the same originally mentioned error message. Suggestions appreciated :) – Martin Pernollet Nov 13 '21 at 16:27
  • I fixed by also forcing the target bytecode version to be the one of the VM I am running on. – Martin Pernollet Nov 21 '21 at 19:42