运行一个java程序
我想使用shell脚本运行java程序。 java程序在p2目录下,名字是maxconnect4,我已经编译好了,类名是maxconnect4。我这样编写 shell 命令:
java p2/maxconnect4 arg1 arg2 arg3
这个 shell 命令不起作用。 它给出一个错误: Exception in thread "main" java.lang.NoClassDefFoundError: p2/maxconnect
但是,我以这种方式编译java程序:
javac p2/*.java,并且它有效。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设该类声明了 package p2; ,那么应该可以工作——尽管更标准的方法是在完全限定的类名中使用点而不是斜杠——java p2.maxconnect< /代码>。
如果该类没有包声明,请尝试 java -cp p2 maxconnect。您需要指定一个类路径,以便在顶层找到类文件。
如果该类有其他包声明,则需要将其放入与其包匹配的文件夹中。
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.
只需使用 java -cp p2 maxconnect4 arg1 arg2 arg3 即可。 -cp 设置 JVM 的类路径。编辑:我假设您没有使用 maxconnect4 的包。
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.尝试使用
另外,您可以尝试检查类名,并验证文件 p2/maxconnect4.class 是否存在。
Try with
Also, you can try to check the class name, and verify if the file p2/maxconnect4.class exists.