Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java9 JNLP --add-opens not working

I have created a simple test case to test Java 9 Web Start with the new modules. Unfortunately, Java 9 Web Start does not by default support --permit-illegal-access like regular Java 9 does.

Java 9 Web Start is suppose to support --add-opens (see https://bugs.openjdk.java.net/browse/JDK-8172986).

I'm using Java 1.9.0_181_ea.

Here is my test 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;
       }
   }
}

Here is my JNLP file which should make java.awt code able to accessible.

<?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>

This seems inline with what is discussed here (How to pass the Java 9 '--permit-illegal-access' flag to a Webstart application?). However, it does not work. Running it under Java Web Start I get the following error:

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)

It is unclear to me where my error is and why java.awt.Toolkit is not available to use via Reflection.

like image 758
Stephen M -on strike- Avatar asked Aug 30 '17 22:08

Stephen M -on strike-


1 Answers

Because Java 9 isn't confusing enough, the right answer is:

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

That equals sign matters in a JNLP file, but does not on the command line!

On the command line both this:

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

and

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

work correctly!

like image 167
Stephen M -on strike- Avatar answered Sep 18 '22 21:09

Stephen M -on strike-