Error: Can not find or load the main class in Java (Eclipse or CMD)?

8

If you have ever encountered the error message: Erro: não é possível localizar nem carregar a classe principal probably will not be able to execute your codes in Java.

In the eclipse I came across this message:

Erro: não é possível localizar nem carregar a classe principal

But is it also possible that some people will encounter this message in the "CMD" itself? If so, how do I fix this?

    
asked by anonymous 24.08.2015 / 16:51

6 answers

7

Firstly, if you come across an error like this in Eclipse, you've probably imported some project from your *workspace* or from anywhere else in your Windows or you've touched the default settings of Eclipse .

If you imported a project, check the folder (open through explorer ) and notice that it has a file named .classpath , open the file with an editor ( notepad , for example). Note that there will be a variable like this:

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>

Possibly if you switched from eclipse , this variable may change, causing the error message quoted not to find the "default" JRE of Eclipse . This error can be corrected by creating a new Java project in Eclipse itself, and then importing your project by unchecking the .project and .classpash files.

If you encountered this problem when trying to compile a code manually through prompt de comandos do Windows (CMD) , environment variables are likely to be misconfigured and the Java Development Kit (JDK) or Java Runtime (JRE) is incorrect. In this case, make sure that you have created a variable of type JAVA_HOME, and then set the value of it to the path of your JDK , in my case the default folder of JDK is:

  

C: \ Program Files \ Java \ jdk1.8.0_20

This variable serves as the programming language variables we use, JAVA_HOME will store the default path of my JAVA language build package.

After this, check that the environment variable CLASSPASH has the value:

  JAVA_HOME% \ lib \ tools.jar;% JAVA_HOME% \ lib \ dt.jar;% JAVA_HOME% \ lib \ htmlconverter.jar;% JAVA_HOME% \ jre \ lib;% JAVA_HOME% JAVA_HOME% \ jre \ lib \ rt.jar

It is clear that the operating system will change the value of the variable JAVA_HOME by the path of its JDK default within Windows , that is, C:\Program Files\Java\jdk1.8.0_20 .

Another important point is that if you compile your code via CMD , always remember to compile the classes in the correct order, that is, if ClasseA instantiates ClasseB , ClasseB must be compiled first, and ClasseA then. This way, it is also important to check if you are using package (folders), because if you do not specify the path to compile the files in the command, your default file bitecodes (files with% extension%) will itself compiling the code. So, if you are going to compile .class that belongs to ClasseA , and it uses pacote A that belongs to ClasseB , you must have this structure of folders, compile pacote B within ClasseB % (put the pacote B file inside the ClasseB.class folder), then compile the pacote B inside a ClasseA (put the pacote A file inside the ClasseA.class folder). In this case the pacote A is the main, then you execute the ClasseA file from it ( bitecode ) If you do not do this, the compiler will not be able to see the other folders, causing errors of type "Could not find class XXXXX".

    
24.08.2015 / 16:51
2

If I have been using command line to compile use: javac -cp /caminho/prodiretorio/src package.NomeDaClasse

Run:

java -cp /caminho/prodiretorio/src package.NomeDaClasse

Remembering which dependent classes should be compiled first.

    
16.10.2016 / 23:54
2

This error also occurs when you run your program in CMD with the extension .java.

c:\>java Exemplo.java
Erro: Não foi possível localizar nem carregar a classe principal Exemplo.java

Just run the program without the extension.

c:\>java Exemplo
Resultado com sucesso
    
26.03.2017 / 13:59
0

Navigate to the directory where your files are .class :

cd C:\Users\MeuNome\workspace\ProjetoX\bin\br\com\pacote

Execute the code below by passing the classpath as a reference:

java -classpath ../../.. br.com.pacote.ClassePrincipal

If it does not work with the relative path try with the absolute path:

java -classpath C:/Users/MeuNome/workspace/ProjetoX/bin/ br.com.pacote.ClassePrincipal
    
01.12.2017 / 01:03
-1

Very simple to solve this problem, of course if you installed java properly. Good is only you strip: (package xxxxxxx;) of the class and compile in CMD normally with: 1 ° (javac ClassName.java) 2 ° (java ClassName). It will run normally.

    
16.09.2016 / 17:36
-3

I tried to put the name of the class right, I was trying to put the name of the class everything small, then when I put it exactly as it was written it worked ... I did not put javac ... just java and the class name exactly as it is written .

    
19.09.2016 / 17:58