Spawn Shell Process

This topic has been viewed 8278 times and has 5 replies
Thread status: Active
Questions about extending features and developing plug-ins [img]http://www.sweethome3d.com/images/flags/en_small.gif[/img]
Post Reply New Topic
treehousenorris2
Posts: 16
Joined: Mon Sep 14, 2020 8:21 pm
Country: United States

Spawn Shell Process

Post by treehousenorris2 »

Hello!

I'd like to execute a shell command from my plugin's java.

I am getting "cannot run program" errors, such as the following. Is there anything Sweet Home specific to folder or spawn permissions that would affect this?

`hdiutil mount ~/Downloads/SweetHome3D-6.4.2-macosx.dmg`
`cd ‘/Volumes/SweetHome3D-6.4.2/Sweet Home 3D.app/Contents/MacOS’`
`./SweetHome3D`

java.io.IOException: Cannot run program "sh" (in directory "/bin"): error=316, spawn failed
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)

java.io.IOException: Cannot run program "adb" (in directory "/usr/local/bin"): error=316, spawn failed
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)

I'm working on a plugin to act as a one-click exporter to transfer the home's .obj file (etc) from a mac onto an android mobile device.

Thanks!
User avatar
Puybaret
Posts: 9440
Joined: Mon Nov 07, 2005 12:00 pm
Country: Paris, France
Contact:

Re: Spawn Shell Process

Post by Puybaret »

What Java method did you try to run your command?
Under which Sweet Home 3D version? I wouldn’t be astonished that the sandboxed version available on the Mac App Store doesn’t allow such a feature.
Emmanuel Puybaret, Sweet Home 3D creator
treehousenorris2
Posts: 16
Joined: Mon Sep 14, 2020 8:21 pm
Country: United States

Re: Spawn Shell Process

Post by treehousenorris2 »

Sweet Home 6.4.2 from SourceForge. To see the Sweet Home stdout I am using the dmg>app>executable because I wasn't able to get the Sweet Home jar to run from the terminal (not on mojave but worked on a catalina machine). Mojave only works by double clicking the Sweet Home jar, which defaults to open with Jar Launcher, but without stdout.

@Override
public void execute() {
routine();
}

public void routine() {
Process process;
try {
process = Runtime.getRuntime().exec(
new String[]{"bash", "-c", "pwd"},
null, new File("/bin"));
printResults(process);
} catch (IOException e) {
e.printStackTrace();
}
}

public void printResults(Process process) throws IOException {
BufferedReader reader = new BufferedReader(new
InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
treehousenorris2
Posts: 16
Joined: Mon Sep 14, 2020 8:21 pm
Country: United States

Re: Spawn Shell Process

Post by treehousenorris2 »

Got a shell to work from a plugin used with the SourceForge Sweet Home on Windows.

Code: Select all

runProcess("cmd /c echo hello world", true);
// or
// String cmdFilePath = runProcess("where cmd", true);
// String cmdToRun = cmdFilePath +" /c echo hello world";

private String runProcess(String command, boolean shouldWait) {
	    try {
	      Runtime rt = Runtime.getRuntime();
	      Process proc = rt.exec(command);
	      if (!shouldWait) {
	        return null;
	      }

	      System.out.println("--"+command);
	      InputStream stdout = proc.getInputStream();
	      InputStreamReader isrout = new InputStreamReader(stdout);
	      BufferedReader brout = new BufferedReader(isrout);
	      String output = "";
	      String lineout = null;
	      System.out.println("<OUT>");
	      while ( (lineout = brout.readLine()) != null) {
	        output += (output.length() == 0 ? "" : "\n") + lineout;
	        System.out.println(lineout);
	      }
	      System.out.println("</OUT>");
	  
	      InputStream stderr = proc.getErrorStream();
	      InputStreamReader isr = new InputStreamReader(stderr);
	      BufferedReader br = new BufferedReader(isr);
	      String line = null;
	      System.out.println("<ERROR>");
	      while ( (line = br.readLine()) != null) {
	        System.out.println(line);
	      }
	      System.out.println("</ERROR>");

	      int exitVal = proc.waitFor();
	      System.out.println("Process exitValue: " + exitVal);
	      return output;
	  
	    } catch (IOException ex) {
	      // TODO Auto-generated catch block
	      ex.printStackTrace();
	    } catch (InterruptedException ex) {
	      // TODO Auto-generated catch block
	      ex.printStackTrace();
	    }
	    return null;
	  }
treehousenorris2
Posts: 16
Joined: Mon Sep 14, 2020 8:21 pm
Country: United States

Re: Spawn Shell Process

Post by treehousenorris2 »

Aha, the jre bundled with Sweet Home for Mac needed an executable permission on jspawnhelper.

Code: Select all

chmod u+x /Applications/Sweet\ Home\ 3D.app/Contents/Plugins/Java.runtime/Contents/Home/jre/lib/jspawnhelper
or

Code: Select all

<target name="macosxBundle"...
    <!-- Change executable permission of jspawnhelper lost during copy task -->
    <chmod perm="+x" file="install/macosx/SweetHome3D-${version}/Sweet Home 3D.app/Contents/Plugins/Java.runtime/Contents/Home/jre/lib/jspawnhelper"/>
User avatar
Puybaret
Posts: 9440
Joined: Mon Nov 07, 2005 12:00 pm
Country: Paris, France
Contact:

Re: Spawn Shell Process

Post by Puybaret »

Thanks for the information about jspawnhelper, I updated build.xml accordingly.
Emmanuel Puybaret, Sweet Home 3D creator
Post Reply New Topic

Who is online

Users browsing this forum: No registered users and 1 guest