Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why it could not find the main class?

I have a very simple code:

package mygame;
public class RunGame {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

I can compile that code but I cannot run it. When I type "java RunGame" in the command line I get:

Exception in thread "main" java.lang.NoClassDefFoundError: RunGame (wrong name: mygame/RunGame).
....
Could not find the main class: RunGame. Program will exit.

like image 910
Roman Avatar asked Mar 10 '10 12:03

Roman


People also ask

How do you set class as main class?

To change the main class being used, go to the File menu and choose Project Properties. This dialog gives all the options that can be changed in a NetBeans project. Click on the Run category. On this page, there is a Main-Class option.

Is Main class allowed in java?

The Java Main ClassIf only a single Java class in your Java program contains a main() method, then the class containing the main() method is often referred to as the main class. You can have as many classes as you want in your project with a main() method in.

Could not find or load main class when running a jar?

When the JVM is unable to locate the main class, it's often because it's looking for the corresponding . class files in the wrong classpath. Of course, the way to rectify this problem is to manually specify the classpath by either using packages or specifying the classpath.


2 Answers

java mygame.RunGame 

is the java executable syntax. i.e, java classname.qualified.with.full.packaging

Also what is the RunColoredTrails class in the output you have shown?

like image 88
Kannan Ekanath Avatar answered Sep 22 '22 09:09

Kannan Ekanath


u might be trying
C:\your-java-directory-\mydir\> java RunGame right ?

remember RunGame is inside a package called mydir. so go one step back in ur execution path..

c:\your-java-directory\>

now compile and execute like this

c:\your-java-directory\> javac mydir\RunGame.java
c:\your-java-directory\> java mydir.RunGame

like image 21
raj Avatar answered Sep 23 '22 09:09

raj