11

Is it possible to tell eclipse to add the following command line option: --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED when compiling.

I think it may also be needed when running tests.

Is it also possible to remove this message: enter image description here

Note that I tried to add those to the VM options of one of my unit tests but that did not work.

Luke
  • 884
  • 8
  • 21

2 Answers2

23
  1. Go to Project > Properties: Java Build Path, tab Libraries
  2. Select the JRE > Is modular node and click Edit...
  3. Go to the tab Details
  4. In the Added exports section click Add...
  5. Enter the following:
    • Source module: jdk.compiler
    • Package: com.sun.tools.javac.tree

enter image description here

howlger
  • 31,050
  • 11
  • 59
  • 99
  • Note, that in order to be able to edit the ***Is modular*** node, a modular JDK is needed, so first change the JDK, apply and close then re-open the build path properties, otherwise Eclipse can't edit this. Also the configuration has changed it is now available in a specific tab named ***Modules Dependencies***. – bric3 Jan 14 '22 at 09:30
  • Moreover Eclipse require the JDK to appear on the ModulePath, without that you cannot configure `--add-export` like you would do on the command line. ‍♂️ – bric3 Jan 14 '22 at 10:10
  • @Brice I didn't get your point. What you mean by JDK? When using Java 9 or higher, the system library is always on the modulepath. The system library cannot be moved to the classpath, neither by Eclipse nor via a command line argument. – howlger Jan 14 '22 at 10:43
  • Actually it can it works in both modes classpath and module path, take this class `public class Main { public static void main(String[] args) { System.out.println(sun.jvmstat.monitor.HostIdentifier.class); }}`, you can export a package from a module to the _classpath_ `javac --add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED Main.java`, I didn't declare any module whatsoever. – bric3 Jan 14 '22 at 15:37
  • The so called _unnamed module_ is equivalent to the _classpath_, one can pass `javac` compiler option to extend the classpath (unnamed module). And Eclipse don;t allow that from the UI, Java 11 is always added as a Module. – bric3 Jan 14 '22 at 15:48
  • @Brice `--add-exports jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED` can be configured in Eclipse via the UI (please note that my answer is older than the _Module Dependencies_ tab has been introduced in the _Java Build Path_ dialog which makes this even easier than described in my answer). An `--add-exports` does not move a package to the classpath or extend the classpath, but drills a hole into the encapsulation of the module, so it can be accessed from the classpath. – howlger Jan 14 '22 at 20:22
  • Indeed the UI has changed but the issue is still the same, if I add the JavaSE-11 via the UI it is added as a module path, and this the only way to _tweak_ the module (via _Expose Package_). But right after the Eclipse compiler report this error `The package com.sun.tools.attach is accessible from more than one module: `. If I change this via the `.classpath` file, then Eclipse see it the same way as a JDK 8, and it works. https://imgur.com/a/ZVA88Cg – bric3 Jan 16 '22 at 16:27
  • 1
    @Brice Yes, this "works". But according to the Java languages specification, the system library of Java 9 and higher must be treated as on the modulepath. Since Java 9, it's illegal to have the same package on the classpath and in a module of the system library. That it is working is a confirmed bug of `javac` and there is no guarantee that an (upcoming) Java VM will not crash when ignoring this error, see https://stackoverflow.com/a/53824670/6505250 – howlger Jan 17 '22 at 07:54
  • Yes I agree that's why I'd like to be able to define the export directives. Just as I am able to in the command line / maven or Gradle. And at this time with Eclipse 4.22.0 I didn't find a way to get a working setup from within Eclipse. I find the Eclipse UI getting in the way in this case. – bric3 Jan 17 '22 at 08:00
  • @Brice Well, it's illegal according to the Java language specification. `javac` (which is used by default by Maven, Gradle, etc.) should give you the same error, but it does not. `javac` does not even show a warning. Eclipse sticks to the JLS and not to some (buggy) implementations. I would like if two modules could contain the same package. But unfortunately this is not how it is specified in Java 9 and higher. So the JLS, which Eclipse is sticking to, is getting in the way here. – howlger Jan 17 '22 at 08:36
-2

You can add the --add-exports jdk.compiler/.... options on VM Arguments as shown below. enter image description here

Ramesh Subramanian
  • 944
  • 1
  • 12
  • 28
  • 1
    Wouldn't these be `java` arguments? Or are these used for `javac` as well? Just wanted to understand, I am not an Eclipse user though. – Naman Jan 07 '19 at 05:54
  • 1
    You can use the same jvm option along with 'javac' – Ramesh Subramanian Jan 07 '19 at 05:58
  • These settings in the launch configuration are used to run/debug a Java application and not used for compiling (Eclipse has its own compiler). – howlger Jan 07 '19 at 08:44
  • Hi howlger, configuration used for run/debug can also be used during compile when Window > Preferences > Java > Compiler > Classfile Generation. (various check boxes) are selected . Refer : https://stackoverflow.com/questions/9483315/where-do-you-configure-eclipse-java-compiler-javac-flags – Ramesh Subramanian Jan 07 '19 at 09:06
  • @RameshSubramanian I don't think that works because that would be the arguments to running the main and not for for compiling. Yes you can use the same JVM option for javac, but how can we tell eclipse to do that? Also how do we stop eclipse from giving errors messages? – Luke Jan 07 '19 at 09:18