0

Yesterday I uploaded some project to my github It compilled fine, Today I'm trying to clone it from other computer, I followoed this guide, but when I'm trying to run the code I'm getting:

Error: Could not find or load main class gui.MainScreen Caused by: java.lang.ClassNotFoundException: gui.MainScreen

I tried to do like this post, but I didn't understand his answer, can someone please post clearer answer? Screenshots will help a lot.

Code roughly as follows:

   package gui;


    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class MainScreen extends JFrame
    {   
    public MainScreen() throws IOException
    {   
    ....
        this.pack();
        this.setVisible(true);
    }
    }

    public static void main(String[] args)
    {
        //avoid blocking the main thread
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {

                try {

                    new MainScreen();
                } catch (IOException e) {
                    e.printStackTrace();
                };
            }           
        });


    }
}   

enter image description here

Codey
  • 487
  • 1
  • 8
  • 27

3 Answers3

0

You need to have the main class in a separate file. From your screenshot, it looks like you have the main class within the MainScreen class. You can name the file that has the main class anything you want (don't name it main though, just to avoid confusion).

If your main class is indeed in a separate file, set the main class after cloning your project in eclipse. Check this link How to set the main class in Eclipse. I am not sure that information is present in the github project that you cloned. Go to Run Configurations and setup the fully qualified main class name and as the link says, you can also search the whole project, which will give you the main class. Set that and then run the project. Hopefully, that will work.

Ashish
  • 209
  • 1
  • 10
  • "You need to have the main class in a separate file" not corrent. 1. The code runs fine from the computer that commited the project, even that the main isn't separate file. 2.I tried to move the main to separate file and I'm getting same error – Codey Jun 18 '19 at 06:05
  • Hi Codey. Then, can you try the second way that I mentioned. By opening Run Configurations in eclipse and then setting the main class by clicking Search. It should bring up your class which contains the main method. Select that, and hopefully, it should work. – Ashish Jun 18 '19 at 06:07
  • The main class cofigured as MainScreen – Codey Jun 18 '19 at 06:09
  • Okay. Can you please post your Run Configuration screenshot with the main class set? That would be really helpful in us solving your problem. Thank you. – Ashish Jun 18 '19 at 06:12
  • Can you please post what does the class with Main contain? I can see that in the left hand side that there are two classes - Main and MainScreen. What does the class with Main contain? Please post a screenshot here. – Ashish Jun 18 '19 at 06:25
  • package gui; public class Main { public static void main(String[] args) { System.out.println("test"); } } – Codey Jun 18 '19 at 06:30
  • Okay. That means you have two main classes. Can you remove the main method in the MainScreen class and copy that to the Main.java class, set Main.java as the Main class in Run Configuration and then run the project? Basically, the idea here is to have one main class in your project. The current Main.java class does nothing in the main method except printing test. – Ashish Jun 18 '19 at 06:33
  • @Tried to remove main method in MainScreen and then running the project, gives the same error – Codey Jun 18 '19 at 06:38
  • Did you set the Main.java class as the main class now in Run Configuration? You also had to remove the main method from MainScreen class and copy it to the Main.java class, open Run Configuration and set Main.java as the main class. – Ashish Jun 18 '19 at 06:44
  • I have created the following pastebins with the classes Main.java and MainScreen.java - https://pastebin.com/vFFyxLyY, https://pastebin.com/nUmrFtNa – Ashish Jun 18 '19 at 06:51
0

I do not know if this answer will be beneficial to you or not but the same issue happened with me. To fix this what I did was just deleted the project from workspace and again imported it back from the git folder and everything started working fine.

0

My suggestion for those who encounter this problem, is to create a new draft project in Eclipse, see that it's running well, and then compare it to the

project > preference > java build path

that there is there (by edit you can see the settings of the working project vs the not working).

You can use screenshots to be sure.

When we are cloning a project it makes sense that some setting of the project are not fit to our local Eclipse setting, so we need to handle that.

lingar
  • 477
  • 1
  • 7
  • 25