Issue
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();
};
}
});
}
}
Solution
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.
Answered By - Ashish
Answer Checked By - Robin (JavaFixing Admin)