本文介绍了如何创建AppleScript-或命令文件启动Mac OS上的Java应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Java应用程序,需要prepare它在任何操作系统上运行。对于Windows我创造了这样的的启动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

现在我想因为二者都是基于Unix的MacOS将是相当类似Linux和我问一个朋友用的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 PROGRAMM。我现在的主要问题如下:

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:


  1. MacOS的不正式由Sun / Oracle的,这就是为什么它是很难找到一些好的文档支持。 (我需要最新的JRE 7)。

  2. 我从来没有使用过任何Mac或没有任何尝试它如何工作。

所以我的问题现在:


  1. 如何在MacOS的命令行中运行java,什么是正确的语法?或为什么上面的命令中不工作? (例如Windows和Linux之间的主要区别是使用分号; 而不是冒号分离器的类路径)

  2. 应如何MacOS的脚本文件应命名为? .SH .scpt .command 或者是就像Linux中的重要,只要该文件的结局不一样,你使用chmod + X 脚本文件?

  1. 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.)
  2. 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 you chmod +x the script file?

感谢您的任何提示。

推荐答案

好吧,研究几个小时后,似乎有不只是一个答案更多的这个问题(S)。

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 with chmod +x.

  • 的一个原因的NoClassDefFoundError 可以是在。没有更多我可以做有关不仅仅是告诉用户安装JRE lateste

  • 的另一个原因的NoClassDefFoundError 是 - 这是相当惊人 - 在Mac OS的的bash脚本不运行在同一目录内,在那里位于但是从用户的主目录。在solution是添加一行到bash脚本来找出工作目录:光盘$(目录名称$ 0)(See也的。)

  • 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 OS:的启动macos.command

#!/bin/bash
cd "$(dirname "$0")"
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass

这篇关于如何创建AppleScript-或命令文件启动Mac OS上的Java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:44