So I've been writing this little code, and it works -- but I have to type in the whole Computer;~ User$ java MyProject
and change the path every time, so I decided to stick it in a .jar
file (so I could just run it on a double click). I am an Intellij user on running macos. I created the file, File/Project Structure/artifacts/+/myMain
etc and now have a .jar
file sitting on my desktop. When I run the jar inside of Intellij, or run the jar using
java -jar /Users/Me/Desktop/MyProject/out/artifacts/MyProject_jar/MyProject.jar
, it works fine.
However, when I:
Run it from the terminal with java -jar MyProject.jar
I get Error: Unable to access jarfile MyProject.jar
^^Resolved - using the chmod 755 MyProject.jar
command, see comment for further details
Or run it on right click with Jar launcher I get The Java JAR file "MyProject.jar" could not be launched. Check the Console for possible error messages.
The Java JAR file "MyProject.jar" could not be launched. Check the Console for possible error messages.
There are no error messages in the console.
And before anyone says it, I have Java 13 and the project is running on Java 13 -- it's the default that my computer has, and has been updated automatically ever since installation.
How would I be able to launch this file, and what could be stopping it from doing so?
in terminal, do an
ls -l MyProject.jar
You'll see something like:
-rw-r--r-- 1 youruser yourgroup 1024 Apr 24 15:41 MyProject.jar
The -rw-r--r-- part is the file permissions where an "r" means readable, "w" means writable, and an "x" would mean executable, but as you can see there isn't one there. Without going into the long explanation, use the chmod command to make the file executable by doing
chmod 755 MyProject.jar
Afterwards, use the same ls command and you should see -rwxr-xr-x in the permissions field. The file is now executable (by anyone) and should launch if you click in in Finder.
If your program has no GUI but requires console input, you should run it from the terminal instead of double clicking it. When you double click the MyProject.jar
file, you actually launch it through jar launcher. The jar launcher will pass the files to the JVM. However, since you are not launching it from the terminal, the JVM does not know which terminal to use, so the program might not be properly executed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.