NoClassDefFoundError while trying to run my jar with java.exe -jar...what's wrong?

I have an application that I'm trying to wrap into a jar for easier deployment. The application compiles and runs fine (in a Windows cmd window) when run as a set of classes reachable from the CLASSPATH. But when I jar up my classes and try to run it with java 1.6 in the same cmd window, I start getting exceptions:

C:\dev\myapp\src\common\datagen>C:/apps/jdk1.6.0_07/bin/java.exe -classpath C:\myapp\libs\commons -logging-1.1.jar -server -jar DataGen.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at com.example.myapp.fomc.common.datagen.DataGenerationTest.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    ... 1 more

The funny thing is, the offending LogFactory seems to be in commons-logging-1.1.jar, which is in the class path specified. The jar file (yep, it's really there):

C:\dev\myapp\src\common\datagen>dir C:\myapp\libs\commons-logging-1.1.jar
 Volume in drive C is Local Disk
 Volume Serial Number is ECCD-A6A7

 Directory of C:\myapp\libs

12/11/2007  11:46 AM            52,915 commons-logging-1.1.jar
           1 File(s)         52,915 bytes
           0 Dir(s)  10,956,947,456 bytes free

The contents of the commons-logging-1.1.jar file:

C:\dev\myapp\src\common\datagen>jar -tf C:\myapp\libs\commons-logging-1.1.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/apache/
org/apache/commons/
org/apache/commons/logging/
org/apache/commons/logging/impl/
META-INF/LICENSE.txt
META-INF/NOTICE.txt
org/apache/commons/logging/Log.class
org/apache/commons/logging/LogConfigurationException.class
org/apache/commons/logging/LogFactory$1.class
org/apache/commons/logging/LogFactory$2.class
org/apache/commons/logging/LogFactory$3.class
org/apache/commons/logging/LogFactory$4.class
org/apache/commons/logging/LogFactory$5.class
org/apache/commons/logging/LogFactory.class
... (more classes in commons-logging-1.1 ...)

Yep, commons-logging has the LogFactory class. And finally, the contents of my jar's manifest:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 10.0-b23 (Sun Microsystems Inc.)
Main-Class: com.example.myapp.fomc.common.datagen.DataGenerationTest
Class-Path: commons-logging-1.1.jar commons-lang.jar antlr.jar toplink
 .jar GroboTestingJUnit-1.2.1-core.jar junit.jar

This has stumped me, and any coworkers I've bugged for more than a day now. Just to cull the answers, for now at least, third party solutions to this are probably out due to licensing restrictions and company policies (e.g.: tools for creating exe's or packaging up jars). The ultimate goal is to create a jar that can be copied from my development Windows box to a Linux server (with any dependent jars) and used to populate a database (so classpaths may wind up being different between development and deployment environments). Any clues to this mystery would be greatly appreciated!


Asked by: Joyce262 | Posted: 21-01-2022






Answer 1

The -jar option is mutually exclusive of -classpath. See an old description here

-jar

Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point.

See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

A quick and dirty hack is to append your classpath to the bootstrap classpath:

-Xbootclasspath/a:path

Specify a colon-separated path of directires, JAR archives, and ZIP archives to append to the default bootstrap class path.

However, as @Dan rightly says, the correct solution is to ensure your JARs Manifest contains the classpath for all JARs it will need.

Answered by: Kelvin738 | Posted: 22-02-2022



Answer 2

You can omit the -jar option and start the jar file like this:

java -cp MyJar.jar;C:\externalJars\* mainpackage.MyMainClass

Answered by: Ada123 | Posted: 22-02-2022



Answer 3

This is the problem that is occurring,

if the JAR file was loaded from "C:\java\apps\appli.jar", and your manifest file has the Class-Path: reference "lib/other.jar", the class loader will look in "C:\java\apps\lib\" for "other.jar". It won't look at the JAR file entry "lib/other.jar".

Solution:-

  1. Right click on project, Select Export.
  2. Select Java Folder and in it select Runnable JAR File instead of JAR file.
  3. Select the proper options and in the Library Handling section select the 3rd option i.e. (Copy required libraries into a sub-folder next to the generated JAR).

[ EDIT = 3rd option generates a folder in addition to the jar, 2nd option ("Package required libraries into generated JAR") can also be used as you have the jar. ]

  1. Click finish and your JAR is created at the specified position along with a folder that contains the JARS mentioned in the manifest file.
  2. open the terminal,give the proper path to your jar and run it using this command java -jar abc.jar

    Now what will happen is the class loader will look in the correct folder for the referenced JARS since now they are present in the same folder that contains your app JAR..There is no "java.lang.NoClassDefFoundError" exception thrown now.

This worked for me... Hope it works for you too!!!

Answered by: Melanie498 | Posted: 22-02-2022



Answer 4

if you use external libraries in your program and you try to pack all together in a jar file it's not that simple, because of classpath issues etc.

I'd prefer to use OneJar for this issue.

Answered by: Victoria729 | Posted: 22-02-2022



Answer 5

i had the same problem with my jar the solution

  1. Create the MANIFEST.MF file:

Manifest-Version: 1.0

Sealed: true

Class-Path: . lib/jarX1.jar lib/jarX2.jar lib/jarX3.jar

Main-Class: com.MainClass

  1. Right click on project, Select Export.

select export all outpout folders for checked project

  1. select using existing manifest from workspace and select the MANIFEST.MF file

This worked for me :)

Answered by: Emma910 | Posted: 22-02-2022



Answer 6

I have found when I am using a manifest that the listing of jars for the classpath need to have a space after the listing of each jar e.g. "required_lib/sun/pop3.jar required_lib/sun/smtp.jar ". Even if it is the last in the list.

Answered by: Blake825 | Posted: 22-02-2022



Similar questions

java - NoClassDefFoundError without a class name

I'm not quite sure if this is specific to Sun Java Systems Application Server but there are occasions where I run into a NoClassDefFoundError where the class in question, the one not found, is not mentioned in the error. Does anyone know what conditions would lead to a NoClassDefFoundError being raised without specifying which class was not found? Edit: Java 1.4


java - NoClassDefFoundError on command-line with new NetBeans project

I've created a new J2SE project in NetBeans, and I can run it from the IDE, but when I try to run it using Ant on the command line, I get the following problem: &lt;snip&gt; run: [java] Exception in thread "main" java.lang.NoClassDefFoundError: IndexBuilder [java] Java Result: 1 &lt;snip&gt; Based on the snippet from project.properties below, the clas...


java - NoClassDefFoundError on JFace FontRegistry

When I launch an SWT application (via an Eclipse launch profile), I receive the following stack trace: Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jface/resource/FontRegistry at org.eclipse.jface.resource.JFaceResources.getFontRegistry(JFaceResources.java:338) at org.eclipse.jface.window.Window.close(Window.java:313) at org.eclipse.jface.dialogs.Dialog.close(Dialog.jav...


java - Why is this NoClassDefFoundError being thrown when the class exists?

I've tried to create a generic Observable class that I can use in my program: public class GeoGolfObserver&lt;T&gt; extends Observable { public GeoGolfObserver() { super(); } public void passObject(T object) { setChanged(); notifyObservers(object); } } It is created using: GeoGolfObserver&lt;Cache&gt; cacheObserver = new GeoG...


java - NoClassDefFoundError inside jar

I have a class that that sits in a package called com.toptur.sysTray all it does is load system tray it does not use any external packages. i create a SysTray object to install the system tray. Everthing builds fine. i can run the application from command line and systray gets installed. But when i try to create a jar from the class files and run it i get NoClassDefFoundError. Package and its class files are in the...


java - Ant + JUnit: NoClassDefFoundError

Ok, I'm frustrated! I've hunted around for a good number of hours and am still stumped. Environment: WinXP, Eclipse Galileo 3.5 (straight install - no extra plugins). So, I have a simple JUnit test. It runs fine from it's internal Eclipse JUnit run configuration. This class has no dependencies on anything. To narrow this problem down as much as possible it simply contains: @Test public v...


unix - Java Application NoClassDefFoundError

Created a Java application to upload documents via CIS (Content Integration Suite) to a storage application. The app runs successfully in RAD, but as a executable jar in a unix environment, getting a NoClassDefFoundError. I can not find the class on my local machine and there are not references to the class on the internet. The manifest contains that class path for the needed jar files and the main class. ...


jax rpc - Java JAX-RPC NoClassDefFoundError

I am new to JAX-RPC. I tried creating a simple Java bean in RAD 7.0, and went through the options to create a JAX-RPC webservice. My method is as below. public byte[] getData(byte[] argument) { &lt;Customclass&gt; proxy = new &lt;Customclass&gt;(); List outputList = new ArrayList(); try { // Call a method on proxy } catch (Exception e) { // TODO Auto-generated catch block ...


java - Catching webservice exception with CXF: NoClassDefFoundError: SOAPFaultBuilder

I've been using Apache CXF wsdl2java generated code to call methods from a webservice for some time now, which so far has been working fine.. The problem I'm having is that when the webservice (implemented just down the hall from me) legitimately throws a soap exception, CXF comes up with the following Error message: Could not initialize class com.sun.xml.internal.ws.fault.SOAPFaultBuilder


Java in bash at university fails with NoClassDefFoundError

I know there are many posts concerning NoClassDefFoundError, they all seem to be talking about jar files. While I'm comfortable with java in eclipse, I'm pretty lost as to why the simplest thing I can come up with is not functioning, unless they broke something on the university side of this. public class hello { public static void main (String args[]) { System.out.println ("Hello World!"); ...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top