25

I am distributing a Java program where I want a double-clickable file to run

java -cp MyProgram.jar;MyLib.jar my.program.Main

On Windows I simply distribute a .bat file, for *nix an executable .sh file. Problem is, double-clicking the .sh file just opens it up in a text editor on Mac. What should I do for Mac?

Epaga
  • 38,231
  • 58
  • 157
  • 245
  • Another useful related question is https://stackoverflow.com/questions/14065069/equivalent-of-bat-in-mac-os. – Edward Oct 05 '18 at 11:36

4 Answers4

44

On mac, there is a specific extension for executing shell scripts by double clicking them: this is .command.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • 9
    This is a pretty old question, but for those getting a `.command could not be executed because you do not have appropriate access privileges.` error, as I was, execute the following on Terminal: `chmod u+x /path/to/file.command` and that should get rid of that problem. – asherbret Mar 14 '15 at 12:07
  • 1
    @user2016436 I don't want to add `chmod u+x` to run the file, as the executable file will run at the client. what should I do? – Mr_Green Sep 12 '15 at 16:22
5

For Java applications on Mac, you really should use Apple's Jar Bundler (in the Developer Tools/Applications/Utilities folder; really a symlink to /usr/share/java/Tools/Jar Bundler). It lets you make a proper OS X double-clickable app, including setting preferences for e.g. using the Mac toolbar, JVM version, graphics system, OS X app metadata and classpath/resources.

Barry Wark
  • 107,306
  • 24
  • 181
  • 206
3

You can use a .sh (Shell Script), after all MacOSX is Unix!

Till
  • 22,236
  • 4
  • 59
  • 89
1

The answer about using the Jar Bundler tool is correct, but if you want to use a .sh file, make sure the unix permissions are set properly to something like 755 with CHMOD, and make sure the first line contains the path to a shell installed by default on Mac OS X. Also note that even with the +x bit set, it may still ask the user whether they want to open it or run it.

Noah
  • 998
  • 1
  • 12
  • 22