Java Mac 终端:无法找到或加载主类 CLASSNAME
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24174947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mac Terminal: Could not find or load main class CLASSNAME
提问by Dean Leitersdorf
I am trying to run a java program through the Terminal on Mac, yet getting:
我正在尝试通过 Mac 上的终端运行 Java 程序,但得到:
Error: Could not find or load main class (MY CLASSNAME)
I compiled this application with Eclipse, and when I run this with Eclipse, it works fine. Furthermore, I am in the right directory, as when I type "ls" in the Terminal, it lists all the files, includes the class file I am trying to run.
我用 Eclipse 编译了这个应用程序,当我用 Eclipse 运行它时,它工作正常。此外,我在正确的目录中,因为当我在终端中键入“ls”时,它会列出所有文件,包括我尝试运行的类文件。
This is what I type:
这是我输入的内容:
java mainClass
I would very much appreciate help to solve this!
我非常感谢帮助解决这个问题!
Thank you,
谢谢,
Dean
院长
EDIT: Solution - instead of java mainClass, it must have package too: java startPackage.mainClass
编辑:解决方案 - 而不是 java mainClass,它也必须有包:java startPackage.mainClass
采纳答案by MadProgrammer
Start by making sure you are at the directory above the top level package
首先确保您位于顶级目录之上 package
If the class belongs to the package
com.foo.bar
, you want to be in the directory above com
.
如果类属于package
com.foo.bar
,则要在上面的目录中com
。
In your case, you want to be in the directory above startPack
.
在您的情况下,您希望位于上面的目录中startPack
。
Then you need to use the fully qualified name to run the class...
然后您需要使用完全限定名称来运行该类...
java statPack.mainClass
For example...
例如...
回答by merlin2011
Make sure you have the current directory inside your CLASSPATH.
确保您的 CLASSPATH 中有当前目录。
java -cp . mainClass
To set this globally, you can use export CLASSPATH=$CLASSPATH:.
inside .bash_profile
.
要全局设置它,您可以使用export CLASSPATH=$CLASSPATH:.
inside .bash_profile
。
Separately, if your class lives inside a package such as com.foo.bar
, then you will need to go to the parent directory of com
and run your application with the full path.
另外,如果您的类位于诸如 之类的包中com.foo.bar
,那么您将需要转到 的父目录com
并使用完整路径运行您的应用程序。
java com.foo.bar.mainClass
回答by gbhati
I too faced this on Mac machine and then what I had to do to make it work was:
我也在 Mac 机器上遇到了这个问题,然后我必须做的是让它工作:
Problem Statement:
问题陈述:
I had one package xyz under the root of project i.e src/main/java and then inside xyz package I had one class Student.java
我在项目的根目录下有一个包 xyz,即 src/main/java 然后在 xyz 包内我有一个类 Student.java
my current directory is /Users/username/projectname/src/main/java/xyz: I can see Student.java exists here and I compiled it using javac Student.java
我当前的目录是 /Users/username/projectname/src/main/java/xyz: 我可以看到 Student.java 存在在这里,我使用 javac Student.java 编译它
Now I see class file has been created at this location. But when I try to run the class file using java Student I get the error: Error: Could not find or load main class Student
现在我看到在这个位置创建了类文件。但是当我尝试使用 java Student 运行类文件时,出现错误:错误:找不到或加载主类 Student
Solution:
解决方案:
Now the solution is to go one step back in the directory and go to root path:/Users/username/projectname/src/main/java and run the command
现在的解决办法是在目录中后退一步,进入根路径:/Users/username/projectname/src/main/java 并运行命令
java xyz.Student
java xyz.Student
and it will work.
它会起作用。
Link to follow: https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html
链接如下:https: //javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html
回答by Charles Goodwin
For people dumb like me, make sure you are typing java HelloWorld
- and NOTjava HelloWorld.class
- to run the compiled file with the name HelloWorld.class
. This is especially so if you are used to hitting the tab key to complete the file name, as the terminal will give you java HelloWorld.class
if you hit the tab key for autocomplete after typing something like java He
...
对于像我这样愚蠢的人,请确保您正在输入java HelloWorld
- 而不是java HelloWorld.class
- 以运行名称为HelloWorld.class
. 如果您习惯于按 Tab 键来完成文件名,则尤其如此,因为java HelloWorld.class
如果您在键入诸如java He
...
This answer is here because it took 3 sites, including this answer, and 25 mintues before I figured out what I was doing wrong.
这个答案在这里是因为它花了 3 个网站,包括这个答案,在我弄清楚我做错了什么之前 25 分钟。
Logic is easy, typing is hard.
逻辑很简单,打字很难。