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

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

问题描述

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

代码如下:

import java.awt.*;导入 java.awt.event.*;导入 java.lang.*;导入 java.util.Random;公共类随机鼠标{公共静态无效主(字符串 [] args){for (int i=1; i<1000; i++) {随机 rand = new Random();int w = rand.nextInt(1024) + 1;int h = rand.nextInt(768) + 1;int t = rand.nextInt(2000) + 1;尝试 {机器人 r = 新机器人();r.mouseMove(w,h);线程.sleep(t);} catch (AWTException e) {}catch (InterruptedException e) {}catch (NullPointerException e) {}}}}

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

编译它

javac randommouse.java

这有效,当我使用

运行它时

java 随机鼠标

它也很好用.

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

jar cvf randommouse.jar randommouse.class

它的工作原理.然后我双击 jar 文件,它出现了一个错误 Java Exception.
然后我用

在 cmd 中运行它

java -jar randommouse.jar

并得到这个错误

F:Java>java -jar randommouse.jar线程main"中的异常 java.lang.NullPointerException在 sun.launcher.LauncherHelper.getMainClassFromJar(LauncherHelper.java:399)在 sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:463)F:Java>

我是否需要提出一个论点,如果需要,我应该把它放在什么地方以及如何提出?

提前致谢
山姆

解决方案

来自 JDK 文档:

<块引用>

为了使此选项起作用,JAR 文件的清单必须包含一行的形式

主类:类名

<块引用>

这里是类名标识具有 public static void main(String[] args) 的类作为应用程序起点的方法.看到罐子工具参考页面和 Java 教程的 Jar 路径有关使用 Jar 文件和 Jar 文件清单的信息.

当你使用这个选项时,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.

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.

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

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