When executing "java" command in CMD it is not possible to locate or load the main class

Asked

Viewed 24,752 times

4

After executing the command javac normally and create the file .class, I try to execute the command java plus it gives this problem of not being able to locate or load the main class.

I think it might be a confusion of mine in the environment variables because I’m starting now to learn programming and really not mean much about such variables.

Anyway I used this "step-by-step" in the link below to configure the system variables:

http://tisuperinfo.blogspot.com.br/2013/01/problema-javac-nao-e-reconhecido-como.html

I used only the commands javac and java in the cmd, out of those to locate in directories.

Using this code below, right after the java command to run the program, it occurs. Error: Unable to find or load Cprincipal main class.

import javax.swing.JFrame;

public class CPrincipal {

    public static void main (String args[]){

        JFrame janela = new JFrame ();

        janela.setSize(500,300);
        janela.setVisible(true);
    }


}
  • 1

    Could [Edit] your question and add your code to it, the commands you used and the console output?

  • From what you said about the main class I believe the file class. java you made has a main method, without the main method the application will not start. The main is the entry point of the program, which is why the program starts.

  • Place exactly the error message that occurred and when it occurred.

  • I think the problem is that you didn’t correctly define the variable PATH.

  • I answered your question with a broader question that can help more future users of the site with the same question: http://answall.com/questions/82229/erro-n%C3%A3o-%C3%A9-poss%C3%Advel-localize-nor-load-a-class-main-in-java-eclipse/

2 answers

5


In the directory where you compiled the file with javac

javac CPrincipal.java

the file will appear Cprincipal.class

In that same directory, write

java CPrincipal

Jframe screen should appear normally.

  • the problem is just this... I do exactly that more than the error when using the java command. ERROR: Unable to locate or load the main class Cprincipal.class

  • Remove all java you have installed and poe 1 SDK only. Then check that %Java_home% is in Environment variables and add %Java_home% bin to the path. Finally, when calling the java command, see the option to add classpath and place a point. Type java -classpath=. Cprincipal

2

You probably did the same stupid thing I did when it came to creating the environment variables.

The variable ClassPath need to have a .; before the assigned path.

Mine was like this: .;C:\Program Files\Java\jdk1.8.0_40\lib

Browser other questions tagged

You are not signed in. Login or sign up in order to post.