加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:10:49 所属栏目:Java 来源:网络整理
导读:我刚刚开始学习 java,只知道少量的代码,但是这仍然是一个简单的程序.这更是一个恶作剧程序,但大多只是测试我是否可以制作一个jar文件. 这是代码: import java.awt.*; import java.awt.event.*; import java.lang.*; import java.util.Random; public class
我刚刚开始学习 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读