G Boggs
G Boggs

Reputation: 391

Eclipse error "Could not find or load main class"

I know there are many duplicates of this question, but I have looked at them all, and none of them have solved the issue.

I am trying to run a class that has a main function. I have cleaned the project, checked the classpath for '.', added the bin folder to the classpath under run configurations. I'm not sure what else to try because the class is certainly in the source folder.

Could someone please help me with this issue?

package testIt;

public class MemoryVisualizerApp extends Application{

public static void main(String[] args) {
    launch(args);
}

//Setup the scene and launch with given properties
@Override
public void start(Stage primaryStage) throws IOException{
    Parent root = FXMLLoader.load(getClass().getResource("/MemoryVisualizer.fxml"));
    Scene scene = new Scene(root, 650, 300);
    //Set whether the screen should be re-sizable (possibly best size = default)
    primaryStage.setResizable(true);
    primaryStage.setMinHeight(300);
    primaryStage.setMinWidth(550);
    primaryStage.setTitle("Memory Usage");
    primaryStage.setScene(scene);
    scene.getStylesheets().add("testIt/MemoryVisualizer.css");
    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() 
    {
        public void handle(WindowEvent e){
              /*Currently the threads continue running after window is closed. Looking for
              a way to stop the app threads when window closed without stopping the program.*/
        }
    });
    primaryStage.show();
    primaryStage.show();
}

}

This code is located within a package, withing the src folder. It utilizes some JavaFX files that arent shown but that shouldnt be an issue.

This is the error: Error: Could not find or load main class testIt.MemoryVisualizerApp

Upvotes: 15

Views: 133764

Answers (26)

Ashish Kumar Mondal
Ashish Kumar Mondal

Reputation: 459

I have checked all possible solutions including re-installation of JDK, eclipse as it worked for many people but unfortunately it didn't work for me. Also updated main class in pom.xml but it didn't help. It took my sleep away. I was trying different options and suddenly the magic happened when I checked this checkbox. It might be helpful for many people so thought of sharing enter image description here

Upvotes: 7

Saurabh Gupta
Saurabh Gupta

Reputation: 139

If you are using maven Project...

First please clean your project. then installed maven- if build success then run your project.

then project will be working fine.

thanks

Upvotes: 0

Prateek Joshi
Prateek Joshi

Reputation: 4067

I faced the similar problem, a workaround is to open up a terminal, go to project folder and run

java cp target/<your_jar_artifact.jar> <com.your_package.YourMainClass>

Upvotes: 0

user3755282
user3755282

Reputation: 883

I had similar problem, my standalone java program was not working. I had other compilation error in my workspace in other unrelated java files. After removing all compilation errors I could run my standalone java program.

Upvotes: 0

kapishreshth
kapishreshth

Reputation: 4098

I came across the same problem. You might get this error in many situations like below.

  • Some dependency problem in your pom.xml
  • The run configuration may not have proper configuration. You might want to delete the existing one and create the new one like suggested in other answers.
  • You may want to try like mvn clean, mvn build and other options.
  • You may want to delete the project and import it again.
  • You may want to restart Eclipse and check your luck if it works.

Here, the list of hit and trial goes on. Believe me the tool Eclipse is not that stupid. It is quite smart to find errors even at compile time. I tried all ways mentioned above, but none worked for me.

The main problem which worked for me was JRE version which was somehow got set to default (JRE System Library [jre1.5]). I reset it to jre1.8.* which I had installed in my machine.

I don't commit that this is only the solution to this problem but yes this could be one of the solution. So keep an eye on JRE version.

Upvotes: 0

NickyPatel
NickyPatel

Reputation: 565

I was having the same problem. I was having a maven project. According to my knowledge, it was because i had cleaned the project and there were no class files generated. I did mvn compile and it was started running.

Upvotes: 3

dhamu
dhamu

Reputation: 695

Usually cleaning and rebuilding the project from Eclipse does the trick.

However, when using Maven I have discovered times this does not work and I've noticed the 'target' folder of my maven project does not have anything under it. Running 'mvn clean install' from the command line and refreshing the project in eclipse does the trick.

Upvotes: 0

Tamias
Tamias

Reputation: 311

I followed several of the less drastic suggestion here to no avail. Then it dawned on me that Eclipse was confused because I had made a backup copy of my src project folder. (It was compiling all these "copy of" folders' *.java files and I noticed the subfolders for them in my bin folder.) I moved the backup elsewhere, got rid of the extraneous class files, did a Project > Clean, and everything was back to normal.

Upvotes: 0

suresh reddy
suresh reddy

Reputation: 1

i had same problem but in my case firewall was blocking java directory path, make sure system firewall is allowing java directory path access.

Upvotes: 0

Nidal Mujtaba
Nidal Mujtaba

Reputation: 1

I ran into the same error today, I refactored my project name and then refactored it back to the old name and it ran just fine after that.

Upvotes: 0

user3553070
user3553070

Reputation: 45

Try to check first in the Problems window the errors you have. It is most likely it. Then check in the Configure Build Path... window under Order and Export for errors in jars and libraries (missing, duplicates, etc.).

Fixing the problems will fix the class-loading issue.

Upvotes: 0

nps
nps

Reputation: 384

I was also getting same message.That is due to the workspace problem.Create one new workspace and try there once. I think it will works.

Upvotes: 0

Darlysson Felipe
Darlysson Felipe

Reputation: 11

I was getting the same message and I solve the problem moving out two dependencies from my pom.xml called "org.slf4j". After that It worked perfectly!

Upvotes: 0

Matthew Song Loong
Matthew Song Loong

Reputation: 21

Had the same problem, struggling with it for a while: couldn't find main class.

I did Project -> Clean -> Clean all Projects

Cleaned everything, and tada, it compiled!

Upvotes: 0

k.horde
k.horde

Reputation: 149

I had the same issue and I could not start any project with main() methods.

I tried almost everything mentioned above, but it turned out that I had a little mistake in my pom.xml file.

You can check this file too!

Upvotes: 1

vinsinraw
vinsinraw

Reputation: 2135

I also had the same issue and was getting "Error: Could not find or load main class ...."

I solved it as follows:

  1. Right clicked on my Java main class > Run As > Run Configurations.

  2. In the resultant popup box and Under Java Application your java file name would be selected mode... if not select it

  3. On the right side of the same box and under Classpath tab Clear all Bootstrap and User Entries (if any)
  4. Under Bootstrap Entries Click Advanced Button > Select Add Library Radio and click OK. In next "Add Library" box Select JRE System Library and provide your correct JRE location and click Finish. (If it is not visible then u have to add it by clicking Installed JRE > Add button). Now you can see JRE System Library under BootStrap Entries

  5. Next User Entries > Click Add Projects Button > And then under Project Selection box check on your Project name > OK

The main java file should now work. Hope this may help someone who would be facing similar issues.

Upvotes: 1

Tom Rusky
Tom Rusky

Reputation: 1

Removing the JRE System Library and adding the default one worked for me.

Upvotes: 0

sguha
sguha

Reputation: 11

I had the same problem. In my case I did the following, to resolve it.

  1. Select the project in the Eclipse IDE, right click, and open properties dialog.
  2. Select "Java Build Path" item on the left panel of the Properties dialog.
  3. Select the "Libraries" tab, on the right hand panel.
  4. Remove the libraries (jars), which are marked red, or fix the path.
  5. Save the Properties dialog. After removing the "Not found" libraries, Eclipse run/debug started working. The "Not found" message would also show up in the "Problems" pane. which you can open using, Window->Show View->Problems, command sequence. This pane gives you useful messages. The same message might show up for many reasons, other than what I encountered.

Observation: The classes directory in my case was not created, which means that the javac failed. On command line also, javac works the same way, if you have multiple java files in the same invocation of javac, and one java file encounters a problem, then no class files are created.

Which means Eclipse puts all the java files in the project with single invocation of javac.

Now if there are no class files generated, obviously, the message now makes sense. The compile errors are also shown in the "Problems" pane.

Now this would be the case if there is a compile error. If just a jar file which is not used at all, is on the classpath, there should not be any problem.

Even if my project had problems, I ignored it, because I was just testing a single class, and was thinking that only that file would get compiled and run, because it was a standalone test, without any dependencies.

But that is not the case with Eclipse. I wish they did it that way. As you can see lot of people got burnt this way.

Thanks

Upvotes: 1

Edy Aguirre
Edy Aguirre

Reputation: 2143

you should add bin folder - run->run Configuration tab classpath advanced button - add folder - choose bin folder (yourproyect/bin)

Upvotes: 4

Yellaiah D
Yellaiah D

Reputation: 69

I too faced same problem.

Simple solution is : delete project( not source files please). close eclipse--> go to project folder-- > remove classpath and project file .

then open Eclipse and create project in same location

Upvotes: 6

Frank Jiao
Frank Jiao

Reputation: 129

If all the solution does not work for you, it may be caused by missing libraries:

  1. Right click your project, Build Path --> Configure Build Path --> Java Build Path --> Libraries
  2. Remove the missing libraries
  3. Go to your main class and run it

Upvotes: 12

Brendan
Brendan

Reputation: 1

Found This while searching, and it was after I updated my Java that the problem seemed to occur.

In Eclipse from your project:

Right-click on your project
Click Properties
Java build path: Libraries; Remove the "JRE System Library[J2SE 1.4]"
Click Add Library -> JRE System Library
Select the new "Execution Environment" or Workspace default JRE

Upvotes: -1

Joi Pu
Joi Pu

Reputation: 1

I just encountered the same problem as yours and I followed Kumar's directions and it worked out. One thing to add: you need to make sure that after you go to the correct main class, check the "Classpath" and your project name should be existed under the "User Entries". If not, add your project to the directory then you're good to go.

Upvotes: 0

Niroshan Abeywickrama
Niroshan Abeywickrama

Reputation: 99

Follow these steps :

1)Delete your current run configuration. 2)Go to the main class and run it by right clicking the mouse-->Run As-->Java Application.

Then you will see an auto generated new run configuration.

Upvotes: 4

Yani
Yani

Reputation: 51

Try to point the classpathentry output path in the .classpath file to target/classes The classpath file is located in the project folder. Edit it manually.

In my case, it was like this before:

classpathentry kind="output" path="bin"

I changed it to:

classpathentry kind="output" path="target/classes"

Upvotes: 5

Kumar Abhinav
Kumar Abhinav

Reputation: 6675

If you are facing similar problem,it has to do with the "Run Configurations" not working properly in Eclipse.

error1

Please right clik on your java class containing the main method .Go to Run As-> Run Configurations and put the correct patrameters.Project should be the Project which contains the main class and Main class should be fuly qualified class name.See the snapshots below for clarity.It should wok fine then.Basically,you are creating a Run configuration and you can use that saved configuration for later use also.

selecting run configurationparameters to be entered correctly

Upvotes: 14

Related Questions