How To Run Mac OS Terminal Commands From Java (Using Runtime?)

寵の児 提交于 2019-12-01 06:38:18

I suspect the problem you have is the path used to find your executables. It depends also if you use an OSX app or a unix cmd

If a unix cmd (or use the Unix part of an OSX app e.g. /Applications/AppName.app/Contents/MacOS/AppName) then there are two ways to fix this

  1. Put the full path to the executable in the Java code e.g.
    String[] cmd = {"/full/absolute/path/to/love", "/Users/mtc06/testgame"};

  2. Alter the path to include the executable. This depends on the method java is launched.

    a)If java is run from the command line then add the directory of the executable to the PATH environment variable.
    b) If the java program is run from the Finder the path has to be altered in ~/MacOSX/environment.plist e.g. adding /Users/mark/bin

   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
   <plist version="1.0">
   <dict>
      <key>PATH</key>
      <string>/Users/mark/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/X11R6/bin:/usr/libexec/binutils:</string>
   </dict>
   </plist>

If the app is an OSX app the you need to launch it using open so the command line is

open -a love.app "/Users/mtc06/testgame"  

so Java command is (not tested)

String[] cmd = {"/usr/bin/open", "-a" , "love.app",  "/Users/mtc06/testgame"};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!