My Eclipse (Indigo) was running just fine. I created a simple class Hello
. It is placed in package cont
in the folder ch13
. However, when I try to run it from Eclipse I get info from Java Virtual Machine Launcher:
Could not find the main class: cont.Hello.
I tried to run other classes from this package and they run just fine (that is the classes that existed there before). However any new class I create in this package has these problems. Actually any new class I create in Eclipse runs into this problems. I decided to check how it works from the command line. It seems that the problem still exist - I get same error. I checked the path and it is C:\Program Files\Java\jdk1.7.0_02\bin
, which is correct (note the other classes are running from Eclipse just fine). I tried to run with java -cp . Hello
and there are some Errors produced starting with java.lang.NoClassDefFoundError: Hello (wrong name: cont/Hello)
. Code itself is simple:
package cont;
public class Hello {
public static void main(String[] args){
System.out.println("Hello");
}
}
How can I fix it so that my classes still run under Eclipse?
.metadata
is corrupted.
Steps:
Warning: Deleting .metadata will delete all your Eclipse configurations, plugins, project setups. Make a backup before you attempt this!
Stop eclipse, delete .metadata in workspace and restart eclipse
Import Project
Run again
Removing the Run Configuration
Sometimes I have a similar problems in some pre-release versions of eclipse. For fix the error, I delete the Run Configuration. You can find that in menu Run, Run Configurations...
Then I launch the app with Alt+Shift+X, then J. If this don't work, Ctrl+F11.
Deleting the
.metadata
directoryIn another way, the configuration settings for your current workspace may are corrupted. Those settings are in the
.metadata
directory in your current workspace 1. In that case, there is no other choice than delete the directory:- Close eclipse.
- Delete the
.metadata
directory. - Start eclipse.
- Import the projects.
- Run the project again.
Notes
- You will see that directory with File > Switch Workspace > Other...
I have solved the issue following way:
Go to Run Configuration (Right Click on Java File->Run->Run Configuration).
Go to ClassPath Tab: Click on Advanced -> Add Folders -> Add bin directory (which has class file in it for Java source code)
Re run the code, now it will solve the issue. It worked for me
You must have main function in your class. Like
public class MyDataBase {
public static void main(String args[]) {
}
}
Problem
This can also be caused by a Java Build Path Problem.
In my case, I had a an error:
A cycle was detected in the build path of project {project}. The cycle consists of projects {x, y, z}.
This can occur when you include other projects in the build path of the project you wish to run. In fact, all the projects will fail to run with the error
Could not find the main class: Example.class
Solution
Open
Windows
-> Preferences
-> Java
-> Compiler
-> Building
-> Build Path Problems
Uncheck the Abort build when build path errors occur
toggle
This seems like a can of worms if you end up with other build path errors I image. So use with caution.
- Note: This only works if you have a "cycle error". This error message can be found in the "Markers" tab
I found the solution to this here
Info
- Java 1.8.0_152
- Eclipse Photon (June 2018)
I had this same problem in a Maven project. After creating the src/test/java folder within the project the error went away.
Renaming the main class should be enough (and easiest):
- Go to your class and set cursor to your class name;
- ALT + Shift + R and rename the class (build if not done automatically);
- You should be able to run it now;
- Rename the class to the old name if you want;
Another tip: I initialized static fields in a wrong order - surprisingly it didn't bring up a Problem (NullPointerException?), instead Eclipse complained with exactly the message OP posted. Correcting the static initialization order made the class run-able. Example:
private static ScriptEngineManager factory = null;
private static ScriptEngine engine = null;
static {
engine = factory.getEngineByName("JavaScript");
// factory is supposed to initialize FIRST
factory = new ScriptEngineManager();
}
I found the way to fix this problem was to rename the project. If you give it a name with strange characters (in my case, :), it will have trouble locating your class. I don't know if this rule applies to numbers, but try renaming the project or making a new one and copying the files. Name it without any numbers or special characters.
Move your file into a subdirectory called cont
Standard troubleshooting steps for Eclipse should include deleting and re-importing the project at some point, which when I have dealt with this error has worked.
I solved my issue by doing this:
- cut the entire main (CTRL X) out of the class (just for a few seconds),
- save the class file (CTRL S)
- paste the main back exactly at the same place (CTRL V)
Strangely it started working again after that.
In most cases and in my case there is just a depended Library missing. So make sure your project includes all depended Libraries (e.g. in a pom.xml if you are using maven) even if your class don't use them!
It is possible to have 2 groovy-xxx-all.jar files by excample in lib directory. which makes that an app is not running
I had the same problem, this is my solution:
- I manually deleted the bin folder of the project
- Then I refreshed the project which recompiled the whole project and created a new bin with all .class files
I did it because when I performed Clean(project->clean) my .class files were not getting deleted. the above solution works for me hope its useful to others.
I had the same problem.I solved with following command maven:
mvn eclipse:eclipse -Dwtpversion=2.0
PS: My project is WTP plugin
If you are using a pre-defined run configuration, go to classpath and try "Restore Default Entries". This will reconfigure the classpath for that configuration.
This worked for me finally : RUN -> RUN CONFIGURATIONS -> DELETE THE RUN CONFIGURATION CLOSE ECLIPSE REOPEN ECLIPSE CREATE RUN CONFIGURATION AGAIN.
Tadaaaa !! It works
I solved this error by closing the project, removing it from eclipse and then importing it again.
Might be a little simpler than to redo the whole workspace setup.
来源:https://stackoverflow.com/questions/10356854/eclipse-cant-find-load-main-class