How to tell eclipse to add-exports when compiling
Asked Answered
S

2

12

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.

Stipitate answered 7/1, 2019 at 5:35 Comment(0)
C
25
  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

Circumambient answered 7/1, 2019 at 9:18 Comment(10)
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. – Penuchle
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. πŸ€¦β€β™‚οΈ – Penuchle
@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. – Circumambient
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. – Penuchle
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. – Penuchle
@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. – Circumambient
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: <unnamed>. If I change this via the .classpath file, then Eclipse see it the same way as a JDK 8, and it works. imgur.com/a/ZVA88Cg – Penuchle
@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://mcmap.net/q/94565/-eclipse-can-39-t-find-xml-related-classes-after-switching-build-path-to-jdk-10 – Circumambient
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. – Penuchle
@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. – Circumambient
Y
-2

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

Yi answered 7/1, 2019 at 5:43 Comment(5)
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. – Vaulting
You can use the same jvm option along with 'javac' – Yi
These settings in the launch configuration are used to run/debug a Java application and not used for compiling (Eclipse has its own compiler). – Circumambient
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 : #9483815 – Yi
@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? – Stipitate

© 2022 - 2024 β€” McMap. All rights reserved.