简体   繁体   English

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

[英]NullPointerException when trying to run .jar file

I have just started learning java, and know only a small amount of code, however this is still a simple program. 我刚开始学习java,只知道少量代码,但这仍然是一个简单的程序。 It is more of a prank program, but mostly just to test if I can make a jar file. 它更像是一个恶作剧程序,但主要是测试我是否可以制作一个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. 那么我尝试创建一个jar文件。 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 . 然后我双击jar文件,它出现了一个错误Java Exception
So then I run it in the cmd with 那么我在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 山姆

From the JDK doc : JDK doc

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. 这里,classname标识具有public static void main(String [] args)方法的类,该方法充当应用程序的起点。 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文件和Jar文件清单的信息,请参阅Jar工具参考页面和Java Tutorial的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文件是所有用户类的源,并忽略其他用户类路径设置。

You have to set the entry point 您必须设置入口点

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

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

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. 我看了一下类的源代码,它似乎试图从属性列表中获取主类属性,这些属性是字符串,然后在找到的主类属性上调用trim()方法。 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. 当没有指定主类时,没有主类属性,这会导致搜索方法返回null来表示,并且当调用trim()时,它会导致NullPointerException,因为搜索方法已经返回空值。 To avoid this, be sure that the main class is specified in the jar manifest: 要避免这种情况,请确保在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]

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 implicitly imported. 默认情况下, java.lang每个类都是隐式导入的。 You don't need to do it explicitly. 您不需要明确地执行此操作。

2) This is dangerously bad 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 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: 在这个特定的上下文中(在main方法中),以下是一种更好的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM