java 尝试运行 .jar 文件时出现 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7368349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NullPointerException when trying to run .jar file
提问by Sam
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.
我刚开始学习java,只知道很少的代码,但这仍然是一个简单的程序。它更像是一个恶作剧程序,但主要是为了测试我是否可以制作一个 jar 文件。
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
我将其保存到名为 的文件中randommouse.java
,然后使用
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 文件。我使用命令
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
它有效。之后我双击 jar 文件,它出现了一个错误Java Exception
。
然后我在 cmd 中运行它
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
提前谢谢
山姆
回答by Ed Staub
From the JDK doc:
从JDK 文档:
In order for this option to work, the manifest of the JAR file must contain a line of the form
为了使此选项起作用,JAR 文件的清单必须包含以下形式的一行
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.
在这里, classname 标识具有 public static void main(String[] args) 方法的类,该方法用作应用程序的起点。有关使用 Jar 文件和 Jar 文件清单的信息,请参阅 Jar 工具参考页面和 Java 教程的 Jar 跟踪。
使用此选项时,JAR 文件是所有用户类的来源,其他用户类路径设置将被忽略。
回答by Fu86
You have to set the entry point
你必须设置入口点
$> echo "Main-Class: randommouse" > Manifest
$> jar cfm randommouse.jar Manifest randommouse.class
回答by Anthony
Did you specify the entry point in the manifest?
您是否在清单中指定了入口点?
http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
回答by Stephen C
A couple of issues with your code that are not related to your actual problem, but are important nevertheless.
您的代码中的一些问题与您的实际问题无关,但仍然很重要。
1) This statement is unnecessary:
1) 这个语句是不必要的:
import java.lang.*;
By default, every class in java.lang
is implicitlyimported. You don't need to do it explicitly.
默认情况下,每类java.lang
中隐含的进口。您不需要明确地这样做。
2) This is dangerouslybad code:
2)这是危险的坏代码:
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 evidencethat 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:
在此特定上下文中(在main
方法中),以下是更好的方法:
try {
// ...
} catch (Throwable e) {
System.err.println("An unexpected error has occurred:");
e.printStacktrace(System.err);
System.exit(1);
}
回答by gparyani
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:
我查看了类的源代码,它似乎试图从属性列表中获取主类属性,这些属性是字符串,然后在找到的主类属性上调用 trim() 方法。当没有指定主类时,没有主类属性,这会导致搜索方法返回null来表示如此,并且当在其上调用trim()时,由于搜索方法已返回导致NullPointerException空值。为避免这种情况,请确保在 jar 清单中指定了主类:
[directory of class files]>jar -cvmf [name of manifest] MyApp.jar
[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]