Java命令行找不到或加载主类 [英] Java command line could not find or load main class
问题描述
我一直试图从命令行运行任何.class文件,但我一直收到此错误.在整夜试图解决问题之后,我正式陷入了困境.
I have been trying to run any .class file from the command line, and I keep getting this error. After trying to figure out the problem on my own all night, i'm officially stumped.
我在Linux上,通常使用eclipse,因此我的.java文件和.class文件位于两个不同的位置.
I'm on linux, and I use eclipse usually so my .java file and .class file are in two different locations.
这里的类路径....
%> echo $CLASSPATH
/home/mike/Documents/java
这里是我的.class目录之一...
Heres one of my .class directories...
/home/mike/Documents/java/homework/bin/src
在此对应的.java目录...
Heres the corresponding .java directory...
/home/mike/Documents/java/homework/src/src
这里是我在bash中输入的内容...
Heres what I enter in bash...
[ mike (23:31:16): ~] $ >java homework.bin.src.EulerMath
OR
[ mike (23:32:41): ~/Documents/java] $ >java -cp . homework.bin.src.EulerMath
OR
[ mike (23:34:08): ~/Documents/java/homework/bin/src] $ >java -cp . EulerMath
所有人都给我同样的错误.显然,我正在尝试运行src包中的EulerMath.class.这可以在eclipse中编译并正常运行,而我尝试从命令行运行的所有其他程序也存在类似的问题.我可以使用javac编译.java文件,但是尝试运行新的.class文件时出现此错误.非常感谢您的帮助
All give me the same error. Obviously im trying to run EulerMath.class, which is within the package src. This compiles and runs fine within eclipse, and I have a similar issue with every other program i try to run from the command line. I can compile the .java file with javac but when trying to run the new .class file i get this error. Id appreciate any help
推荐答案
修改您的 CLASSPATH
之类的
export CLASSPATH="$HOME/Documents/java/homework/bin"
然后您就可以运行它,而无需像
这样的 -cp
命令行
then you can run it without a -cp
command line like
java src.EulerMath
无需设置 CLASSPATH
(或者如果要覆盖设置的 CLASSPATH
),则可以
Without setting the CLASSPATH
(or if you want to override the set CLASSPATH
) you can do
java -cp $HOME/Documents/java/homework/bin src.EulerMath
但是 src.EulerMath
是您的类的名称.或者,您可以将其打包为带有一些元信息的jar文件(基本上是 Main-Class
),然后使用 java -jar< jarfile.jar>
But src.EulerMath
is the name of your class. Alternatively, you can package it in a jar file with some meta-information (basically a Main-Class
) and run it with java -jar <jarfile.jar>
这篇关于Java命令行找不到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!