0

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.

Caelum
  • 801
  • 2
  • 10
  • 19

1 Answers1

1

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Ty, this worked, but I was using the cmd console because exporting it as a runnable did not work, why is this? – Caelum Aug 29 '13 at 20:44
  • to elaborate, when I export it as a runnable jar running it will not create a new process. – Caelum Aug 29 '13 at 20:50
  • @user2104648: I don't understand what you mean. The export will just create a jar file, and running that jar file will definitely create a new process. It may not *work*, but that's a different matter. – Jon Skeet Aug 30 '13 at 05:44