4

Trying to run the following test.command script on Mac with a double-click. (requirement: MUST be run with a double-click)

#!/bin/sh
sudo java -jar ExecutableJar.jar

Here is the output. (The terminal stays open with the below message)

Last login: Mon Aug 13 15:59:05 on ttys001
/Applications/Application\ Folder/test.command ; exit;
code-mac:~ code$ /Applications/Application\ Folder/test.command ; exit;
Unable to access jarfile ExecutableJar.jar
logout

[Process completed]

When I run the same command from Terminal...

sudo java -jar ExecutableJar.jar

...it works fine and opens the executable jar as expected (after prompting for my password). Any ideas? Also, if possible, I'd like the script to either not open a terminal at all, or at the very least close the terminal after starting the executable jar.

Thanks!

6
  • Perhaps you need to specify the full path to ExecutableJar.jar. It's probably not in the default directory that double-clicked scripts get run in (probably your home directory). Running it manually, you have probably done a cd beforehand or something... Commented Aug 13, 2012 at 21:27
  • Since it's a program that is being installed to a user-chosen location, the location can't be hard-coded. The script is in the same directory as the executable jar, how would I change the script to switch to the current directory? Maybe a "cd ."? Commented Aug 13, 2012 at 21:29
  • In that case, you may need an installer script that customizes the test.command script to accommodate the users chosen installation location. Commented Aug 13, 2012 at 21:34
  • Not possible with the installer and requirements I have to go by. I'm pretty hesitant to believe that the script can't figure out it's own location and set the location of the jar executable itself... Commented Aug 13, 2012 at 21:38
  • Does your system have dirname on it? I'm not very familiar with the Mac/OSX environment. In that case, d=$(dirname $0) at the top of your test.command and changing the java invocation to java -jar ${d}/ExecutableJar.jar should work. Won't work if you don't have dirname, though... Commented Aug 13, 2012 at 21:44

1 Answer 1

13

Adding the following to the beginning of the script made it work as expected when double-clicked.

cd "$(dirname "$0")"

The entire test.command script is as follows:

#!/bin/sh
cd "$(dirname "$0")"
sudo java -jar ExecutableJar.jar &

Adding the & at the end of the sudo command makes that executable jar run as a background process and allows further commands after the sudo to be processed, in my case, to close the Terminal window. (Otherwise it stays open)

Lastly, adding either of the following to the end of the script will close it once it's done. The first approach closes all Terminal windows and is a bit of an overkill, but it gets the job done.

killall Terminal

The second will prompt the user to close the Terminal window, giving the user the choice.

osascript -e 'tell application "Terminal" to quit'

A final, important note is that neither closing technque would work for my case. Since my script requires a sudo and prompts the user to enter their password, using either route prompted the user to close the Terminal (osascript) or closed the Terminal (killall) before the user had the chance to enter their password. In order to ask for the password first and then run the executable jar, use:

sudo -v

to prompt for the password if needed, then run the executable jar in the background with & and use killall or osascript.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.