简体   繁体   English

Mac OS 无法运行.Jar 文件

[英]Mac OS unable to run .Jar file

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).所以我一直在写这个小代码,它可以工作——但我必须输入整个Computer;~ User$ java MyProject并每次都更改路径,所以我决定将它粘贴在.jar文件中(所以我可以双击运行它)。 I am an Intellij user on running macos.我是运行 macos 的 Intellij 用户。 I created the file, File/Project Structure/artifacts/+/myMain etc and now have a .jar file sitting on my desktop.我创建了文件File/Project Structure/artifacts/+/myMain等,现在我的桌面上有一个.jar文件。 When I run the jar inside of Intellij, or run the jar using当我在 Intellij 内部运行 jar 或使用 jar 运行

java -jar /Users/Me/Desktop/MyProject/out/artifacts/MyProject_jar/MyProject.jar , it works fine. java -jar /Users/Me/Desktop/MyProject/out/artifacts/MyProject_jar/MyProject.jar ,它工作正常。

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 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.或者使用 Jar 启动器右键单击运行它我得到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.在任何人说之前,我有 Java 13 并且该项目在 Java 13 上运行 - 这是我的计算机的默认设置,并且自安装以来一直自动更新。

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. -rw-r--r-- 部分是文件权限,其中“r”表示可读,“w”表示可写,“x”表示可执行,但正如您所见,那里没有。 Without going into the long explanation, use the chmod command to make the file executable by doing无需进行冗长的解释,使用 chmod 命令通过执行使文件可执行

chmod 755 MyProject.jar

Afterwards, use the same ls command and you should see -rwxr-xr-x in the permissions field.之后,使用相同的 ls 命令,您应该会在权限字段中看到 -rwxr-xr-x。 The file is now executable (by anyone) and should launch if you click in in Finder.该文件现在是可执行的(任何人),如果您在 Finder 中单击,该文件应该会启动。

If your program has no GUI but requires console input, you should run it from the terminal instead of double clicking it.如果您的程序没有 GUI 但需要控制台输入,您应该从终端运行它,而不是双击它。 When you double click the MyProject.jar file, you actually launch it through jar launcher.当您双击MyProject.jar文件时,您实际上是通过 jar 启动器启动它。 The jar launcher will pass the files to the JVM. jar 启动器会将文件传递给 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.但是,由于您不是从终端启动它,JVM 不知道要使用哪个终端,因此程序可能无法正确执行。

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

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