如何创建 AppleScript 或 Command 文件以在 Mac OS 上启动 Java 应用程序? [英] How to create an AppleScript- or Command-file to launch a Java application on Mac OS?
问题描述
我创建了一个 Java 应用程序,需要准备它以在任何操作系统上运行.对于 Windows,我创建了一个这样的批处理文件 launch-win32.bat
:
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
:
@echo off
javaw -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*;bin/myjar-latest.jar my.package.MyMainClass
对于 linux,我创建了一个像这样的 shell 脚本 launch-linux.sh
:
For linux I created a shell script like this launch-linux.sh
:
#!/bin/sh
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass
现在我认为 MacOS 将与 linux 非常相似,因为两者都是基于 unix 的,我让一个使用 mac 的朋友尝试运行 shellscript 来启动我的应用程序.但它失败了以下 NoClassDefFoundError
:
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
:
Exception in thread "main" java.lang.NoClassDefFoundError: my/package/MyMainClass
Caused by: java.lang.ClassNotFoundException: my.package.MyMainClass
at java.net.URLClassLoader$1.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)
看起来java命令的语法不正确,因为类路径没有正确添加到java程序中.我现在的主要问题如下:
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:
- MacOS 不受 Sun/Oracle 的正式支持,因此很难找到一些好的文档.(我需要最新的 JRE 7).
- 我从未使用过任何 Mac,也没有尝试过 Mac 是如何工作的.
所以我现在的问题是:
- 如何在 MacOS 中从命令行运行 java,正确的语法是什么?或者为什么上面的命令不起作用?(例如,Windows 和 Linux 之间的主要区别是使用分号
;
而不是冒号:
分隔符作为类路径.) - MacOS 脚本文件应该如何命名?
.sh
或.scpt
或.command
还是像在 linux 中一样,只要您文件结尾就无关紧要chmod +x
脚本文件?
- 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?
感谢您的任何提示.
推荐答案
好的,经过几个小时的研究,似乎这个问题的答案不止一个.
Okay, after some hours of research it seems there are more than just one answer to this issue(s).
- 在 Mac OS 中创建脚本的最简单方法似乎是
.command
bash脚本文件.它们看起来与 linux shell 脚本非常相似.使用chmod +x
使它们像 shell 脚本一样可执行.
- The simplest way to create scripts in Mac OS seems to be the
.command
bash script files. They look quite similar to linux shell scripts. Make them executable like shell scripts withchmod +x
.
NoClassDefFoundError
的一个原因可能是 Mac OS 上默认安装的 Java VM 较低 比用于编译软件所需的 JRE/JDK.除了告诉用户安装最新的 JRE 之外,我无能为力.NoClassDefFoundError
的另一个原因是 - 这非常令人震惊 - Mac OS 中的 bash 脚本不在它们所在的同一目录中运行 但来自用户的主目录.解决方案是在bash中添加一行脚本找出工作目录:cd "$(dirname "$0")"
(另见.)
- One reason for the
NoClassDefFoundError
can be that the default installed Java VM on Mac OS is lower than 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 in but from the user's home directory. The solution is to add a line to the bash script to find out the working directory:cd "$(dirname "$0")"
(See also.)
@echo off
javaw -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*;bin/myjar-latest.jar my.package.MyMainClass
Linux:launch-linux.sh
#!/bin/sh
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass
Mac 操作系统:launch-macos.command
#!/bin/bash
cd "$(dirname "$0")"
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass
这篇关于如何创建 AppleScript 或 Command 文件以在 Mac OS 上启动 Java 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!