尝试运行 .jar 文件时出现 NullPointerException
我刚刚开始学习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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自JDK 文档:
From the JDK doc:
您必须设置入口点
You have to set the entry point
您是否在清单中指定了入口点?
http://download.oracle.com/javase/tutorial/deployment/jar /appman.html
Did you specify the entry point in the manifest?
http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
您的代码有几个问题与您的实际问题无关,但仍然很重要。
1) 此语句是不必要的:
默认情况下,
java.lang
中的每个类都是隐式导入的。您不需要明确地执行此操作。2) 这是危险的糟糕代码:
您捕获的异常很可能是由于编程错误造成的,并且丢弃了它们曾经发生过的所有证据。至少,您应该打印出某种错误消息......以及可以诊断它的异常堆栈跟踪。
在这个特定的上下文中(在
main
方法中),以下是更好的方法:A couple of issues with your code that are not related to your actual problem, but are important nevertheless.
1) This statement is unnecessary:
By default, every class in
java.lang
is implicitly imported. You don't need to do it explicitly.2) This is dangerously bad code:
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:我查看了该类的源代码,它似乎尝试从属性列表(字符串)中获取主类属性,然后在找到的主类属性上调用 trim() 方法。当未指定主类时,没有主类属性,这会导致搜索方法返回 null 来指示这一点,并且当对其调用 trim() 时,会导致 NullPointerException,因为搜索方法已返回无效的。为了避免这种情况,请确保在 jar 清单中指定主类:
[directory of class files]>jar -cvmf [name of manifest] MyApp.jar
并确保您已编写明显的权利(最后换行):
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):