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}'" );
Related
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();
I need to run some node.js program using java
my code looks like this
String filePath= "/home/gilles/eclipse-workspace/informationGewinnungApp/videotool/src/videotool.js";
String option1 = "-m resources/WetterBerich";
String option2 = "--bg_content resources/logo.png";
ProcessBuilder Pb =
new ProcessBuilder("node",filePath+option1+option2);
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("/usr/bin"));
File log = new File("log");
// pb.redirectErrorStream(true);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
assert pb.redirectInput() == Redirect.PIPE;
assert pb.redirectOutput().file() == log;
assert p.getInputStream().read() == -1;
but I get this Error
module.js:549
throw err;
^
Mostly dupe: Cannot ProcessBuilder to execute command for ffmpeg -i shortWav.wav -af silenceremove=1:0:-50dB shortWavCued.mp3
You are putting multiple (all) options in ONE argument.
When you give a command line node this that to a shell, it passes this and that to node as separate arguments, and that is how node expects to receive them. ProcessBuilder does not split strings into separate arguments like a shell does, you must do it:
ProcessBuilder Pb = new ProcessBuilder("node",filePath,
"-m", "resources/WetterBerich",
"--bg_content", "resources/logo.png");
PS: using /usr/bin for your working directory is typically not a good idea.
I'm trying to get the output of grep linux shell command in java by using process builder. But i got a stuck in this case. Please help me.
Thank in advice!
String[] args = new String[7];
args[0] = "/bin/bash";
args[1] = "-c";
args[2] = "grep";
args[3] = "-n";
args[4] = "-e";
args[5] = "KERNELVERSION";
args[6] = kernelFilePath.trim();
ProcessBuilder pb;
Process process = null;
try {
pb = new ProcessBuilder(args);
pb = pb.directory(new File(directory));
pb.inheritIO();
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
process = pb.start();
process.waitFor();
} catch (IOException | InterruptedException e) {
System.out.println("executeCmdWithOutput() exception : " + e.toString());
} finally {
if (process != null) {
process.destroy();
}
}
==> Error:
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
I tried the command in bash and it worked fine:
grep -n -e KERNELVERSION ..../Makefile
Have you tried change the args[2] as full command?
Also, you can use pgrep, it does not require you to use pipe.
You don't need to explicitly run /bin/bash in order to execute the grep process. Just call it directly and ProcessBuilder will run it:
String[] args = {"grep", "-n", "KERNELVERSION", kernelFilePath.trim()};
Also, you don't need to use the -e option, unless there are multiple patterns that you are searching for.
If you really wanted to run grep in /bin/bash:
String[] args = {"/bin/bash", "-c", "grep -n KERNELVERSION " + kernelFilePath.trim()};
passes a single argument to bash containing the full command and arguments to execute.
I'd like to execute multiple commands with ProcessBuilder. I want to avoid using script files, only hardcoded strings in Java.
This is the current file that I'd like to execute.
#!/bin/sh
if [ -e /tmp/pipe ]
then
rm /tmp/pipe
fi
mkfifo /tmp/pipe
tail -f /dev/null >/tmp/pipe & # keep pipe alive
cat /tmp/pipe | omxplayer $1 -r &
Now, this is my current code.
private static final String[][] commands = {
{"rm", "-f", "/tmp/airpi_pipe"},
{"mkfifo", "/tmp/airpi_pipe"},
{"tail", "-f", "/dev/null", ">", "/tmp/airpi_pipe"}
};
public static void main(String[] args) throws IOException {
for (String[] str : commands) {
ProcessBuilder pb = new ProcessBuilder(str);
pb.redirectErrorStream(true);
Process process = pb.start();
InputStream is = process.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
System.err.println("next one");
}
}
Obviously, the tail command doesn't work in my ProcessBuilder. I haven't even tried with cat /tmp/pipe | omxplayer $1 -r &.
So, my question is, how could I manage to execute the content of my sh script with ProcessBuilder, but only with hardcoded commands (no script file), as I'm trying to do?
Thank you.
UPDATE
I had to use new ProcessBuilder("/bin/sh", "-c", "<commands>"); to make it work!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ProcessBuilder redirecting output
The following code:
ProcessBuilder pb = new ProcessBuilder(new String[] {"echo", "some text", ">", "test"});
keeps returning "some text > test".
What am I doing wrong?
EDIT:
this worked
ProcessBuilder pb = new ProcessBuilder(new String[] {"bash", "-c", "echo sometext > test"});
Try following
ProcessBuilder pb =
new ProcessBuilder("cmd.exe", "/c" ,"echo", "some text", ">", "test");
This is for windows
Actually 'Echo' is not a command its an internal command of the shell (cmd.exe) in windows and "bash" in linux or unix. So , for Unix/Linux
ProcessBuilder pb =
new ProcessBuilder("bash", "-c","echo \"some text\" >test");