run a java program
I want to run a java program using shell script. The java program is in p2 开发者_JAVA技巧directory and its name is maxconnect4 and I have already compiled it, the class name is maxconnect4. I write the shell commands like this:
java p2/maxconnect4 arg1 arg2 arg3
This shell command does not work. It give an error: Exception in thread "main" java.lang.NoClassDefFoundError: p2/maxconnect
However, I compile the java program in this way:
javac p2/*.java, and it works.
Assuming that the class has package p2;
declared, that should work -- although the more standard way is to use dots instead of slashes in the fully-qualified classname -- java p2.maxconnect
.
If the class has no package declaration, try java -cp p2 maxconnect
. You need to specify a classpath such that the class file is found at the top level.
If the class has some other package declaration, you need to put it into a folder that matches its package.
Just use java -cp p2 maxconnect4 arg1 arg2 arg3
. -cp sets the classpath of the JVM. Edit: I assume you don't use a package for maxconnect4.
Try with
java p2.maxconnect4 arg1 arg2 arg3
Also, you can try to check the class name, and verify if the file p2/maxconnect4.class exists.
精彩评论