开发者

NullPointerException when trying to run .jar file

开发者 https://www.devze.com 2023-04-04 02:00 出处:网络
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 ca开发者_如何学Cn make a ja

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 ca开发者_如何学Cn 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


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.


You have to set the entry point

 $> echo "Main-Class: randommouse" > Manifest
 $> jar cfm randommouse.jar Manifest randommouse.class 


Did you specify the entry point in the manifest?

http://download.oracle.com/javase/tutorial/deployment/jar/appman.html


A couple of issues with your code that are not related to your actual problem, but are important nevertheless.

1) This statement is unnecessary:

 import java.lang.*;

By default, every class in java.lang is implicitly imported. You don't need to do it explicitly.

2) This is dangerously bad code:

    try {  
            // ...
    } catch (AWTException e) {
    } catch (InterruptedException e) {
    } catch (NullPointerException e) {  
    }

You are catching exceptions that are most likely due to programming errors, and throwing away all evidence that they ever happened. At the very least, you should print out some kind of error message ... and the exception stacktrace to that you can diagnose it.

In this particular context (in a main method), the following is a better approach:

    try {  
            // ...
    } catch (Throwable e) {
        System.err.println("An unexpected error has occurred:");
        e.printStacktrace(System.err);
        System.exit(1); 
    }


I took a look at the source code of the class and it seems to try to get the main class attribute from a list of attributes, which are Strings, and is then invoking the trim() method on the found main class attribute. When the main class is not being specified, there is no main class attribute, which causes the searching method to return null to indicate so, and when trim() is being invoked on it, it is causing the NullPointerException since the searching method has returned null. To avoid this, be sure that the main class is specified in the jar manifest:

[directory of class files]>jar -cvmf [name of manifest] MyApp.jar

And be sure that you have written the manifest right (with the line break at the end):

Main-Class: [name of main class]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号