尝试运行.jar文件时出现NullPointerException [英] NullPointerException when trying to run .jar file

查看:164
本文介绍了尝试运行.jar文件时出现NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习java,只知道少量代码,但这仍然是一个简单的程序。它更像是一个恶作剧程序,但主要是为了测试我是否可以制作一个jar文件。

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.

以下是代码:

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 的文件中,
然后使用

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

javac randommouse.java  

这是有效的,当我使用

java randommouse 

它也能正常工作。

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

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

jar cvf randommouse.jar randommouse.class 

它有效。然后我双击jar文件,它出现错误 Java异常

那么我在cmd中运行它

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

并收到此错误

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?

提前谢谢你们!
Sam

Thank you in advance
Sam

推荐答案

来自 JDK doc


为了使此选项有效,JAR文件的清单必须包含
格式的行

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



Main-Class: classname




这里,classname
标识具有public static void main(String [] args)
方法的类,该方法充当您的应用程序的初始点。有关使用Jar文件和Jar文件清单的
信息,请参阅Jar
工具参考页面和Java Tutorial的Jar跟踪。

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.

当您使用此选项时,JAR文件是所有用户
类的源,并忽略其他用户类路径设置。

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

这篇关于尝试运行.jar文件时出现NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆