Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Every java program I try to start shows error

SOLVED, Program was in location with national symbol in it's path.

I just started studying java, but every program i try to start (even example ones from my course) shows an error.

Error: Could not find or load main class "Any class name of program I try start"
C:\Users\Mine\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53:           Java returned: 1
BUILD FAILED (total time: 0 seconds)

edit:

example of code, but happens to any code.

public class Hello {

    static void hello(){
        System.out.println("Hello, World!");

    }

    public static void main(String[] args) {
        hello();

    }    
}
like image 597
atilas1 Avatar asked Nov 16 '15 11:11

atilas1


2 Answers

This error means that when Netbeans is invoking the JVM, the JVM cannot find the class file for the class Netbeans is telling it to run. When you create a project in Netbeans, the classpath will be configured for you by the IDE, so you shouldn't normally see this error unless you have deleted the auto-generated main class and made a new one from scratch in the wrong place.

So the first thing to do is check what class Netbeans is using as the main class:

Right-click on the project name in the Projects tab and click on "Properties"

Step 1

Then click on "Run" and check the name of the class in "Main Class":

Step 2

Note in my example the class is called "tests.Test". This means the class Test in the package "tests". In your question, your class "Hello" doesn't have a package declaration at the top (although you may have chosen not to copy this). If you have no package (and you really should be using packages, even for trivial programs like "Hello, World!", just to get used to doing so, if nothing else), the "Main Class" entry should just be the class name.

So you need to either move your class into the package specified in this parameter, or change this parameter to match the fully qualified name of your main class

like image 159
daiscog Avatar answered Oct 03 '22 15:10

daiscog


Error: Could not find or load main class "Any class name of program I try start"
C:\Users\Mine\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53:           Java returned: 1
BUILD FAILED (total time: 0 seconds)

You are attempting to run a class called Any class name of program I try start, however the name of your class is Hello.

I don't know how Netbeans does things, but I would first try compiling and running the program without netbeans.

javac Hello.java
java Hello

If that works then open up the run settings in netbeans and make sure that it is doing the same thing.

like image 22
Michael Lloyd Lee mlk Avatar answered Oct 03 '22 15:10

Michael Lloyd Lee mlk