2
votes

I am trying to create a jar file having following project contents

  • Project
    • user_interface (package folder including all .classes related to GUI)
    • function_operations (package folder including all .classes related to operations)
    • sounds (folder include all sound files)
    • icons (include all images and icons)
    • jars (include all external jar files)
    • manifest.txt (having one line *Main-Class: user_interface.messengerGUI* and two enters)

i m using command

jar cvfm helloBuddy.jar manifest.txt user_interface/*.class function_operations/*.class icons/ sounds/

It successfully create jar file but gives following error while executing

JAI-MATA-DI-LINUX Project # java -jar helloBuddy.jar Exception in thread "main" java.lang.NoClassDefFoundError: user_interface/messengerGUI Caused by: java.lang.ClassNotFoundException: user_interface.messengerGUI at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: user_interface.messengerGUI. Program will exit.

another problem is how to include external jars?

Edited: finaly some success to jar formation. Using eclipse i am able to create jar file. But when i run it gives the following error

Exception in thread "main" java.lang.NoClassDefFoundError: org/jivesoftware/smack/ChatManagerListener Caused by: java.lang.ClassNotFoundException: org.jivesoftware.smack.ChatManagerListener at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: user_interface.messengerGUI. Program will exit.

I have included few external jar files in project. org/jivesoftware/smack/ChatManagerListener this class belongs to one of the smackx.jar file. so this error is of external jar files added to project. any clue how can i resolve this?

2
I suspect your class end up in the root of the Jar. What is the output of jar -tvf helloBuddy.jar? Copy/past it into the question as an edit and use code formatting.Andrew Thompson
Please do have a look at this answer of mine, might be it can help a bit, you not adding your package(.class files and images) in the right sense it appears to me to the .jar file. You can not suppose to add individual classes to the .jar file, instead you have to include the whole package, so instead of writing user_interface/*.class you be writing user_interface if user_interface is the package which contains all the .class files.nIcE cOw

2 Answers

1
votes

Try this, this is the way to create a jar or runnable jar in eclipse, all your external libraries in the project will be included

File -> Export-> Java ->Runnbale JAR file

Launch configuration : your Class containing the public static void main(String[] args)

Export destination : Target place

Library Handling:

Package required libraries into generated JAR

FINISH
1
votes

You cannot nest jar files like that unless you use a custom class loader like jarclassloader or use a tool like onejar.