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:
So my questions are now:
;
instead of colon :
separator for the classpath.).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.
Okay, after some hours of research it seems there are more than just one answer to this issue(s).
.command
bash script files. They look quite similar to linux shell scripts. Make them executable like shell scripts with chmod +x
.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.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.)launch-win32.bat
@echo off
javaw -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*;bin/myjar-latest.jar my.package.MyMainClass
launch-linux.sh
#!/bin/sh
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass
launch-macos.command
#!/bin/bash
cd "$(dirname "$0")"
java -Xss1024k -Xmn256m -Xms512m -Xmx1024m -cp lib/*:bin/myjar-latest.jar my.package.MyMainClass