简体   繁体   English

“找不到或加载主类……”

[英]“Could Not Find or Load Main Class…”

I am new to Java and I got this book to help me start. 我是Java的新手,我拿到了这本书来帮助我入门。
I have successfully compiled Hello.java using " javac Hello.java ". 我已经使用“ javac Hello.java ”成功编译了Hello.java。
Now it says to type in " java Hello " and I am getting " Could not load of find main class Hello ". 现在它说输入“ java Hello ”,我得到“ Could not load of find main class Hello ”。 I have tried to find out how to fix it before but all the answers are complicated and confusing. 我曾尝试找出解决方法,但所有答案都很复杂且令人困惑。 If someone could explain how to fix this, that would be awesome. 如果有人可以解释如何解决此问题,那将是很棒的。

The problem appears to be of CLASSPATH . 问题似乎出在CLASSPATH上

Solution 1 解决方案1

Add the path of your directory where you have compiled your class to the CLASSPATH variable in Environment Variables. 将您已在其中编译类的目录的路径添加到“环境变量”中的CLASSPATH变量。

Solution 2 解决方案2

Every time you run the program add the current folder and libraries you are referencing in the classpath using -classpath . 每次运行程序时,都使用-classpath在classpath中添加当前要引用的文件夹和库。 eg: 例如:

java -classpath .;lib/referenced-libs.jar my.package.MainClass

Make sure the main method with exactly this syntax is present in the Hello.java file: 确保Hello.java文件中存在具有这种语法的main方法:

public static void main(String[] args) {
    // your code will go here...
}

you should check if the hello.class file is present in the folder. 您应该检查文件夹中是否存在hello.class文件。 java runs these .class files. Java运行这些.class文件。 These files are created on successful compilation. 这些文件是在成功编译后创建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM