Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this particular case you could fake it by using AppleScript to open the terminal:</p> <pre><code>final ProcessBuilder processBuilder = new ProcessBuilder( "/usr/bin/osascript", "-e"); final Map&lt;String, String&gt; environment = processBuilder.environment(); final String path = environment.get("PATH"); processBuilder.command().add("tell application \"Terminal\" to do script \"" + "cd /Volumes ; export PATH=\\\"/mypath:" + path + "\\\\"\""); final Process process = processBuilder.start(); process.waitFor(); </code></pre> <p>If you want to populate the <code>cd</code> directory and the <code>PATH</code> value from user-supplied parameters then I would consider using <code>on run</code> to let the script take arguments, and use <code>quoted form of</code> to avoid any potential issues with quoting:</p> <pre><code>final ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/osascript", "-e", "on run argv", "-e", " tell application \"Terminal\" to do script " + "\"cd \" &amp; quoted form of item 1 of argv &amp; \" ; " + "export PATH=\" &amp; quoted form of item 2 of argv", "-e", "end run"); // create a File and use getAbsolutePath to resolve any relative paths // against this Java process's working directory. File cdDir = new File(dirParam); // this argument will be "item 1 of argv" to the AppleScript processBuilder.command().add(cdDir.getAbsolutePath()); final Map&lt;String, String&gt; environment = processBuilder.environment(); final String path = environment.get("PATH"); File pathPrefix = new File(pathParam); // and this will be "item 2 of argv" processBuilder.command().add( pathPrefix.getAbsolutePath() + File.pathSeparator + path); final Process process = processBuilder.start(); process.waitFor(); </code></pre> <p>If you can't do the <code>getAbsolutePath</code> trick on the Java side but still want relative paths (relative to the current directory of <code>osascript</code>) to work, then you'd need a trick like this:</p> <pre><code>final ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/osascript", "-e", "on run argv", "-e", " set currentdir to (do shell script \"pwd\")", "-e", " set currentpath to (do shell script \"echo $PATH\")", "-e", " tell application \"Terminal\" to do script \"" + "cd \" &amp; quoted form of currentdir &amp; \" ; "" + "cd \" &amp; quoted form of item 1 of argv &amp; \" ; " + "export PATH=\" &amp; quoted form of currentpath", "-e", "end run", dirParam); final Map&lt;String, String&gt; environment = processBuilder.environment(); final String path = environment.get("PATH"); environment.put("PATH", pathParam + File.pathSeparator + path); final Process process = processBuilder.start(); process.waitFor(); </code></pre> <p>This is a bit of a Russian doll - we're executing <code>osascript</code> from Java, which in turn runs its own subshell to do <code>pwd</code> and <code>echo $PATH</code> to get the values of the current working directory and the <code>PATH</code> variable <em>of the osascript process</em> into AppleScript variables, which we then inject into the new Terminal. By first doing a <code>cd</code> to the "current" dir, and then another <code>cd</code> to the specified <code>dirParam</code> this will handle both relative and absolute paths.</p> <p>Finally note that <code>osascript</code> prints the return value of the script, so you should make sure you consume all the process output before doing <code>waitFor</code> (or if you're on Java 7 you can use <code>processBuilder.redirectOutput(new File("/dev/null"))</code> and the same for <code>redirectError</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload