如何从命令行执行java .class [英] How to execute a java .class from the command line
问题描述
我有一个已编译的java类:
I have a compiled java class:
Echo.class
public class Echo {
public static void main (String arg) {
System.out.println(arg);
}
}
我 cd
到目录并输入: java Echohello
我收到此错误:
C:\Documents and Settings\joe\My Documents\projects\Misc\bin>java Echo "hello"
Exception in thread "main" java.lang.NoClassDefFoundError: Echo
Caused by: java.lang.ClassNotFoundException: Echo
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Echo. Program will exit.
在我可以从命令行运行的表单中获取我的java代码的最简单方法是什么因为不得不使用Eclipse IDE?
What is the simplest way to get my java code in a form that I can run from the command line as apposed to having to use Eclipse IDE?
推荐答案
尝试:
java -cp . Echo "hello"
假设您编译时使用:
Assuming that you compiled with:
javac Echo.java
那么当前目录可能不在你的类路径中(java查找.class定义)
Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )
如果那是案例和列出您的目录显示的内容:
If that's the case and listing the contents of your dir displays:
Echo.java
Echo.class
然后任何一个可能有效:
Then any of this may work:
java -cp . Echo "hello"
或
SET CLASSPATH=%CLASSPATH;.
java Echo "hello"
后来为弗雷德里克指出你会收到另一个错误消息就像。
And later as Fredrik points out you'll get another error message like.
线程main中的异常java.lang.NoSuchMethodError:main
当发生这种情况时,去看看他的答案:)
When that happens, go and read his answer :)
这篇关于如何从命令行执行java .class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!