javaeclipsemainclass

Error: Cannot find or load main class only when running outside of eclipse


I've made a Java application which runs fine inside of eclipse problem is, when I export it as a jar file and then attempt to run that jar file via windows cmd(current directory is the same as that of where I exported) with java "2Dwars 2.0.jar"

it says Error: Could not find or load main class 2Dwars 2.0.jar

I've searched google, and stackoverflow extensively, a lot of people are having similar problems, but none a similar solution.

my main class is as follows:

public class MainClass extends JFrame implements Controll.keyPressEvent
{

    static Render render;
    public init in;

    public MainClass()
    {
        new Options();

        setUndecorated(true);
        Options.setBoolean("Draw chunks", false);
        System.out.println("Test");

        add(new Render(new Controll()));

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(895, 675);

        setLocationRelativeTo(null);
        setTitle("2D wars");
        setResizable(false);
        setVisible(true);

        System.out.println("Adding render");


        System.out.println("Loaded Options.");
        System.out.println("added render");
        init in=new init();

        Controll.addListener(Controll.Key.ESC, this);

        new Sounds();   
    }

    public static void main(String[] args) 
    {           
        new MainClass();

    }

    @Override
    public void sendKeyPressEvent(KeyEvent e)
    {
        if(e.getKeyCode()==Controll.Key.ESC.getKeyCode())
        {
            System.exit(1);
        }
    }

}

System specs:

Windows vista 32 bit.
Jre 1.7 32 bit
JDk 5,6,7.

EDIT the issue can been resolved by exporting as a runnable and adding -jar to the command arguements, for some reason the jar had to be exported as a Runnable Jar to find the main class.


Solution

  • To run it as a jar file, you need the -jar option. Something like:

    java -jar "2Dwars 2.0.jar"
    

    (I'd suggest using a filename which doesn't have spaces in for simplicity's sake, too.)