简体   繁体   中英

Could not find main class in Netbeans

package abc ;   
class Trying
{
Trying ()
{
    System.out.println("hello");
}
        }

public class trying {
public static void main(String[] args) {
    new Trying () ;
}

}

In this when I change the name of class from Trying to some other name it works , but here it says :

Error: Could not find or load main class abc.trying /Users/name/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

Why is this happening ? I didn't find such case in any of the questions already asked .

Java is case sensitive language, but there is no documentation that class name should be case sensitive or not.

In eclipse it will show you syntax error

Class file collision: A resource exists with a different case: '/sample/bin/abc/Trying.class'.

OR

If not shows error it will create class file of only one class either Trying or trying .

1) If class file of Trying class is generated then It will throw

Error: Main method not found in class abc.trying

Since there is no main method in class Trying , And at runtime it looking for main method to start.

2) If class file of trying class is generated then It will throw

Exception in thread "main" java.lang.NoClassDefFoundError:

here at runtime it looking for class Trying since it called in main of class trying . It fails to load coz its not compiled.

So we can conclude java not allow two class with sameName even different case

more details of case sensitive of class name is here

class Trying
{
    Trying ()
    {
        System.out.println("hello");
    }
}

public class Try_Main {
    public static void main(String[] args) {
        new Trying () ;
    }
}

Please use two different class names other than same name with different cases. On compilation, the compilation will success and compile will create two class files with same name but different cases. But, the OS only allows a single file and it simply overwrites first one( which created firstly on compilation, then second) by the second one. On running, you will get a run time error, because one of the classes is missing. So, please use different names...

好吧,该类应该是公共的, 并确保将文件名保存为类Name

You have specified Trying multiple times.

package abc;

public class Trying {

    public static void main(String[] args) {

        trying1();

    }

    public static void trying1() {

        System.out.println("Good?!");

    }

}

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