15

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

G Boggs
  • 381
  • 1
  • 6
  • 19
  • Did you right-click on the class with the `main()` method and clicked "Run as Java application", or are you right-clicking on the project? In the latter it should propose all the classes with a `main()` method, though. – watery Sep 23 '14 at 19:39
  • 2
    Is your `main()` method static? – watery Sep 23 '14 at 19:40
  • 1
    How are you trying to run our code? Can we see where is it placed and how it looks like? – Pshemo Sep 23 '14 at 19:40
  • @watery Yes I have tried to run the code by right clicking the class and doing run as java app. And yes the main method is static. Code added. – G Boggs Sep 23 '14 at 19:48
  • Can we see what the error message is when you try to run the program? And also the launch() function? – DucRP Sep 23 '14 at 19:52
  • @Duc added the error, and there is no launch() function. The tag was supposed to say the code wouldnt launch – G Boggs Sep 23 '14 at 19:55
  • Is the package declaration missing from above, or you just didn't copy it? – watery Sep 23 '14 at 19:55
  • @watery sorry I just forgot to copy it – G Boggs Sep 23 '14 at 19:57
  • @GBoggs I think there is a problem in your Run Configuration in Eclipse – Kumar Abhinav Sep 23 '14 at 20:43

26 Answers26

14

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

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • 5
    Unfortunately this did not work. These settings have already been set, yet it still gives me the error that it cannot find/load the main class. – G Boggs Sep 23 '14 at 20:51
  • @GBoggs While running the java class,you have to select your newly created configuration and then click Run from configuration – Kumar Abhinav Sep 23 '14 at 20:53
  • @GBoggs Eclipse sometimes continues to run the Java class with old configuration.Simply clicking the Run method from your java class will be of no help I guess – Kumar Abhinav Sep 23 '14 at 20:55
  • 1
    I tried it with the new configurations, but it still didnt work. The weird thing is, my project icon shows a red X (indicating an error), but there are no errors to be found within my code. – G Boggs Sep 23 '14 at 20:58
  • @GBoggs why dont you post the snapshot from your eclipse?It will make the problem clearer – Kumar Abhinav Sep 23 '14 at 20:59
  • @GBoggs The image should contain the directory structure of your main class and the Project ,along with your run configuraton – Kumar Abhinav Sep 23 '14 at 21:01
11

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
Irshad
  • 3,071
  • 5
  • 30
  • 51
Frank Jiao
  • 119
  • 1
  • 4
7

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

6

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

Yellaiah D
  • 69
  • 1
  • 2
5

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"

Denis C de Azevedo
  • 6,276
  • 2
  • 32
  • 49
Yani
  • 51
  • 5
4

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.

4

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

Edy Aguirre
  • 2,113
  • 20
  • 20
  • I was having this issue and it was looking absurd to me in Eclipse as manually it can easily be solved. I have been looking around all the responses which look too much technical and attractive and your answer was so simple so initially I ignored it but truly it worked for me. Thanks You a Lot! – Fundi Jul 27 '23 at 11:49
3

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.

NickyPatel
  • 503
  • 4
  • 11
1

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

sguha
  • 11
  • 2
1

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.

vinsinraw
  • 2,003
  • 1
  • 16
  • 18
1

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!

k.horde
  • 149
  • 3
  • 12
0

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.

Joi Pu
  • 1
  • 1
0

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

0

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!

0

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!

0

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.

sudar
  • 1,446
  • 2
  • 14
  • 26
0

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.

0

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.

0

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

0

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.

Tamias
  • 173
  • 9
0

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.

Daniel Bickler
  • 1,119
  • 13
  • 29
dhamu
  • 605
  • 1
  • 7
  • 17
0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ram
  • 3,887
  • 4
  • 27
  • 49
0

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.

user3755282
  • 813
  • 2
  • 9
  • 15
0

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>
Prateek Joshi
  • 3,929
  • 3
  • 41
  • 51
0

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

Saurabh Gupta
  • 139
  • 2
  • 4
  • 13
-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