Java – NullPointerException when trying to run Jar file
•
Java
I've just started learning Java and only know a small amount of code, but it's still a simple program This is a prank program, but most of them just test whether I can make a jar file
This 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 saved it to a file named randommouse Java file, and then use it to compile
javac randommouse.java
This is effective when I run it
java randommouse
It's also very good
So I tried to create a jar file I use commands
jar cvf randommouse.jar randommouse.class
After it works, I double - click the jar file and an error Java exception appears So I run it 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>
I need to put forward an argument. If so, what should I do and what should I do?
Thank you first, Sam
Solution
From the JDK doc:
Main-Class: classname
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
