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".