如何创建 AppleScript 或 Command 文件以在 Mac OS 上启动 Java 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9655843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an AppleScript- or Command-file to launch a Java application on Mac OS?
提问by Afr
I created a Java application and need to prepare it to run on any OS. For Windows I created a batch file like this launch-win32.bat
:
我创建了一个 Java 应用程序,需要准备它以在任何操作系统上运行。对于 Windows,我创建了一个这样的批处理文件launch-win32.bat
:
@echo off
javaw -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*;bin/myjar-latest.jar my.package.MyMainClass
For linux I created a shell script like this launch-linux.sh
:
对于 linux,我创建了一个这样的 shell 脚本launch-linux.sh
:
#!/bin/sh
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass
Now I thought MacOS will be quite similar to linux as both are unix based and I asked a friend with a mac to try to run the shellscript to launch my application. But it failed with the following NoClassDefFoundError
:
现在我认为 MacOS 将与 linux 非常相似,因为两者都是基于 unix 的,我让一个使用 mac 的朋友尝试运行 shellscript 来启动我的应用程序。但它失败了NoClassDefFoundError
:
Exception in thread "main" java.lang.NoClassDefFoundError: my/package/MyMainClass
Caused by: java.lang.ClassNotFoundException: my.package.MyMainClass
at java.net.URLClassLoader.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
It looks like the syntax of the java command is not correct as the classpath is not properly added to the java programm. My main problem now is the following:
看起来 java 命令的语法不正确,因为类路径没有正确添加到 java 程序中。我现在的主要问题如下:
- MacOS is not officially supported by Sun/Oracle that's why it's difficult to find some good documentation. (I need the latest JRE 7).
- I never used any Mac or don't have any to try out how it could work.
- Sun/Oracle 不正式支持 MacOS,这就是为什么很难找到一些好的文档。(我需要最新的 JRE 7)。
- 我从来没有使用过任何 Mac 或没有任何尝试它是如何工作的。
So my questions are now:
所以我现在的问题是:
- How to run java from command line in MacOS, what's the correct syntax? Or why does the command above not work? (For example the main difference between Windows and Linux is using semicolon
;
instead of colon:
separator for the classpath.) - How should a MacOS script file should be named?
.sh
or.scpt
or.command
or is it like in linux that the file ending doesn't matter as long as youchmod +x
the script file?
- 如何在 MacOS 中从命令行运行 java,正确的语法是什么?或者为什么上面的命令不起作用?(例如,Windows 和 Linux 之间的主要区别是在类路径中使用分号
;
而不是冒号:
分隔符。) - MacOS 脚本文件应该如何命名?
.sh
或者.scpt
或者.command
还是像在linux中一样,只要您chmod +x
是脚本文件,文件结尾就无关紧要?
Thanks for any hints.
感谢您的任何提示。
采纳答案by Afr
Okay, after some hours of research it seems there are more than just one answer to this issue(s).
好的,经过几个小时的研究,似乎这个问题的答案不止一个。
Bash scripts
Bash 脚本
- The simplest way to create scripts in Mac OS seems to bethe
.command
bash script files. They look quite similar to linux shell scripts. Make them executable like shell scripts withchmod +x
.
- 在Mac OS中创建脚本最简单的方法似乎是在
.command
bash脚本文件。它们看起来与 linux shell 脚本非常相似。使它们像 shell 脚本一样可执行chmod +x
。
Multiple issues
多个问题
- One reason for the
NoClassDefFoundError
can be that the default installed Java VM on Mac OS is lowerthan the needed JRE/JDK which was used compiling the software. Nothing more I can do about that than just telling the user to install the lateste JRE. - Another reason for the
NoClassDefFoundError
is - and this is quite shocking - that bash scripts in Mac OS don't run from within the same directory as where they are located inbut from the user's home directory. The solution is to add a line to the bash scriptto find out the working directory:cd "$(dirname "$0")"
(See also.)
- 原因之一
NoClassDefFoundError
可能是Mac OS 上默认安装的 Java VM 低于编译软件所需的 JRE/JDK。除了告诉用户安装最新的 JRE 之外,我无能为力。 - 另一个原因
NoClassDefFoundError
是——这非常令人震惊——Mac OS 中的 bash 脚本不是从它们所在的同一目录中运行,而是从用户的主目录中运行。该解决方案是将行添加到bash脚本,找出工作目录:cd "$(dirname "$0")"
(另见。)
Summary
概括
Windows: launch-win32.bat
视窗: launch-win32.bat
@echo off
javaw -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*;bin/myjar-latest.jar my.package.MyMainClass
Linux: launch-linux.sh
Linux: launch-linux.sh
#!/bin/sh
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass
Mac OS: launch-macos.command
苹果系统: launch-macos.command
#!/bin/bash
cd "$(dirname "##代码##")"
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass