link GetOpt to main jar
I have a folder gnu\getopt
contains Getopt.class
which is need by ChatDemo.jar for parser argument parameters, when I ran with java -cp xSocket.jar;. ChatDemo.jar
it produce:
Exception in thread "main" java.lang.NoClassDefFoundError: gnu/getopt/Getopt
at ChatDemo.main(ChatDemo.java:24)
Caused by: java.lang.ClassNotFoundException: gnu.getopt.Getopt
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
开发者_如何学Python at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 1 more
ChatDemo.java:
import gnu.getopt.Getopt;
ChatDemo Manifest:
Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)
Main-Class: ChatDemo
Class-Path: xSocket.jar
Run it with java -classpath "xSocket.jar;.;ChatDemo.jar ChatDemo
(no .jar
after the name of the main class).
Note that you must add the folder which contains "gnu" to the classpath. Another solution is to put the getopt classes in a JAR and add that to the classpath. Make sure that the folder structure (gnu\getopt
) is preserved.
It looks like you are mixing up the two ways of running java
. You should either supply a classpath (e.g. using the -cp
argument), or launch from a JAR file. You cannot do both.
If you launch using a JAR file, the -cp
argument is ignored. In that case, you must specify the complete classpath in the JAR file's manifest.
The manifest in the ChatDemo.jar file apparently doesn't include. You can remedy this in two ways:
- Update the manifest classpath to be "
Class-path: xSocket.jar .
" ... note we use a space to separate the entries here, not;
or:
. - Add
Getopt.class
to the JAR file asgnu/getopt/Getopt.class
.
However, if you launch using a JAR file you have to use the -jar
option. If you try to your application like this:
java -cp xSocket.jar;. ChatDemo.jar
it will fail telling you that it cannot find a class called ChatDemo.jar
; i.e. it will misinterpret the JAR filename as a qualified classname.
Add Main-Class - as fully qualified (I mean as per package structure) class name of your class which contains main method.
精彩评论