Execute behind the scenes bash command from Java ProcessBuilder - java

I'm using in windows:
builder = new ProcessBuilder("cmd.exe", "/c", my_command);
Process p = builder.start();
i've tried to replicate that behaviour, just for macOS
i've tried theese and none of them have worked:
1. builder = new ProcessBuilder("bin/bash", "-c", my_command);
2. builder = new ProcessBuilder("osascript", my_command);
Process p = builder.start();
Will appreciate the help
Thanks

The paths you have are incorrect, they should be:
/bin/bash
and
/usr/bin/osascript

This one worked for me:
builder = new ProcessBuilder("bash", "-c", command);
Process process = builder.start();

Related

Process Builder Arguments

final String commands[] = {"arp", "-n", "|" ,"grep", "98:5d:ad:3d:36:ef", "|", "awk '", "{print $1}", "'"};
ProcessBuilder pb = new ProcessBuilder(commands);
I would like to retrieve the IP, given the MAC ADDRESS.
When I insert this command to the terminal (ubuntu 16.04) it works.
But it doesn't work when I use it in JAVA.
What am i doing wrong?
It only works when I run it like this:
final String commands[] = {"arp", "-n"};
ProcessBuilder pb = new ProcessBuilder(commands);
You need to invoke "sh" and pass to that program your piped command.
Try:
ProcessBuilder b = new ProcessBuilder( "/bin/sh", "-c",
"arp -n | grep 98:5d:ad:3d:36:ef | awk '{print $1}'" );

How Do I start a Terminal in Mac OS from Java

How do I start a terminal from Mac OS from a java program?
The code that I have tried are
String[] arg = new String[] {"/bin/bash", "-c", "Terminal"};
Process proc = new ProcessBuilder(arg).start();
OutputStream out = null;
Process proc = new ProcessBuilder("Terminal").start();
Runtime.getRuntime().exec("/bin/bash -c Terminal");
out = proc.getOutputStream();
out.write("cd /home/me/Desktop/".getBytes());
out.flush();
However both of em didnt work. Any help would be appreciated.
The following command works for me just give the absolute path of your terminal app and it should work fine.
Runtime.getRuntime().exec("/bin/bash -c /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal");

Java process gets stuck [duplicate]

I was trying to build up a ProcessBuilder calling the ffmpeg binary.
My problem is that calling it, it returns perfectly under MacOs, Ubuntu and WindowsXp,
but under Windows7 the waitFor() never returns.
Has anyone similar experience under windows 7? Any help would be appreciated!
My command:
ProcessBuilder pb = new ProcessBuilder( );
pb.command( "C:\\Windows\\System32\\cmd.exe", "/c", "c:\\ffmpeg\\bin\\ffmpeg.exe", "-version" );
Tried these ones too:
pb.command( "c:\\ffmpeg\\bin\\ffmpeg.exe", "-version" );
pb.command( "C:\\Windows\\System32\\cmd.exe", "/c", "start c:\\ffmpeg\\bin\\ffmpeg.exe -version" );
Result is the same. :(
Looks like your process writes something in its out and/or err streams. Their buffer overflow and process blocks. You should read out and err streams of your process to avoid this.
See "When Runtime.exec() won't" for more information
If you are using java 7 you could do something like that :
File encodingFile = new File(outfile + ".encoding");
ProcessBuilder pb = new ProcessBuilder(vars.config.ffmpeg, "-i", file, "-y", "-s", width + "x" + height, "-vcodec", "libvpx", outfile); //or other command....
encodingFile.createNewFile();
pb.redirectErrorStream(true);
pb.redirectInput(ProcessBuilder.Redirect.PIPE); //optional, default behavior
pb.redirectOutput(encodingFile);
Process p = pb.start();
// if you want to wait for the process to finish
p.waitFor();
encodingFile.delete();
you should do something like this
ProcessBuilder pb = new ProcessBuilder( );
pb.command( "C:\\Windows\\System32\\cmd.exe", "/c",
"c:\\ffmpeg\\bin\\ffmpeg.exe", "-version" );
Process process = pb.start();
OutputStream stdOutput = process.getOutputStream();
InputStream inputStream = process.getInputStream();
InputStream errorStream = process.getErrorStream();
then it will work as your process is giving some output but you are not reading it

java processbuilder x264.exe

i want encoding a video with the x264 out of my own java application.
the problem is that the x264.exe start to work, and than it stop.
the process not terminate, but froze. i try to run the x264 from terminal, that works fine. the problem must be in java.
here my code:
String[] command= new String[]{"cmd","/c","x264.exe ....";
ProcessBuilder pb = new ProcessBuilder(temp);
Process p = pb.start();
int ev = 0;
if (p.waitFor() != 0)
{
ev = p.exitValue();
}
best regards,
paul
i use the following log to analyse my problem:
ProcessBuilder pb = new ProcessBuilder(temp);
File log = new File("log");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();int ev = 0;if (p.waitFor() != 0){ev = p.exitValue();}

Java ProcessBuilder: OutputStream(Reader) of a process never gets ready() for *certain* commands

I've written an interface to call shell commands from Java for testing purposes. For a few commands, that works quite fine, but for others, the OutputStream of the process never gets ready(). Does anyone have an explanation ? I give the full code:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.err.print("cmd: ");
String line=in.readLine();
The following are all fine for "cat -n", not for "sed s/a/e/"
ProcessBuilder pb = new ProcessBuilder(Arrays.asList(("bash -c \""+line+"\"").split(" ")));
// ProcessBuilder pb = new ProcessBuilder(Arrays.asList(("cmd /C "+line).split(" ")));
// ProcessBuilder pb = new ProcessBuilder("cmd","/C",line);
// ProcessBuilder pb = new ProcessBuilder("bash","-c",line);
// ProcessBuilder pb = new ProcessBuilder(Arrays.asList(line.split(" ")));
Interaction with the process:
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedWriter toP = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
BufferedReader fromP = new BufferedReader(new InputStreamReader(p.getInputStream()));
for(line = in.readLine(); line!=null; line=in.readLine()) {
toP.write(line+"\n");
toP.flush();
System.err.println("stdin: \""+line+"\"");
while(!fromP.ready()); // sed hangs, cat doesn't
System.out.println("result: \""+fromP.readLine()+"\"");
}
One can find plenty of information on ProcessBuilder issues, and for most of them, wrapping stdin and stdout into different Threads seems to be a solution. But if indeed this is the solution, then why ?
An further: Does anyone have an explanation why the straight-forward approach fails and under what circumstances this occurs ? From the example, I can rule out that it is the way the arguments are presented or the specific shell (cmd/bash).
I'm working with Java 1.6 on a Windows7 machine with Cygwin installed, hence both bash and cmd. Could that be a Cygwin issue ?

Categories

Resources