Java/OpenJDK : How to add JAR to classpath

Java/OpenJDK : How to add JAR to classpath … here is a solution to the problem.

Java/OpenJDK : How to add JAR to classpath

I ran a 3rd-party jar file on the command line like this (no UI):

 /usr/bin/java -jar exchange-sync.jar

After my release (Debian Buster) was upgraded to the latest OpenJDK 11, this command no longer works. That’s what I get now:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/ws/http/HTTPException
        at microsoft.exchange.webservices.data.core.ExchangeService.findItems(ExchangeService.java:976)
        at microsoft.exchange.webservices.data.core.ExchangeService.findAppointments(ExchangeService.java:1264)
        at microsoft.exchange.webservices.data.core.ExchangeService.findAppointments(ExchangeService.java:1285)
        at com.zerodes.exchangesync.exchange.ExchangeSourceImpl.getAllAppointments(ExchangeSourceImpl.java:399)
        at com.zerodes.exchangesync.sync.SyncCalendarsImpl.generatePairs(SyncCalendarsImpl.java:45)
        at com.zerodes.exchangesync.sync.SyncCalendarsImpl.syncAll(SyncCalendarsImpl.java:98)
        at com.zerodes.exchangesync.App.main(App.java:48)
Caused by: java.lang.ClassNotFoundException: javax.xml.ws.http.HTTPException
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

I

already know that I now need to install the jaxws-api Debian package, which provides the missing classes in /usr/share/java/jaxws-api.jar.

Which java command line do I need in order to include classes from installed jar files? I’ve tried it, but I’m getting the same error :

/usr/bin/java -cp /usr/share/java/jaxws-api.jar -jar exchange-sync.jar

By the way: I’m a Java noob and I don’t want to mess around with the recompile thing. I’m also sure javax.xml.ws.http.HTTPException resides in jaxws-api.jar because I browsed that JAR file with vim and was in it

Solution

Well, I finally figured it out myself. This is how to correctly add the modules that JAR expects to exist:

/usr/bin/java --module-path /usr/share/java/jaxws-api.jar --add-modules jaxws.api -jar exchange-sync.jar

So it really has nothing to do with the classpath, it’s just about adding modules.

Related Problems and Solutions