Incorrect classpath causing java.lang.NoClassDefFoundError - java

When I run my java application from the command line I get the following exception:
java -jar target/my-app.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/my_company/utilities/http_server/HttpServer
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.my_company.utilities.http_server.HttpServer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Looking into this I have discovered the following:
My local repo contains:
~/.m2/repository/com/my_company/utilities/http_server/0.0.1-SNAPSHOT/utilities-0.0.1-20151208.162219-2.jar
MANIFEST.MF contains the following entry in the classpath:
Class-Path: lib/utilities-0.0.1-20151208.162219-2.jar
and the target directory contains a lib subfolder with the following file:
utilities-0.0.1-SNAPSHOT.jar
My POM file contains the following entry:
<dependency>
<groupId>com.my_company.utilities</groupId>
<artifactId>utilities</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
I can see that the exception is caused by the wrong entry in the classpath. But I don't know why that is wrong - i.e. why are the names different. Can someone help?
The exception does not occur if I run the application from within Eclipse.

Try disabling the <useUniqueVersions> configuration element:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
From Handling Snapshot Versions

Related

Dataflow and Bigtable dependency conflict

The error I'm getting:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/beam/sdk/transforms/DoFn
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.beam.sdk.transforms.DoFn
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
My pom dependencies:
<dependencies>
<dependency>
<groupId>com.google.cloud.bigtable</groupId>
<artifactId>bigtable-hbase-beam</artifactId>
<version>1.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-runners-google-cloud-dataflow-java</artifactId>
<version>2.13.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
I assumed that the issue was coming from the two dependencies using different versions of the beam sdk, so I changed the beam-runners-google-cloud-dataflow-java version to 2.11.0, which uses the same beam sdk version. But I got the same error.
Any help would be greatly appreciated!
How are you running the pipeline? I'm guessing that the issue is that the beam jars are not on your classpath. You either need to use the maven exec plugin to invoke your main() with all of the maven dependencies on the classpath or you need to create a fat jar using something like the maven shade plugin or the maven assembly plugin. Please see here for example of how to use the maven exec plugin to run a pipeline:
https://github.com/GoogleCloudPlatform/cloud-bigtable-examples/blob/a017a237fa8dae764451a658150190743c1b4127/java/dataflow-connector-examples/pom.xml#L146-L161

java Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

I am able to compile my code and run it in my local (windows) machine using Eclipse. However, when I do it on my remote (ubuntu) machine, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
at com.tools.App.main(App.java:36)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
I have added the following entry in my pom.xml file:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Can someone please help me understand what else I need to do to get this working? Thanks.
that means the jar is available at compile time but when you are running your application this jar isn't present in classpath and so the error
Try to look into the build path of your project and see if commons-io artifact is present there in the build path.

Why am I getting a can't load class, when the class is in my JAR file?

I'm running JBoss 4.2.3.GA (can't change this version) and Java 6. I'm tryhing to deploy a JAR file. Within it, I'm attempting to connect to a web service, but am getting the error, "Cannot load java type: org.mainco.bsorg.BusinessServiceFaultException," even though the class in question is within the JAR file I'm deploying to the server. I'm thinking there is some kind of class loader issue, but don't know where to start looking. The Java code I use to connect is
final OrganizationService service = new OrganizationService(wsdl, qname);
service.getOrganizationServicePort()
I'm generating my web service client code using the Maven plugin below ...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlUrls>
<wsdlUrl>${wsdl.url}</wsdlUrl>
</wsdlUrls>
<sourceDestDir>${basedir}/src/main/java</sourceDestDir>
<packageName>org.mainco.bsorg</packageName>
</configuration>
</execution>
</executions>
</plugin>
and I have these dependencies ...
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc-api</artifactId>
<version>1.1</version>
</dependency>
Below is the exception. Any ideas where I need to start digging?
2012-08-13 12:45:02,522 ERROR [org.quartz.core.ErrorLogger] Job (default.job.0.1344875788431 threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: javax.ejb.EJBTransactionRolledbackException: Cannot load java type: org.mainco.bsorg.BusinessServiceFaultException]
at org.quartz.core.JobRunShell.run(JobRunShell.java:214)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
* Nested Exception (Underlying Cause) ---------------
javax.ejb.EJBTransactionRolledbackException: Cannot load java type: org.mainco.bsorg.BusinessServiceFaultException
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:87)
at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:195)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.mdb.MessagingContainer.localInvoke(MessagingContainer.java:249)
at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.delivery(MessageInflowLocalProxy.java:268)
at org.jboss.ejb3.mdb.inflow.MessageInflowLocalProxy.invoke(MessageInflowLocalProxy.java:138)
at $Proxy80.execute(Unknown Source)
at org.jboss.resource.adapter.quartz.inflow.QuartzJob.execute(QuartzJob.java:57)
at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
Caused by: org.jboss.ws.WSException: Cannot load java type: org.mainco.bsorg.BusinessServiceFaultException
at org.jboss.ws.metadata.umdm.FaultMetaData.getJavaType(FaultMetaData.java:151)
at org.jboss.ws.core.jaxws.DynamicWrapperGenerator.generate(DynamicWrapperGenerator.java:139)
at org.jboss.ws.metadata.builder.jaxws.JAXWSMetaDataBuilder.addFault(JAXWSMetaDataBuilder.java:324)
at org.jboss.ws.metadata.builder.jaxws.JAXWSMetaDataBuilder.processWebMethod(JAXWSMetaDataBuilder.java:797)
at org.jboss.ws.metadata.builder.jaxws.JAXWSMetaDataBuilder.processWebMethods(JAXWSMetaDataBuilder.java:891)
at org.jboss.ws.metadata.builder.jaxws.JAXWSClientMetaDataBuilder.rebuildEndpointMetaData(JAXWSClientMetaDataBuilder.java:300)
at org.jboss.ws.core.jaxws.spi.ServiceDelegateImpl.getPortInternal(ServiceDelegateImpl.java:271)
at org.jboss.ws.core.jaxws.spi.ServiceDelegateImpl.getPort(ServiceDelegateImpl.java:202)
at javax.xml.ws.Service.getPort(Service.java:143)
at org.mainco.bsorg.OrganizationService.getOrganizationServicePort(OrganizationService.java:56)
at org.mainco.subco.orgsclient.quartz.UpdateOrgsJob.execute(UpdateOrgsJob.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
... 11 more
Caused by: java.lang.ClassNotFoundException: No ClassLoaders found for: org.mainco.bsorg.BusinessServiceFaultException
at org.jboss.mx.loading.LoadMgr3.beginLoadTask(LoadMgr3.java:212)
at org.jboss.mx.loading.RepositoryClassLoader.loadClassImpl(RepositoryClassLoader.java:521)
at org.jboss.mx.loading.RepositoryClassLoader.loadClass(RepositoryClassLoader.java:415)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.jboss.wsf.common.JavaUtils.loadJavaType(JavaUtils.java:143)
at org.jboss.ws.metadata.umdm.FaultMetaData.getJavaType(FaultMetaData.java:138)
Edit: I'm deploying the JAR file to $JBOSS_HOME/server/default/deploy . The JAR file contains a Quartz job that I want to run, which is why I'm saving it to this directory.
Check if the jar that you have is the correct version and if you have that jar file on the jboss lib folder (It's C:\jboss-4.2.3.GA\server\seam\lib here)
*seam = the name of the profile you're using

java.lang.NoClassDefFoundError: org/apache/axis2/client/Stub

I've added a Web Service using Axis2 to my project and now I can't run my application.
This is the classpath I am using:
<property name="classpath" location="bin:EventReservationCore/bin:EventReservationCore/db:EventReservationCore/lib/*:EventReservationCore/lib/util_iso2.jar:EventReservationCore/lib/sqlitejdbc-v056.jar:AuthorizationRMI/lib/AuthorizationService.jar:EventReservationCore/lib/activemq-all-5.4.3.jar:/home/ander/axis2-1.6.1/webapp/axis2.war"/>
And this is the target that runs until I add the Axis2 Web Service.
<target name="run.besocial">
<java classname="eventReservationServer.ReservationEventServer" classpath="${classpath}" fork="true">
<jvmarg value="-Djava.rmi.server.codebase=file:EventReservationCore/bin/ file:EventReservationCore/lib/util_iso2.jar"/>
<jvmarg value="-Djava.security.policy=EventReservationCore/java.policy" />
</java>
</target>
As a result I get this error:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axis2/client/Stub
[java] at java.lang.ClassLoader.defineClass1(Native Method)
[java] at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
[java] at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
[java] at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
[java] at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
[java] at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[java] at eventReservationServer.eventServerGateway.WSEventServerGateway.getEvents(WSEventServerGateway.java:19)
[java] at eventReservationServer.ReservationEventServer.<init>(ReservationEventServer.java:101)
[java] at eventReservationServer.ReservationEventServer.main(ReservationEventServer.java:130)
[java] Caused by: java.lang.ClassNotFoundException: org.apache.axis2.client.Stub
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[java] ... 15 more
If you are using tomcat, copy all the jar files located under axis2/lib folder to the tomcat/lib folder and also add them to the classpath like this D:\axis2-1.6.2\lib*
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axis2/client/Stub
Above error can be removed by adding axis2-kernel-1.6.2 to the class path, but you might start getting other errors after you have generated the stubs with wsdl2java.bat.
So you better add all the axis2-jars to classpath while compiling the client.
Hope this helps
None of the answers helped me. Here is then what I did.
I used eclipse to generate Runnable Jar(right click->Export->Runnable Jar) with all the libraries added as package.
It gave me a JAR that I could run from command line.
Then I did diff between Jar from eclipse and Jar that my build created. I was able to find the missing dependencies that my build was not putting in Jar.
If you faced the same issue for JBoss class loader, follow the tips on here and here.
The most important part is to define a new module in JBoss module configurations, and also locate the module in you MANIFEST.MF file.
Maven will help you to do the later part. Following is a sample configuration to add axis module to your Manifest file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<archive>
<manifestEntries> <!--Manually added JBoss Modules (that are not found by JBoss class loader) must be loaded here-->
<Dependencies>axis.axis</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
Depending to your packaging type, you would use other maven plugins like maven-jar-plugin or maven-war-plugin.
The dependency package name must match the name that you have specified to your module name in JBoss modules. The above axis module is defined in JBoss models as explained in the links above.
<module xmlns="urn:jboss:module:1.1" name="axis.axis">
<properties>
<property name="jboss.api" value="private"/>
</properties>
<!-- ... -->
</module>

executing jar file: Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext

I have created a maven project. After I type: mvn clean package everything works fine and I see my jar file inside target. But if I click the file I get the follwing error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.Applica
tionContext
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: comt.test.Test. Program will exit
I can run the project under Eclipse and works ok, but the jar file doesnt work.
This is the content of Manifest:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Build-Jdk: 1.6.0_23
Main-Class: com.stlpo.App
Class-Path: commons-httpclient-3.1.jar commons-codec-1.2.jar h2-1.2.138.jar
commons-lang-2.5.jar spring-core-3.0.3.RELEASE.jar spring-asm-3.0.3.RELEASE.jar co
mmons-logging-1.1.1.jar spring-context-3.0.3.RELEASE.jar spring-beans
-3.0.3.RELEASE.jar spring-expression-3.0.3.RELEASE.jar spring-orm-3.0
.3.RELEASE.jar spring-tx-3.0.3.RELEASE.jar spring-aop-3.0.3.RELEASE.j
ar aopalliance-1.0.jar spring-jdbc-3.0.3.RELEASE.jar spring-test-3.0.
3.RELEASE.jar cglib-nodep-2.2.jar logback-core-0.9.24.jar logback-cla
ssic-0.9.24.jar slf4j-api-1.6.0.jar hibernate-annotations-3.5.4-Final
.jar hibernate-core-3.5.4-Final.jar antlr-2.7.6.jar commons-collectio
ns-3.1.jar jta-1.1.jar hibernate-commons-annotations-3.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar hibernate-entitymanager-3.5.4-F
inal.jar cglib-2.2.jar asm-3.1.jar javassist-3.9.0.GA.jar opencsv-2.1
.jar jfreechart-1.0.13.jar jcommon-1.0.16.jar commons-beanutils-1.8.3
.jar beansbinding-1.2.1.jar poi-3.6.jar log4j-1.2.13.jar
poi-ooxml-3.6.jar poi-ooxml-schemas-3.6.jar xmlbeans-2.3.0.jar stax-
api-1.0.1.jar geronimo-stax-api_1.0_spec-1.0.jar dom4j-1.6.1.jar xml-
apis-1.0.b2.jar commons-dbcp-1.4.jar commons-pool-1.5.4.jar TableLayo
ut-20050920.jar
Thanks in advance for your help.
When you build a JAR, it only has your classes in it. It doesn't contain all the other libraries that your project depends on. (Doing that is possible, but it's not common and mostly inadvisable.) To run a class in your JAR that depends on Spring, you have to be sure Spring is on the classpath, either by passing a classpath to the java executable (doesn't work if using the -jar argument) or by putting a "Class-Path" attribute in the JAR's manifest file. Of course, it's not just Spring. You'll have to include a classpath entry for every jar your project depends on.
see another post regarding the same problem
you need to create a classPath (the eclipse is dong it automatically for you, and this is why you have no problem ruining the project from the eclipse) for the dependencies (in your case the spring)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
<addClasspath>true</addClasspath>
<!-- use class path prefix if your dependencies are out side the jar)
<classpathPrefix>lib/</classpathPrefix> -->
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>

Categories

Resources