How to run Mac OS terminal commands with Java (using runtime?)

I was looking for ways to run external programs using the Java runtime. This works fine, for example:

String[] cmd = {"mkdir", "test"};
Runtime.getRuntime().exec(cmd);

Creates a new directory, as one would expect. Now from the bash window on Mac, I can write this:

love testgame

Launch the Love game engine in the testgame folder. Now the reason is that I imposed “love” to name the executable text. I got the feeling that this is the reason that the following does not work:

String[] cmd = {"love", "/Users/mtc06/testgame"};
Runtime.getRuntime().exec(cmd);

And does not do this (for those who are wondering):

String[] cmd = {"/bin/bash", "love", "/Users/mtc06/testgame"};
Runtime.getRuntime().exec(cmd);

Without a doubt, this is either some kind of Java idiocy on my part, or some kind of collision with the way aliases work. I pass it on to your venerable intellects, SO!

UPDATE: this does not work either:

String[] cmd = {"/bin/sh", "/Applications/love", "/Users/michaelcook/Desktop/Playout"};
Runtime.getRuntime().exec(cmd);

, , - 127 , Runtime. " ", .

+3
1

, , . , OSX unix cmd

unix cmd ( Unix OSX, /Applications/AppName.app/Contents/MacOS/AppName),

  • Java, .
    String[] cmd = {"/full/absolute/path/to/love", "/Users/mtc06/testgame"};

  • . java.

    a) java , PATH.
    ) java- Finder, ~/MacOSX/environment.plist, . //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>

OSX app, open,

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

Java ( )

String[] cmd = {"/usr/bin/open", "-a" , "love.app",  "/Users/mtc06/testgame"};
+6

Source: https://habr.com/ru/post/1786172/


All Articles