What does “Could not find or load main class” mean

2018-12-30 22:02发布

A common problem that new Java developers experience is that their programs fail to run with the error message: Could not find or load main class ...

What does this mean, what causes it, and how should you fix it?

标签: java class main
30条回答
皆成旧梦
2楼-- · 2018-12-30 22:43

When the same code works on one PC, but it shows the error in another, the best solution I have ever found is compiling like the following:

javac HelloWorld.java
java -cp . HelloWorld
查看更多
不流泪的眼
3楼-- · 2018-12-30 22:44

If your classes are in packages then you have to cd to the main directory and run using the full name of the class (packageName.MainClassName).

Example:

My classes are in here:

D:\project\com\cse\

The full name of my main class is:

com.cse.Main

So I cd back to the main directory:

D:\project

Then issue the java command:

java com.cse.Main
查看更多
孤独寂梦人
4楼-- · 2018-12-30 22:44

What fixed the problem in my case was:

Right click on the project/class you want to run, then Run As->Run Configurations. Then you should either fix your existing configuration or add new in the following way:

open the Classpath tab, click on the Advanced... button then add bin folder of your project.

查看更多
路过你的时光
5楼-- · 2018-12-30 22:44

You really need to do this from the src folder. There you type the following command line:

[name of the package].[Class Name] [arguments]

Let's say your class is called CommandLine.class, and the code looks like this:

package com.tutorialspoint.java;

    /**
     * Created by mda21185 on 15-6-2016.
     */

    public class CommandLine {
        public static void main(String args[]){
            for(int i=0; i<args.length; i++){
                System.out.println("args[" + i + "]: " + args[i]);
            }
        }
    }

Then you should cd to the src folder and the command you need to run would look like this:

java com.tutorialspoint.java.CommandLine this is a command line 200 -100

And the output on the command line would be:

args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100
查看更多
看风景的人
6楼-- · 2018-12-30 22:44

I also faced similar errors while testing a Java MongoDB JDBC connection. I think it's good to summarize my final solution in short so that in the future anybody can directly look into the two commands and are good to proceed further.

Assume you are in the directory where your Java file and external dependencies (JAR files) exist.

Compile:

javac -cp mongo-java-driver-3.4.1.jar JavaMongoDBConnection.java
  • -cp - classpath argument; pass all the dependent JAR files one by one
  • *.java - This is the Java class file which has main method. sdsd

Run:

java -cp mongo-java-driver-3.4.1.jar: JavaMongoDBConnection
  • Please do observe the colon (Unix) / comma (Windows) after all the dependency JAR files end
  • At the end, observe the main class name without any extension (no .class or .java)
查看更多
ら面具成の殇う
7楼-- · 2018-12-30 22:45

Use this command:

java -cp . [PACKAGE.]CLASSNAME

Example: If your classname is Hello.class created from Hello.java then use the below command:

java -cp . Hello

If your file Hello.java is inside package com.demo then use the below command

java -cp . com.demo.Hello

With JDK 8 many times it happens that the class file is present in the same folder, but the java command expects classpath and for this reason we add -cp . to take the current folder as reference for classpath.

查看更多
登录 后发表回答