java – NullPointerException当尝试运行.jar文件

前端之家收集整理的这篇文章主要介绍了java – NullPointerException当尝试运行.jar文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚开始学习 java,只知道少量的代码,但是这仍然是一个简单的程序.这更是一个恶作剧程序,但大多只是测试我是否可以制作一个jar文件.

这是代码

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) {}  
        }  
    }  
}

我将其保存到名为randommouse.java的文件中,
然后使用它进行编译

javac randommouse.java

这是有效的,当我运行它

java randommouse

它也很好.

所以我尝试创建一个jar文件.我使用命令

jar cvf randommouse.jar randommouse.class

它的工作原理之后,我双击jar文件,并出现错误Java异常.
所以我用cmd运行它

java -jar randommouse.jar

并得到这个错误

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>

我需要提出一个论据,如果是,我该怎么做,怎么办?

先谢谢你
山姆

解决方法

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.

原文链接:https://www.f2er.com/java/123228.html

猜你在找的Java相关文章