ClassNotFoundException org.h2.Driver in Wildfly - java

I am trying to deploy and run my ear file in wildfly. when I try to access following code it gives ClassNotFoundException for org.h2.Driver.
String jdbcURL = "jdbc:h2:file;MODE=Derby;auto_server=true"; Class.forName("org.h2.Driver");
Connection = DriverManager.getConnection(jdbcURL, username", "password");
My module.xml
<module xmlns="urn:jboss:module:1.1" name="com.h2database.h2">
<resources>
<resource-root path="h2.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
is any I need to Change?

WildFly already ships with a module named com.h2database.h2. I don't think you need to add your own.
As for the ClassNotFoundException, make sure your deployment has a dependency on the module com.h2database.h2. There are several ways to add a dependency to your deployment the easiest probably being to add a Dependencies: com.h2database.h2 to your MANIFEST.MF. See the class loading documentation for other ways.

Related

How to add a module programmatically in Wildfly?

In Wildfly-CLI jboss-cli.shit is possible to add a module like this
module add --name=org.postgres
--resources=postgresql-42.2.5.jar
--dependencies=javax.api,javax.transaction.api
This adds the file postgresql-42.2.5.jar and creates module.xml with the following structure in /modules/org/postgres/main:
<?xml version='1.0' encoding='UTF-8'?>
<module xmlns="urn:jboss:module:1.1" name="org.postgres">
<resources>
<resource-root path="postgresql-42.5.1.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
How can this be done programmatically with ModelControllerClient (available in org.wildfly.core:wildfly-controller-client:19.0.1.Final)?
ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9990);
It cannot be done with the ModelControllerClient. The module command is a high level CLI command and not what is referred to as an operation.
You could launch a CLI process and send the command. The other option would be to copy the JAR and create the module.xml programmatically.

Wildfly 26 Jakarta EE 9.1

Background
Transforming a Wildfly 17 Java EE project (EAR) to Wildfly 26 and Jakarta EE9.1
Updated all dependencies and changed imports. Also some code changes necessary.
Now i am stuck on resteasy-core-spi-6.0.0.Beta1
When deploying i get this error:
Caused by: java.lang.IncompatibleClassChangeError: Expected static method 'java.lang.Object org.jboss.resteasy.spi.ResteasyProviderFactory.getContextData(java.lang.Class)'
The Code that calls this method looks like this.
FooClass foo = ResteasyProviderFactory.getContextData(FooClass .class);
A look at the SPI Class reveals this.
public static <T> T getContextData(Class<T> type) {
return getContextDataMap().get(type);
}
That looks perfectly fine.
Examining the Wildfly system dir and looking in the module for same jar.(resteasy-core-spi-6.0.0.Beta1,jar)
reveals this
<resources>
<resource-root path="resteasy-core-spi-6.0.0.Beta1.jar"/>
</resources>
<dependencies>
<module name="java.desktop"/>
<module name="java.logging"/>
<module name="java.management"/>
<module name="java.naming"/>
<module name="java.xml"/>
<module name="javax.activation.api"/>
<module name="javax.annotation.api"/>
<module name="javax.enterprise.api"/>
<module name="javax.servlet.api"/>
<module name="javax.validation.api"/>
<module name="javax.xml.bind.api"/>
<module name="javax.ws.rs.api"/>
<module name="org.jboss.logging"/>
<module name="org.reactivestreams"/>
</dependencies>
I tried to update that to jakarta namespace but no change.
I am at a loss here, do anyone have some ideas?

java.lang.NoClassDefFoundError: javax/net/ssl/HostnameVerifier

I am trying to execute rest api using HTTP client in java
I am using ant project so using below dependencies in module.xml
<module xmlns="urn:jboss:module:1.3" name="kv.http-client">
<resources>
<resource-root path="httpcore-4.4.14.jar"/>
<resource-root path="httpclient-4.5.13.jar"/>
</resources>
</module>
While executing the code I am getting below error. Please let me know what is wrong with this.
java.lang.NoClassDefFoundError: javax/net/ssl/HostnameVerifier
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:56)
As this thread from JBoss community suggests, you should add a dependency to the javax.api package:
You should be able to fix this by adding
<module name="javax.api"/>
to the dependecies section of the module.xml for the module
org.apache.commons.httpclient

Use WildFly module both datasource and connectionFactory

I have a module in Wildfly to use as my datasource to Firebird. It works great
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.firebirdsql">
<resources>
<resource-root path="jaybird-2.2.13.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.resource.api"/>
<module name="javax.xml.bind.api"/> <!-- Jaybird 3.0 onwards doesn't need this -->
<module name="org.antlr4"/>
</dependencies>
</module>
and the jar is put inside the directory of module.xml.
But I want to use the driver both with data source and also to create a pure JDBC connection in connectionfactory like Class.forName("org.firebirdsql.jdbc.FBDriver");, not using data source provided by the server.
If I put jaybird in pom.xml I got errors. I think because this is duplicating the libs. How can I solve this?
I just added the below code under <subsystem xmlns="urn:jboss:domain:ee:4.0">
<global-modules>
<module name="org.firebirdsql" slot="main"/>
</global-modules>
Thanks, Mark Rotteveel for answer on the Jaybird bug tracker.

"Failed to load module" error for Firebird datasource on WildFly 8.1.0.Final

The server startup error message is:
16:08:37,829 ERROR [org.jboss.as.controller.management-operation] (ServerService
Thread Pool -- 27) JBAS014613: Operation ("add") failed - address: ([
("subsystem" => "datasources"),
("jdbc-driver" => "firebird")
]) - failure description: "JBAS010441: Failed to load module for driver [org.fir
ebirdsql]"
Content of module.xml:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="org.firebirdsql">
<resources>
<resource-root path="jaybird-2.2.5.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.resource"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
Driver definition in standalone.xml:
<driver name="firebird" module="org.firebirdsql">
<driver-class>org.firebirdsql.jdbc.FBDriver</driver-class>
</driver>
(Based on http://masterjboss.blogspot.de/2014/03/how-to-configure-mysql-jdbc-driver-in.html)
A similar problem (without accepted answer): Db2 Driver/Datasource setup on wildfly: Failed to load module for driver [com.ibm]
Replace <module name="javax.resource"/> with <module name="javax.resource.api"/> in the dependencies section.
I have installed WildFly 8.1 and added the module under:
<wildfly-root>\modules\org\firebirdsql\main\
module.xml
jaybird-2.2.5.jar
Note that this doesn't match the location used in the tutorial you link to. The tutorial - incorrectly - roots user modules in modules\system\layers\base instead of modules\, but when I place the module there it works as well.
My module.xml definition has content:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.firebirdsql">
<resources>
<resource-root path="jaybird-2.2.5.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.resource.api"/>
</dependencies>
</module>
I added the driver entry to standalone.xml exactly as you posted. And then added a datasource in the management console, and tested the connection.
This works. My earlier theory in the comments that it isn't working for you due to the Web Profile not including Resource Connectors seems to be wrong. I have also tested using a Java 8 version of Jaybird when WildFly is running on Java 7, but this gives an UnsupportedClassVersionError as expected.
The only way I have been able get the error in your question is to intentionally misplace the module (eg deleting it entirely, having a spelling error in the folder names, or putting it in the wrong location). I would suggest that you carefully check your module location (see above).
See the answer by asohun for the solution to your specific problem. I will leave this answer in place as it contains the correct config and an alternative failure mode that produces the same error.

Categories

Resources