views:

239

answers:

2

I hate starting a post with this but I'm new to Java... I've followed this tutorial to create a socket server (mines in Eclipse). I can run the server within Eclipse, all is well. But when I try to export the project I can't figure out how to run it. I keep getting this error (it varies depending on how I run it)

Exception in thread "main" java.lang.NoClassDefFoundError: xsocketserver/Main
Caused by: java.lang.ClassNotFoundException: xsocketserver.Main
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:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)

I've read this problem relates to class paths being set. I've tried the following:

java -cp libs/xSocket-2.8.12.jar xsocketserver.Main
java -jar xSocketServer.jar    
java -classpath xSocketServer:xSocketServer/libs/xSocket-2.8.12.jar xsocketserver.Main

plus many others. The file structure within the JAR is as follows:

xSocketServer.jar
    -> xsocketserver
        -> Main.class
        -> xSocketDataHandler.class
    -> META-INF
        -> MANIFEST.MF
    -> libs
        -> xSocket-2.8.12.jar

Incidentally I've tried adding my own manifest file which contains the Class-Path but when I check it it always reads:

Manifest-Version: 1.0
Main-Class: xsocketserver.Main

I'm guessing this is a common problem based on the number of hits I've seen in Google but I can't fathom what I'm doing wrong. Wrong Export settings maybe??

+1  A: 

I don't believe a jar-file can include another jar (as you've included xSocket-2.8.12.jar). If xSocket is your own code, let it be included directly in the "outer" jar instead.

Check out http://www.velocityreviews.com/forums/t143595-jars-containing-jars.html and http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar

Otherwise it looks right to me. Make sure you're not trying to run an old/stale version of the jar. (Delete the jar and make sure eclipse exports a new one.)

How are you exporting the .jar file from eclipse? The generated jar-file will usually be based on one of your run-configurations. Make sure you use the one that you use when it works from eclipse.

Unless you've already tried it, try following the steps (listed at the site below)

http://forums.sun.com/thread.jspa?threadID=5358121

1-Right click on your project and select export option.A export window will get pop-up.

2-Select jar file option from the poped-up window which will be in java option of window.

3-After Clicking JarFile option a new window will get pop-up.

4-Select the export destination for ur jar file and click next.

5-After clicking the next a new screen you will see, click on next button again.

6-Now u will see a new screen which has a field name as "Main Class" browse for your main class of appliaction.main class is once which has main method.

7-Now select finish.

aioobe
Thanks for advice aioobe. I had a look at OneJar but couldn't quite get my head around wrapping the Main in a jar itself. I took the easy root in the end and extracted the JAR then put them in a lib folder and added that. Works now. Think I'll spend some more time on a few tutorials.
Trist
A: 

I had a similar problem and yours may or may not be related...

The Jar manifest file has to be in UTF-8 format. I had used windows notepad and that did not work. When I recreated the manifest file with another editor, it worked fine.

I assume eclipse will create UTF-8 files but I'm not sure if it will automatically convert if they are not in that format

If you did use another editor to create the first version of the manifest, try deleting the manifest.mf and recreating inside of eclipse.

John

JohnA