gpt4 book ai didi

Java9 JNLP --add-opens 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:17:02 27 4
gpt4 key购买 nike

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

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

我使用的是 Java 1.9.0_181_ea。

这是我的测试类:

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>

这似乎与此处讨论的内容一致 (How to pass the Java 9 '--permit-illegal-access' flag to a Webstart application?)。但是,它不起作用。在 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

正常工作!

关于Java9 JNLP --add-opens 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45970742/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com