Java9 JNLP --add-opens 不工作

Java9 JNLP --add-opens not working

我创建了一个简单的测试用例来测试Java 9 Web 从新模块开始。不幸的是,Java 9 Web Start 默认情况下不支持 --permit-illegal-access,就像常规 Java 9 一样。

Java 9 Web Start 假设支持--add-opens(参见https://bugs.openjdk.java.net/browse/JDK-8172986)。

我正在使用 Java 1.9.0_181_ea。

这是我的测试class:

import java.awt.Toolkit;
import java.lang.reflect.Method;
import java.lang.IllegalAccessException;
import java.lang.reflect.InvocationTargetException;

public class Java9BreaksWhenTest {

    public static void main(String args[]) throws IllegalAccessException, InvocationTargetException {
       System.out.println("Hello World");

       // Do some dangerous reflection
       Toolkit toolkit = Toolkit.getDefaultToolkit();
       Class c = toolkit.getClass();
       while(true) {
           Method m = null;
           try {
               m = c.getDeclaredMethod("setDesktopProperty", String.class, Object.class);
           } catch (NoSuchMethodException ex) {
               c = c.getSuperclass();
               if (c!=null)
                   continue;
           } catch (SecurityException ex) {
               ex.printStackTrace();
           }
           if (m!=null) {
               m.setAccessible(true);
               m.invoke(toolkit, "awt.font.desktophints", null);
               System.out.println("desktop Properties changed.");
           }
       break;
       }
   }
}

这是我的 JNLP 文件,应该可以访问 java.awt 代码。

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="file:///home/smm/hcs/testcode" href="java9BreaksWhenTest.jnlp">
   <information>
       <title>Java 9 Breaks When Test</title>
       <vendor>HCS</vendor>
       <homepage href="http://hcs.us.com"/>
       <description>Java 9 Breaks When Test</description>
       <description kind="short">Java 9 Breaks When Test</description>
   </information>
   <security><all-permissions/></security>
   <update check="timeout" policy="always"/>
   <resources>
       <j2se version="9" java-vm-args="--add-opens java.desktop/java.awt=ALL-UNNAMED" />
       <jar href="Java9BreaksWhenTest.jar" main="true" download="eager"/>
   </resources>
   <application-desc main-class="Java9BreaksWhenTest">
   </application-desc>
</jnlp>

这似乎与此处讨论的内容一致 ()。但是,它不起作用。 运行 它在 Java Web Start 下 我得到以下错误:

Hello World
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at jdk.javaws@9/com.sun.javaws.Launcher.executeApplication(Launcher.java:1741)
    at jdk.javaws@9/com.sun.javaws.Launcher.executeMainClass(Launcher.java:1677)
    at jdk.javaws@9/com.sun.javaws.Launcher.doLaunchApp(Launcher.java:1525)
    at jdk.javaws@9/com.sun.javaws.Launcher.run(Launcher.java:158)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final void java.awt.Toolkit.setDesktopProperty(java.lang.String,java.lang.Object) accessible: module java.desktop does not "opens java.awt" to unnamed module @1f749ac
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:198)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:192)
    at Java9BreaksWhenTest.main(Java9BreaksWhenTest.java:26)
    ... 9 more
#### Java Web Start Error:
#### Unable to make protected final void java.awt.Toolkit.setDesktopProperty(java.lang.String,java.lang.Object) accessible: module java.desktop does not "opens java.awt" to unnamed module @1f749ac
java.lang.reflect.InaccessibleObjectException: Unable to make protected final void java.awt.Toolkit.setDesktopProperty(java.lang.String,java.lang.Object) accessible: module java.desktop does not "opens java.awt" to unnamed module @1f749ac
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:198)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:192)
    at Java9BreaksWhenTest.main(Java9BreaksWhenTest.java:26)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at jdk.javaws@9/com.sun.javaws.Launcher.executeApplication(Launcher.java:1741)
    at jdk.javaws@9/com.sun.javaws.Launcher.executeMainClass(Launcher.java:1677)
    at jdk.javaws@9/com.sun.javaws.Launcher.doLaunchApp(Launcher.java:1525)
    at jdk.javaws@9/com.sun.javaws.Launcher.run(Launcher.java:158)
    at java.base/java.lang.Thread.run(Thread.java:844)

我不清楚我的错误在哪里以及为什么 java.awt。无法通过反射使用 Toolkit。

因为 Java 9 不够混乱,正确答案是:

<j2se version="9" java-vm-args="--add-opens=java.desktop/java.awt=ALL-UNNAMED"  />

等号在 JNLP 文件中很重要,但在命令行中却没有!

在命令行上都是这样的:

java --illegal-access=deny --add-opens java.desktop/java.awt=ALL-UNNAMED -cp . Java9BreaksWhenTest

java --illegal-access=deny --add-opens=java.desktop/java.awt=ALL-UNNAMED -cp . Java9BreaksWhenTest

工作正常!