Error: Unable to find or load the main class in Java (Eclipse or CMD)?

Asked

Viewed 91,443 times

9

If you ever came across the error message: Erro: não é possível localizar nem carregar a classe principal you probably won’t be able to run your code 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 come across this message in the "CMD" itself? If yes, how to fix this problem?

  • Checked if you placed the input method public static void main(String... args)?

7 answers

10


First of all, if you come across an error like this in Eclipse, you probably imported some project from your *workspace* or anywhere else in your Windows or you’ve tampered with the default settings of Eclipse.

If you imported a project, check in the folder (open through explorer) and realize that you have a file with the name .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 changed version of Eclipse, this variable may change, causing the error message cited for not finding the JRE’s "default" Eclipse. This error can be fixed by creating a new Java project in Eclipse itself, and after that importing your project by unchecking the files .project and .classpath.

If you encountered this problem while trying to compile a code manually through the prompt de comandos do Windows (CMD) environment variables are likely to be poorly configured and JDK (Java Development Kit) or JRE (Java Runtime) are incorrect.

In this case, check if you have created a JAVA_HOME type variable, and then set the path of your path as the same value 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, the JAVA_HOME will store the default path of my JAVA language compilation package.

After that, check the environment variable CLASSPATH holds the value:

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

This clear that the operating system will change the value of the variable JAVA_HOME by the way of his JDK standard within the Windows, that is to say, C:\Program Files\Java\jdk1.8.0_20.

Another important observation is that if you embed your code via CMD, always remember to compile in the correct order the classes, ie if the ClasseA urge to ClasseB, to ClasseB should be compiled first, and ClasseA next.

This way, it is also important to check if you are using package (folders), because if you do not indicate the build path of the files in the command, your bitecodes (files with extension .class) default will be inside the folder itself that is compiling the code. So, if you build the ClasseA that belongs to the pacote A, and it uses the ClasseB that belongs to the pacote B, you must own this folder structure, compile the ClasseB within a pacote B (put the file ClasseB.class inside the briefcase pacote B), then compile the ClasseA within a pacote A (put the file ClasseA.class inside the briefcase pacote A). In that case the ClasseA is the main one, so you run the file from bitecode from her ($ java ClasseA)

If you don’t, the compiler won’t be able to see the other folders, causing errors like "Couldn’t find class XXXXX".

  • JAVA_HOME is deprecated.

4

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

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

2

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

Execute:

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

Remembering that dependent classes should be compiled first.

0

Navigate to the directory where your files are .class:

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

Run the code below by passing the classpath as reference:

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

If it doesn’t work with the relative path try with the absolute path:

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

0

It was with the same problem, I decided to remove the environment variable classpath. In my case (Linux) it pointed to "/usr/local/java/jdk1.8.0_261/lib", from what I understood when trying to run my class Java was looking in the path indicated by classpath.

  • In this case you should not remove, but exchange for ".:/usr(...)" where . indicates the local directory (remembering that in the case of Windows instead of separating with : is with ;).

  • No need to remove. Just Editar this environmental variable.

-1

Very simple to solve this problem, of course if you have installed java correctly. Well just take : (package xxxxxxx;) from class and compile in CMD normally with : 1° (javac Classname.java) 2° (java Classname). He will perform normally.

  • Here it worked! vlw!

-3

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

Browser other questions tagged

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