1

I am working with Eclipse and I've been doing it since several months ago without any problem. Now I've created a new class in a certain package, but Eclipse does not compile it. So I tried to create a new simple class:

public class Test {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

But I get always this error:

Errore: impossibile trovare o caricare la classe principale Test (Error: impossible to find or load main class Test)

I compiled it with cmd: in that case, Eclipse can run the java file, but it can't create the .class file with new edits; it just launches the last version of the compiled file.

I tried to uninstall and reinstall Eclipse, but still got this problem.

sirdan
  • 1,018
  • 2
  • 13
  • 34
  • Possible duplicate of [Eclipse "Error: Could not find or load main class"](http://stackoverflow.com/questions/11235827/eclipse-error-could-not-find-or-load-main-class) – Spock May 12 '17 at 15:29

3 Answers3

2

If you create a java class with public static void main(String[] args), Eclipse will run that main method for you by right clicking on the file itself, or on the file in the project explorer, then choosing:

"Run As" -> "Java Application."

Once you do this, Eclipse stores information about your class, so you can easily run the class again from the Run As menu (Green Play Button on the toolbar) or from the Run Configurations dialog.

If you subsequently MOVE the java class (manually, or however), then again choose

"Run As" -> "Java Application,"

from the new location, Eclipse will run the original stored configuration, attempt to invoke this class from its original location, which causes this error.

For me, the fix was to go to the run configurations, (Green Play Button -> Run Configurations) and remove all references to the class. The next time you run

"Run As" -> "Java Application"

Eclipse will write a new configuration for the moved class, and the error will go away.

Original Source of this answer

Community
  • 1
  • 1
eigenharsha
  • 2,051
  • 1
  • 23
  • 32
  • not working, the thing is that it does not create class files, so there's no way to run the java file, even if I go to run as-java application – sirdan May 12 '17 at 15:35
  • check these settings : (may be it usefull) Project > Properties > Project Facets > java (selected) and Windows > Preferences > Java > Installed JREs > (JDK path || you configure your JDK path – eigenharsha May 12 '17 at 15:47
  • done it, but no positive effects on the result, it keeps saying that it can't find or load the main class. I'm going crazy, it was okay just one hour ago – sirdan May 12 '17 at 15:55
  • can you check JRE System Library in eclipse on Project > Properties > Java Build Path > Libraries (if No then Add Library > JRE System Library) – eigenharsha May 12 '17 at 16:04
  • there is JRE System Library [JavaSE-1.8] with a number of jar under. Is that correct? – sirdan May 12 '17 at 16:08
  • correct ! class not found error generally occur when your JDK is not properly configured in eclipse (JRE library not in your classpath). – eigenharsha May 12 '17 at 16:14
  • ok, so it's all set just like before. But why it used to compile before, now it does not? How do i solve the issue? – sirdan May 12 '17 at 16:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144093/discussion-between-eigenharsha-and-sirdan). – eigenharsha May 12 '17 at 18:18
1

have you tried to change JDK? be sure to download the last version

Fausto
  • 183
  • 12
  • I did, but nothing changed – sirdan May 12 '17 at 15:12
  • be sure to change everything: eclipse, jdk, clean temp folders, change workspace – Fausto May 12 '17 at 15:13
  • ciao fausto scusa se scrivo in italiano, comunque ho avuto anche difficoltà a disinstallarlo, ma alla fine ci sono riuscito da help/about/ ho selezionato tutto e disinstallato, poi ho reinstallato ed utilizzato lo stesso workspace, in quanto contiene tutto il mio lavoro. Come mi consigli di procedere? – sirdan May 12 '17 at 15:14
  • eclipse non necessita di essere disinstallato a meno che non lo hai scaricato tramit un installer, cosa che sconsiglio. dovresti scaricare un nuovo eclipse tramite il zip e usare quello nuovo su un nuovo workspace con una nuova JDK(insomma riscarica i file: TUTTI e utilizza quelli nuovi). se nemmeno così va allora è proprio grave. i progetti li recuperi, al massimo li esporti come war/ear/jar oppure crei dei repository su siti come github per salvare il lavoro effettuato, ma dobbiamo capire se il problema è eclipse, il workspace o la JDK. p.s non è permesso parlare in altri linguaggi(solo ing) – Fausto May 12 '17 at 15:21
  • (Sorry for the italian) So, let's do this as clean as possible: I've downloaded the zip, how do I remove Eclipse in order to use the zip? – sirdan May 12 '17 at 15:26
  • Ok, I've got Eclipse reset, but it keeps not compiling – sirdan May 12 '17 at 15:52
  • I had similar problems (the class files were not generated). In the "Compiler compliance level", I had 1.7, but my jre was 1.8 – BOC Aug 17 '18 at 15:21
0

I solved by deleting everything I had on Eclipse, but when I opened a new Eclipse it gave me the error. So I created new projects, by importing one by one the single java files, then it worked.

sirdan
  • 1,018
  • 2
  • 13
  • 34