0

I have a jar called GameClient.jar which has a main method. And I have a json.jar which does contain an org/json/JSONException.class

Both of these are in the dist folder.

I run the following java -classpath dist/json.jar -jar dist/gameclient.jar

Not really sure why I still get

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Caused by: java.lang.ClassNotFoundException: org.json.JSONException

I am on Windows XP and Java 1.7. I get the same error with or without the classpath option.

4

2 に答える 2

7

From the java documentation:

-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 appli- cation's starting point. See the jar(1) and the Jar trail of the Java Tutorial @ http://java.sun.com/docs/books/tutorial/jar 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.

So your gameclient.jar must include all of the user classes that you need. The classpath setting is ignored. Just create the gameclient.jar to include the classes in json.jar.

于 2012-10-01T21:33:17.713 に答える
2

Rather than supplying the classpath as a command line argument you can add a classpath entry to your GameClient MANIFEST.MF:

Class-Path: dist/json.jar 

See Including a Class-Path In Your MANIFEST.MF

于 2012-10-01T21:42:55.673 に答える