Reputation: 1641
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?
Upvotes: 32
Views: 147976
Reputation: 111
You must have main function in your class. Like
public class MyDataBase {
public static void main(String args[]) {
}
}
Upvotes: 3
Reputation: 14724
I read so many blogs and tried so many tricks but my problem not resolved. I was able to run the code but not able to generate the jar file. :( Sad..
But I tried something which might be very silly but worked for me and bought eclipse on trace. What I did was.. Just deleted the main method from the class. Saved it. Did undo to bring the main class back. Tada... Issue resolved... Just one think would like to say, keep your eclipse in "Build Autometically" mode.
Upvotes: 0
Reputation: 644
Note: This worked in the past and I received many up votes. Perhaps this is not a solution anymore - but it once was - as the eclipse version was indicated.
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
Windows
-> Preferences
-> Java
-> Compiler
-> Building
-> Build Path Problems
Abort build when build path errors occur
toggleThis seems like a can of worms if you end up with other build path errors I image. So use with caution.
I found the solution to this here
Info
Upvotes: 4
Reputation: 11691
I had this issue after upgrading to the Eclipse 2019-12 release. Somehow the command line to launch the JVM got too long and I had to enable the jar-classpath option in the run configuration (right click on file -> run as -> run configs).
Upvotes: 0
Reputation: 763
.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
Upvotes: 36
Reputation: 21
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.
Upvotes: 0
Reputation: 307
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;
Upvotes: 2
Reputation: 472
This worked for me finally : RUN -> RUN CONFIGURATIONS -> DELETE THE RUN CONFIGURATION CLOSE ECLIPSE REOPEN ECLIPSE CREATE RUN CONFIGURATION AGAIN.
Tadaaaa !! It works
Upvotes: -1
Reputation: 59
I had this same problem in a Maven project. After creating the src/test/java folder within the project the error went away.
Upvotes: 1
Reputation: 427
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.
Upvotes: 0
Reputation: 19
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.
Upvotes: -1
Reputation: 42060
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.
.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:
.metadata
directory.Notes
Upvotes: 20
Reputation: 29
I had the same problem.I solved with following command maven:
mvn eclipse:eclipse -Dwtpversion=2.0
PS: My project is WTP plugin
Upvotes: -1
Reputation: 1
I had the same problem, this is my solution:
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.
Upvotes: -1
Reputation: 169
It is possible to have 2 groovy-xxx-all.jar files by excample in lib directory. which makes that an app is not running
Upvotes: -1
Reputation: 560
I solved my issue by doing this:
Strangely it started working again after that.
Upvotes: -1
Reputation: 767
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
Upvotes: 18
Reputation: 29
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();
}
Upvotes: 0
Reputation: 60818
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.
Upvotes: -1