run a java program

天大地大妈咪最大 提交于 2019-12-13 04:45:39

问题


I want to run a java program using shell script. The java program is in p2 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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3798950/run-a-java-program-in-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!