Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 652
Afr Avatar asked Mar 11 '12 14:03

Afr


People also ask

How do I create a Java file on Mac?

Open Terminal. Enter mkdir HelloWorld to create a new directory and cd HelloWorld to move into it. Enter touch HelloWorld. java to create an empty Java file.

How do I run an AppleScript on a Mac?

In the Script Editor app on your Mac, click the Run button in the toolbar, or press Command-R, to execute the commands in your script.

How do I run AppleScript in Java?

A Java AppleScript example Runtime runtime = Runtime. getRuntime(); String applescriptCommand = "tell app \"iTunes\"\n" + "activate\n" + "set sound volume to 40\n" + "set EQ enabled to true\n" + "play\n" + "end tell"; String[] args = { "osascript", "-e", applescriptCommand }; Process process = runtime. exec(args);

Does Apple still use AppleScript?

AppleScript is a scripting language created by Apple Inc. that facilitates automated control over scriptable Mac applications. First introduced in System 7, it is currently included in all versions of macOS as part of a package of system automation tools.


1 Answers

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

Bash scripts

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

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
like image 186
Afr Avatar answered Nov 14 '22 06:11

Afr