Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError while running from jar

I am getting a "no class definition found" exception while trying to run my application on Windows (it runs fine on OS X). The classes the JVM is complaining about are my classes (no third party jars required). When I unzip the files inside the jar, all files are present, including the ones the JVMm is complaining about.

The jar is created using the following task:

<target name="jar" depends="">
<jar destfile="build/app.jar" > 
  <manifest>
    <attribute name="Built-By" value="hamza"/>
    <attribute name="Main-Class" value="com.hamza.driver.ui"/>
<attribute name="Class-Path" value="./"/>
  </manifest>
  <fileset dir="build">
    <include name="**/*.class"/>
<include name="**/*.png"/>
<include name="**/*.xpi"/>
<include name="**/*.html"/>
<exclude name="**/*.jar"/>
  </fileset>
</jar>

I cannot figure out what is causing the problem. If I unzip the jar and run the jar from the directory I unzipped the class to, everything works fine. So, I am assuming all the required files are inside the jar.

EDIT: com.hamza.driver.ui is a class in a package called com.hamza.driver which has main.

After the build, I get one jar "app.jar", and I run it using "java -jar app.jar", which executes fine on OS X, but not on Windows.

If I unzip app.jar in a seperate directory and run "java -jar app.jar", it excutes fine.

EDIT 2: exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/hamza/gui/tr
ansfer/ClipboardTransferHandle
        at com.hamza.driver.ui.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.hamza.gui.transfer.Clipboa
rdTransferHandle
        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)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        ... 1 more

ClipboardTransferHandle .class files are present in the jar.

EDIT 3: imports for the clip board class:

import java.util.logging.Logger;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.IOException;

While playing with it, I found that if I try to declare ClipboardTransferHandle as a static variable in the driver, it works, but every object that is not static is not found. All the main GUI elements are static variables, so the GUI is constructed, but other elements are not; everything that is created not static causes NoClassDefFound, but if I declare them static for testing, they work.

like image 989
Hamza Yerlikaya Avatar asked Nov 15 '25 18:11

Hamza Yerlikaya


2 Answers

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).
  4. Click finish and your JAR is created at the specified position along with a folder that contains the JARS mentioned in the manifest file.
  5. 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 you too!!!

like image 71
Gautam Mandsorwale Avatar answered Nov 18 '25 08:11

Gautam Mandsorwale


Which class is missing? Your Main-Class attribute looks a little suspect--is com.hamza.driver.ui a class or a package?

like image 42
Dominic Cooney Avatar answered Nov 18 '25 08:11

Dominic Cooney