Mac - Run java program at launch

6,777

Save a property list like this as /Library/LaunchAgents/some.label.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>some.label</string>
  <key>ProgramArguments</key>
  <array>
    <string>java</string>
    <string>-jar</string>
    <string>/Library/Server/FiloSync/filosync-server-latest.jar</string>
    <string>-p</string>
    <string>7000</string>
    <string>-s</string>
    <string>7001</string>
  </array>
  <key>RunAtLoad</key>
  <true/> <!-- run the program at login -->
  <key>KeepAlive</key>
  <true/> <!-- run the program again if it terminates -->
  <key>WorkingDirectory</key>
  <string>/Library/Server/FiloSync</string>
</dict>
</plist>

Make sure the file is owned by root. If it is not, it can be loaded without sudo, but it is not loaded automatically at login. Then log out and back in to test if the program was started.

For more information, see man launchd.plist, the Daemons and Agents tech note, or http://osxnotes.net/launchd.html.

Share:
6,777

Related videos on Youtube

emd
Author by

emd

Updated on September 18, 2022

Comments

  • emd
    emd over 1 year

    I want to launch a java program (server program) at computer startup. I can run it from the command line perfectly.

    I created /Library/Server/startFS.sh. The file is executable by root and contains:

    cd /Library/Server/FiloSync
    /usr/bin/java -jar /Library/Server/FiloSync/filosync-server-latest.jar -p 7000 -s 7001
    

    I can't get my launchd .plist included here, the formatting is all off.

    I can run it fine from the command line: ./startFS.sh, but when I put create the plist (via Lingon), nothing happens.

    Now, when run, it outputs a few lines to the console. Might that be problem?

    I have tried prepending nohup and appending &, but no combination seems to work.

    • Admin
      Admin over 10 years
      Have you tried making a script of some sort that automatically launches said java program on startup?
    • Admin
      Admin over 10 years
      Don't Mac OS allows you to launch program at start up? (At user login to be exact) - System Preferences - Users & Groups - Click on your name, and on the right hand side, click on "Login Items" tab, and you can add your startFS.sh in there.
  • emd
    emd over 10 years
    I will check what my plist file looks like (I'm at work and don't have access to home machine) but I think it looks like that (created from Lingon X).