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

编程入门 行业动态 更新时间:2024-10-28 18:32:23
本文介绍了尝试运行.jar文件时出现NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚开始学习java,只知道少量代码,但这仍然是一个简单的程序。它更像是一个恶作剧程序,但主要是为了测试我是否可以制作一个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.

以下是代码:

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 的文件中,然后使用

I save this to file called randommouse.java, then compile it using

javac randommouse.java

这是有效的,当我使用

java randommouse

它也能正常工作。

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

So then I try to create a jar file. I use the command

jar cvf randommouse.jar randommouse.class

它有效。然后我双击jar文件,它出现错误 Java异常。 那么我在cmd中运行它

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

并收到此错误

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?

提前谢谢你们! Sam

Thank you in advance Sam

推荐答案

来自 JDK doc :

为了使此选项有效,JAR文件的清单必须包含格式的行

In order for this option to work, the manifest of the JAR file must contain a line of the form

Main-Class: classname

这里,classname 标识具有public static void main(String [] args)方法的类,该方法充当您的应用程序的初始点。有关使用Jar文件和Jar文件清单的信息,请参阅Jar 工具参考页面和Java Tutorial的Jar跟踪。

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.

当您使用此选项时,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文件时出现NullPointerException

本文发布于:2023-07-14 17:53:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   jar   NullPointerException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!