尝试运行 .jar 文件时出现 NullPointerException

发布于 2024-12-03 14:33:07 字数 1619 浏览 6 评论 0原文

我刚刚开始学习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 的文件中, 然后使用 This Works 编译它

javac randommouse.java  

,当我使用它运行它时,

java randommouse 

它也工作得很好。

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

jar cvf randommouse.jar randommouse.class 

并且它有效。然后我双击 jar 文件,它出现错误 Java Exception
然后我在 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>

我是否需要放入参数,如果需要,我应该将其放在哪里以及如何放入?

预先感谢您
山姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪异←思 2024-12-10 14:33:07

来自JDK 文档

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

形式

Main-Class: classname

这里,类名
标识具有 public static void main(String[] args) 的类
用作应用程序起点的方法。见罐子
工具参考页面和 Java 教程的 Jar 轨迹
有关使用 Jar 文件和 Jar 文件清单的信息。

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

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.

沐歌 2024-12-10 14:33:07

您必须设置入口点

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

You have to set the entry point

 
gt; echo "Main-Class: randommouse" > Manifest
 
gt; jar cfm randommouse.jar Manifest randommouse.class 
小梨窩很甜 2024-12-10 14:33:07

您的代码有几个问题与您的实际问题无关,但仍然很重要。

1) 此语句是不必要的:

 import java.lang.*;

默认情况下,java.lang 中的每个类都是隐式导入的。您不需要明确地执行此操作。

2) 这是危险的糟糕代码:

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

您捕获的异常很可能是由于编程错误造成的,并且丢弃了它们曾经发生过的所有证据。至少,您应该打印出某种错误消息......以及可以诊断它的异常堆栈跟踪。

在这个特定的上下文中(在 main 方法中),以下是更好的方法:

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

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); 
    }
我也只是我 2024-12-10 14:33:07

我查看了该类的源代码,它似乎尝试从属性列表(字符串)中获取主类属性,然后在找到的主类属性上调用 trim() 方法。当未指定主类时,没有主类属性,这会导致搜索方法返回 null 来指示这一点,并且当对其调用 trim() 时,会导致 NullPointerException,因为搜索方法已返回无效的。为了避免这种情况,请确保在 jar 清单中指定主类:

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

并确保您已编写明显的权利(最后换行):

Main-Class: [name of main class]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文