4

I have three applications I deploy with Java Web Start to clients. All three of these applications use JAXB. Under Java 1.9 to get the jaxb module loaded you have to use:

--add-modules java.xml.bind

Java Web Start let's you pass VM arguments to applications with the java-vm-args attribute of the java/j2se tag. However, only the arguments listed in the documentation are supported and --add-modules is not in that list.

So the question is how do you pass "--add-modules java.xml.bind" to the 1.9 VM when running code via Java Web Start that uses JAXB?

Here is what I tried and my testing shows that --add-modules is indeed not supported by the java-vm-arg attribute:

<resources>
    <property name="jnlp.packEnabled" value="true"/>
    <java version="9" java-vm-args="--add-modules java.xml.bind"/>
    <java version="1.8+"/>
    <jar href="redacted.jar"/>
</resources>
Michael
  • 2,683
  • 28
  • 30

3 Answers3

2

Per the Modules Shared with Java EE Not Resolved by Default section of the Java 9 migration doc --add-modules is a workaround because JAXB is being removed from the JDK in the future. So to solve this I just included the JAXB API and an implementation on my classpath, using ANT+Ivy this configuration got me what I needed:

 <dependency org="org.glassfish.jaxb" name="jaxb-runtime" rev="2.3.0"/>
 <dependency org="org.glassfish.jaxb" name="jaxb-core" rev="2.3.0"/>
 <dependency org="javax.xml.bind" name="jaxb-api" rev="2.3.0"/>
 <dependency org="javax.activation" name="activation" rev="1.1.1"/> 

A few transitive dependencies came along with it (FastInfoset, istack-commons, stax-ex, and txw2). These are desktop swing applications so I like keeping the jar file as small as feasible, after pack200 compression it only added about 600K to my fat jar.

The glassfish implementation does result in an illegal reflective access warning from Java 9. I assume an update will come out soon that doesn't use illegal reflection.

Michael
  • 2,683
  • 28
  • 30
1

Use an equals rather than a space. Also the module name is "java.xml.bind". This gives you java-vm-args="--add-modules=java.xml.bind".

Alan Bateman
  • 5,283
  • 1
  • 20
  • 25
  • That is incorrect syntax for the --add-modules argument. It does not take a equals sign: --add-modules [,...]. However, you are indeed correct about it being "java" and not "javax". I am fixing and testing (had it correct when testing from command-line, but typed it wrong when editing the JNLP). – Michael Sep 25 '17 at 17:57
  • Changing it to java.xml.bind still results in a "Thread-9" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException when my app starts up. This appears to be because --add-modules is not supported by the java-vm-args attribute of the JNLP's "java" tag. – Michael Sep 25 '17 at 18:02
  • JEP 293 (http://openjdk.java.net/jeps/293) has the guidelines for CLI options. For the `java` and `javac` launches then you can use space or equals as the separator, for APIs and other usages then only equals can be used. – Alan Bateman Sep 25 '17 at 18:10
  • I didn't discount the idea of using an equals sign out-of-hand. I did test it with that syntax and the VM wouldn't even fire up. – Michael Sep 25 '17 at 18:12
0

Referring the same documentation, you should change to using version equal to 9 or 9+ (ea builds) as:

<resources> 
    <property name="jnlp.packEnabled" value="true"/>
    <j2se version="9" java-vm-args="--add-modules java.xml.bind"/>
    <j2se version="1.8+"/>
    <jar href="redacted.jar"/>
</resources>

The version attribute refers, by default, to a platform version (specification version) of the Java Platform Standard Edition. Currently defined platform versions are 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, and 9. (A platform version does not usually contain a micro version number; for example 1.4.2.)


I've also made this and that answer previously to explain on a permanent solution instead of making such temporary fixes to the code relying on java.xml.bind which is deprecated.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Can you share the link you found that in? A full text search of the Java 9 JNLP documentation doesn't find that verbiage. – Michael Sep 25 '17 at 18:15
  • 1
    That documentation is for the ANT tasks that correspond to the "javapackager" command-line tool that is part of the JDK that lets you build native executables. It does not apply to Java Web Start. – Michael Sep 25 '17 at 18:23
  • @Michael My bad, but probably changing the version should work here – Naman Sep 25 '17 at 18:45
  • I had also noticed that the correct version for 1.9 was 9 in the version attribute and already changed that, it had no effect. – Michael Sep 25 '17 at 19:42
  • @Michael Assuming that you are using 9(for GA build) as the version, did you try the argument with space[`java-vm-args="--add-modules java.xml.bind"`] as well as using =[`java-vm-args="--add-modules=java.xml.bind"`] both? What's the error you get? – Naman Sep 25 '17 at 20:31
  • For the `"--add-modules java.xml.bind"` I get NoClassDefFound exception for java.xml.bind (--add-modules isn't listed as a supported VM parameter for java-vm-args in JNLP doc). For the syntax with an equals sign: Via command-line the equals syntax results in VM not starting with the error: `Unrecognized option: --add-modules=java.xml.bind` (it doesn't like equals in there), via web start the VM just refuses to start (no java console appears). Probably because of the illegal syntax with equals. – Michael Sep 25 '17 at 21:00
  • @Michael Could you add logs to the question as well please. – Naman Sep 25 '17 at 21:02
  • The Java 9 migration guide says `--add modules` is just a workaround anyway because JAXB is being removed from the JDK in the future and recommends to include a JAXB implementation on the classpath. So basically back to like it was pre Java 1.6. https://docs.oracle.com/javase/9/migrate/toc.htm#JSMIG-GUID-F640FA9D-FB66-4D85-AD2B-D931174C09A3 – Michael Sep 25 '17 at 21:05
  • @Michael Thats true. A long-term and cleaner way is to get rid of the module overall, I've also made [this](https://stackoverflow.com/a/46221811/1746118) and [that](https://stackoverflow.com/a/46086920/1746118) answer previously to describe how could this be done as well. But eventually nothing should deny the fact that the argument shall work as wel if required. – Naman Sep 25 '17 at 21:15
  • The `--add-modules` parameter does work from the command-line (or my IDE). However, just not via web start, the JNLP doc says not all VM parameters are supported with the java-vm-args attribute and it lists the ones that are, `--add-modules` is not among them. – Michael Sep 25 '17 at 21:28