Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

macos - How to create an AppleScript- or Command-file to launch a Java application on Mac OS?

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

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

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)

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 is not officially supported by Sun/Oracle that's why it's difficult to find some good documentation. (I need the latest JRE 7).
  2. I never used any Mac or don't have any to try out how it could work.

So my questions are now:

  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?

Thanks for any hints.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay, after some hours of research it seems there are more than just one answer to this issue(s).

Bash scripts

Multiple issues

  • 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.)

Summary

Windows: launch-win32.bat

@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: launch-macos.command

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...