Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when trying to run .jar file

I have just started learning java, and know only a small amount of code, however this is still a simple program. It is more of a prank program, but mostly just to test if I can make a jar file.

Here is the code:

import java.awt.*;  
import java.awt.event.*;  
import java.lang.*;  
import java.util.Random;  
public class randommouse {  
    public static void main(String[] args) {  
        for (int i=1; i<1000; i++) {  
            Random rand = new Random();  
            int w = rand.nextInt(1024) + 1;  
            int h = rand.nextInt(768) + 1;  
            int t = rand.nextInt(2000) + 1;  
            try {  
                Robot r = new Robot();  
                r.mouseMove(w,h);  
                Thread.sleep(t);  
            } catch (AWTException e) {}  
            catch (InterruptedException e) {}  
            catch (NullPointerException e) {}  
        }  
    }  
}  

I save this to file called randommouse.java, then compile it using

javac randommouse.java  

This works and when I run it using

java randommouse 

it works fine also.

So then I try to create a jar file. I use the command

jar cvf randommouse.jar randommouse.class 

and it works. Afterwards I double click the jar file and it comes up with an error Java Exception.
So then I run it in the cmd with

java -jar randommouse.jar

and get this error

F:\Java>java -jar randommouse.jar
Exception in thread "main" java.lang.NullPointerException
        at sun.launcher.LauncherHelper.getMainClassFromJar(LauncherHelper.java:3
99)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:463)

F:\Java>

Do I need to put in an argument, and if so where do I put that in and how?

Thank you in advance
Sam

like image 581
Sam Avatar asked Sep 09 '11 23:09

Sam


1 Answers

From the JDK doc:

In order for this option to work, the manifest of the JAR file must contain a line of the form

Main-Class: classname

Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

like image 143
Ed Staub Avatar answered Sep 20 '22 16:09

Ed Staub