No class def found for SOAPConnectionFactory - java

I am re-building an application from Java 8 in Java 9 (jdk 9.0.4), using modules. One of these modules requires making SOAP calls to our server to check if various services are running, and then reports back the status of those services via email.
I understand that the SOAP libraries have been deprecated in Java 9 but not removed yet. I have the java.xml.ws module declared as a requirement in the appropriate module-info:
module services {
exports com.nams.hyperion.services;
requires configurations;
requires java.xml.ws;
}
It compiles, but I get an error when I try running the project: java.lang.NoClassDefFoundError: javax/xml/soap/SOAPConnectionFactory
scf = SOAPConnectionFactory.newInstance(); <-- error here
conn = scf.createConnection();
mf = MessageFactory.newInstance();
msg = mf.createMessage();
I tried using the --add-modules java.xml.ws compiler option, but that doesn't resolve the problem; does my Java installation not have this module? What are my options? I've mucked around maven a bit looking for a separate distribution but haven't found anything yet.

I found a Maven distribution that I could use: https://search.maven.org/artifact/com.sun.xml.ws/rt-jdk9/2.3.0/jar
However, the module-info file gives me a warning that my module reads java.xml.ws from 2 identically named sources; but the project runs, which is all that matters to me at the moment. I'll work out how to deal with the ambiguity later and update this answer if I manage to do it.
Update:
I solved the ambiguity by removing the jar file from the module's dependencies, but retaining it in Maven's dependencies list.

Related

Getting NoClassDefFoundError -> com/sun/jna/platform/win32/Psapi with Eclipse

My program relies on the following code to get available system memory:
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
// Next line throws exception: NoClassDefFoundError -> com/sun/jna/platform/win32/Psapi
long availableBytes = hal.getMemory().getAvailable();
double availableMegabytes = ((double) availableBytes) / 1048576;
double availableGigabytes = ((double) availableMegabytes)/1024;
Update: After deleting every occurrence of oshi-core from every project in Workspace (to remove possibility of transient conflict dependency - only 4.2.1 is left). Now the error I get is -> java.lang.NoClassDefFoundError: com/sun/jna/platform/win32/VersionHelpers
In pom.xml I've added oshi-core dependency - I've tried almost every version starting from version 3.4.0 to latest version 4.2.1 and they all result in the same error.
I realize oshi-core relies on jna and jna-platform. In Dependency Hierarchy I see both have resolved (compiled) to version 5.5.0.
What is causing this error and how can it be solved?
Thanks!
P.S
I've seen some other threads with similar error but could not find any thread with this exact problem (missing com/sun/jna/platform/win32/Psapi)
While you've pointed out in your comments that you think the latest version of JNA is being resolved, the errors indicate that your project does not have the most recent version of jna-platform (or possibly it has multiple versions linked on the classpath). This is nearly always the case for NoClassDefFoundError and while you're troubleshooting in the right direction, evidence indicates there's an old jna-platform version in your project somewhere.
The com.sun.jna.platform.win32.VersionHelpers class is in jna-platform version 5.3.0 and newer. The GetPerformanceInfo() method required for the method call giving you the error is in the com.sun.jna.platform.win32.Psapi class is in jna-platform version 4.3.0 and newer. If your classloader can't find these classes, then you don't have the correct jars linked to your project -- or you have incorrect jars linked alongside the correct ones.
Maven resolves dependencies by level... first it does all the dependencies you list in your POM (in order), then the transitive dependencies of those projects (in order) and so on. Ensuring the most recent version of JNA is used can be enforced by either (or both) of:
Specify oshi-core dependency earlier in your list of dependencies
in your POM, specifically, before any project that depends on an
earlier version of JNA.
Explicitly specify the jna and
jna-platform versions (5.5.0) in your top-level POM.
Also, in Eclipse, be sure to go through the menus to Update Maven Project to ensure your dependencies are in sync after changes in the POM.
It's possible that your local repository is not downloading the updated jar, in which case you can purge it (or just delete any JNA artifacts, or everything, from C:\Users\<username>\.m2\repository and let it rebuild.)
Also check the classpath in Eclipse. If you have manually added dependencies (e.g., to JNA) before setting up your POM to get them from Maven, you could be using those.
If the above hints do not resolve your problem, please post the contents of the dependencies section your pom.xml file so we can provide additional advice.
Seems oshi-core relies on internal undocumented features of the Sun / Oracle JVM, and you're running on a different and/or newer JVM that doesn't have that undocumented feature anymore. That's the risk of using undocumented features.
Get a newer/other version of oshi-core that supports the version of the JVM you're using, or switch to use a JVM that oshi-core supports.

How to fix undefined MqttChannelInitializer constructor in HiveMQ Client?

I was using HiveMQ Client version 1.0.1 but I decided to update to the recently released version 1.1. I completely started from scratch and imported the project as a Gradle project and tried to build. The build work only after ignoring a few failed tests. I'm getting 3 errors in 3 different classes. I realize this is likely related to the Dagger dependency injection tool and I had already successfully built the project and added the directory of build/generated/source/apt/main/ to my build path as noted by my previous stack post where I had issues with a DaggerSingletonComponent not being found: How to fix DaggerSingletonComponent not resolved in HiveMQ (MQTT protocol) . This seems to be a new issue and I'm not sure what's wrong. I tried rebuilding by project but the errors still persist. I've left some screenshot below as well as the specific errors.
HiveMQ:
https://github.com/hivemq/hivemq-community-edition
https://github.com/hivemq/hivemq-mqtt-client
Errors:
The constructor MqttChannelInitializer(MqttClientConfig, MqttConnAckFlow, MqttEncoder, MqttConnectHandler, MqttDisconnectHandler, MqttAuthHandler, Lazy) is undefined
The constructor MqttSession(MqttClientConfig, MqttSubscriptionHandler, MqttIncomingQosHandler, MqttOutgoingQosHandler) is undefined
The method provideBootstrap(NettyEventLoopProvider, MqttChannelInitializer) in the type ConnectionModule is not applicable for the arguments (MqttClientConfig, NettyEventLoopProvider, MqttChannelInitializer)
Screenshots:
Executing ./gradlew clean build on the command line will fix your error.
But I also think that the real solution for your use case is to create a new empty project (gradle or maven) and add the client library as a dependency, like described here: https://hivemq.github.io/hivemq-mqtt-client/docs/installation.html
The issue turned out to be caused by an issue with the source folder in the directory build/generated/source/apt/main/ not having the option “Update exclusion filters in other source folders to solve nesting” selected. Selecting that option solved all of the errors.

Unable to derive module descriptor: Provider {class X} not in module

I am getting this error message when I try to compile my new modularized Java 11 application:
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\inter\.m2\repository\xalan\xalan\2.7.2\xalan-2.7.2.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.apache.bsf.BSFManager not in module
This appears to be an issue from a dependency of a dependency. I can't even find which module is pulling it in so I can update it.
I am using openjdk 11.0.2, IntelliJ 2018.3.4, Maven
Any advice how I can troubleshoot or fix this? I have found very little documentation on this issue.
Xalan
I had a look at their bug tracker following their index page and wasn't able to find this reported and not sure how actively is the library being maintained either.
General Explanation
Just to explain what has caused the issue in your code, I would share a screenshot and then try to add details around it.
So within the JAR that for version 2.7.2, there are service declarations (META-INF/services) which include org.apache.xalan.extensions.bsf.BSFManager as one of them. The service file here has to indicate the Provider thereby for itself and the class is supposed to be present on the modulepath to be resolved for reliable configuration of modules.
In this case for the module xalan(automatic module), the service listed doesn't have the provider class packaged within the dependency itself. (See the package org.apache, it doesn't further have package bsf and the class BSFManager thereby. Hence the exception as you get.
Short term hack
One of the tweaks to get that resolved would be to get update the library jar (patch it) and get rid of the service file if you're not using it. Or to add the provider copied from the corresponding artifact.
If you don't directly depend on this artifact or its parent dependencies, you can let those remain on the --classpath and get resolved as an unnamed module for your application.
Long term solve
An ideal way would be to report this to the maintainers and getting it resolved. It depends though on how actively are they maintaining it e.g. the last release for xalan was almost 5 years back, might just want to look for an actively participated alternative in my opinion.
I tried to install update for TestNG in eclipse:
"Help -> Check for updates -> deselect all and select TestNG check box. Then
install latest version i installed the version which starts with 7.2.0.
It fixed the issue for me.

Spark 2.3.1 comes with a module name, spark.core.2.11, refused by module-info.java

I converted most of the sub-projects of my application to the new Java Module system that cames with Java 9+.
Eventually, when I come to the one that uses Apache Spark, I fall into a trap. Spark modules seems to be only available with names like "spark.core.2.11" which have numbers inside and are refused my the compiler.
module fr.pays.spark {
requires fr.pays.geographie;
requires fr.pays.territoire;
requires fr.pays.fondation.objetmetier;
requires spring.beans;
requires spring.boot;
requires spring.web;
requires spark.core.2.11; // rejected for the numbers inside
}
I've found this link as a response on Stackoverflow : Unable to derive module descriptor for auto generated module names in Java 9?. And I am thankful because it may be a solution (That I have to understand, and that I haven't tried yet).
However, it seems to me really clumsy. Aren't I misleading myself ?
One year has passed since the release of Java 9, and I figure that Spark must have changed to become fully compliant to Java 9+ now.
What is proper way to reference Spark modules today (I use the 2.3.1 version, the latest I've found) ?
If there none better available than the one the link suggest, do you know have information about when Apache Spark plan to fully integrate with the Java 9+ module system ?
Thanks a lot !

Java 9 no class definition exception

So i want to try the http client
package com.company;
import jdk.incubator.http.HttpClient;
public class Main {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
}
}
And my module info looks like this
module com.company {
requires jdk.incubator.httpclient;
}
But i get java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient
And I don't really understand why. My java version is "build 9-ea+ 169" and I use the latest version of IntelliJ idea (2017.1.3). I looked into this answer and it looks like I have to just add requirement into a file, but it doesn't work for some reason.
works fine for me if I use --add-modules jdk.incubator.httpclient as the start-up parameter.
HttpClient client = HttpClient.newHttpClient();
client.executor().execute(() -> System.out.println("Here")); // prints Here
If you say that your module requires it, does not mean it will be included; at it is not included by default.
Either you or IntelliJ must have made a mistake. You are using an incubator module, about which the documentation says:
Incubator modules are part of the JDK run-time image produced by the standard JDK build. However, incubator modules are not resolved by default for applications on the class path. Applications on the class path must use the --add-modules command-line option to request that an incubator module be resolved. Applications developed as modules can specify requires or requires transitive dependences upon an incubator module directly.
I just confirmed that behavior on java-9-ea+169, i.e. I can compile and launch such a module (from the command line) without additional flags.
The fact that you do not get a compile error seems to indicate that IntelliJ correctly includes the module declaration in the compilation. The fact that you get a run-time error and that this answer helped indicates that the JVM does not see the code you launch as a module.
I ran into the same problems
java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient
with java-9-ea+173 and IntelliJ. I followed Eugenes and Nicolais advice to add jdk.incubator.httpclient explicitly to the module path via --add-modules jdk.incubator.httpclient in Run/Debug Configurations (on macOS: Menu Bar -> Run -> Edit Configurations -> Configuration Tab -> VM Options -> --add-modules jdk.incubator.httpclient
After that everything worked fine. Of course you have to add the dependency into the module-info.java like this as said before:
module network {
requires jdk.incubator.httpclient;
}
UPDATE:
With the latest IntelliJ IDEA 2017.2 EAP 172.2953.9 , I don't need to put the --add-modules to the VM Options. It just works out of the box.

Categories

Resources