0

My code contains import sun.misc.URLClassPath and works normally on Java 8.

I am now moving to Java 11 and the code does not work anymore. It is caused by the sun.mics package was moved into JDK internal package.

Then, I use --add-exports option added to the Java compiler (javac) exports a package to my module to continue using it (for a short time while waiting for the official fixing by my team).

Here is the pom info

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <encoding>UTF-8</encoding>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
                <compilerArgs>
                    <arg>--add-exports=java.base/jdk.internal.misc=com.myapp.af.util</arg>
                    <arg>--add-opens=java.base/jdk.internal.misc=com.myapp.af.util</arg>
                </compilerArgs>
            </configuration>
        </plugin>            
    </plugins>
</build>

When I execute mvn compile I've got:

[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/apps/src/main/java/com/myapp/af/util/AgentClassLoaderUtil.java:[6,16] cannot find symbol
  symbol:   class URLClassPath
  location: package sun.misc
[ERROR] /D:/apps/src/main/java/com/myapp/af/util/AgentClassLoaderUtil.java:[35,17] cannot find symbol
  symbol:   class URLClassPath
  location: class com.myapp.af.util.AgentClassLoaderUtil
[ERROR] /D:/apps/src/main/java/com/myapp/af/util/AgentClassLoaderUtil.java:[35,37] cannot find symbol
  symbol:   class URLClassPath
  location: class com.myapp.af.util.AgentClassLoaderUtil
[INFO] 3 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.091 s
[INFO] Finished at: 2023-06-15T16:27:46+07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project apps: Compilation failure: Compilation failure:
[ERROR] /D:/apps/src/main/java/com/myapp/af/util/AgentClassLoaderUtil.java:[6,16] cannot find symbol
[ERROR]   symbol:   class URLClassPath
[ERROR]   location: package sun.misc
[ERROR] /D:/apps/src/main/java/com/myapp/af/util/AgentClassLoaderUtil.java:[35,17] cannot find symbol
[ERROR]   symbol:   class URLClassPath
[ERROR]   location: class com.myapp.af.util.AgentClassLoaderUtil
[ERROR] /D:/apps/src/main/java/com/myapp/af/util/AgentClassLoaderUtil.java:[35,37] cannot find symbol
[ERROR]   symbol:   class URLClassPath
[ERROR]   location: class com.myapp.af.util.AgentClassLoaderUtil
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Am I missing something?

NoName
  • 877
  • 12
  • 28
  • Change your code accordingly... – khmarbaise Jun 15 '23 at 10:56
  • You should avoid using internal implementation classes because there is no guarantee they will break between version. If you really want to assume the risks see: [Why is the Java 11 runtime ignoring my jar containing sun.misc classes?](https://stackoverflow.com/questions/56385547/why-is-the-java-11-runtime-ignoring-my-jar-containing-sun-misc-classes) – aled Jun 15 '23 at 11:26
  • 1
    There is no point in adding options to open `jdk.internal.misc` when your code keeps trying to access `sun.misc`. But the class `URLClassPath` is in neither of the two packages and to prevent future disappointments, whatever you are intending to do with that class is likely to fail anyway. For example, the system class loader is not a `URLClassLoader` anymore. Recommended read: “[What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378)” – Holger Jun 15 '23 at 15:59

0 Answers0