简体   繁体   中英

“Could Not Find or Load Main Class…”

I am new to Java and I got this book to help me start.
I have successfully compiled Hello.java using " javac Hello.java ".
Now it says to type in " java Hello " and I am getting " 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 .

Solution 1

Add the path of your directory where you have compiled your class to the CLASSPATH variable in Environment Variables.

Solution 2

Every time you run the program add the current folder and libraries you are referencing in the classpath using -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:

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

you should check if the hello.class file is present in the folder. java runs these .class files. These files are created on successful compilation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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