I have below linux command running through java program , where input file name has space in it , while executing system fails
java.io.IOException: Cannot run program "tsp -I afmarker": error=2, No such file or directory
Command :
String[] commandArr = new String[] { "tsp -I afmarker", "/home/test/prad test.mpg" "-P afmarker -a 10 -v 20 -O file", "/home/prad/output.mpg};
Process process = Runtime.getRuntime().exec(commandArr);
How do I solve this problem?
When invoked with an array of Strings, Runtime.getRuntime().exec() expects the first element of the array to be the name of the executable, without any parameters. If I am not mistaken, your code is instructing your JVM to execute a command called tsp -I afmarker, and such command does not exist. All parameters to tsp should be specified separately as elements of the array passed to exec().
Try separating each argument instead:
String[] commandArr = new String[] {
"tsp", "-I", "afmarker",
"/home/test/prad test.mpg",
"-P", "afmarker", "-a", "10", "-v", "20",
"-O", "file", "/home/prad/output.mpg"
};
Related
Wen i try to run this code :
String[] cmd = {"/bin/bash", "-c", "printf '%s'\n"+videoPath+"./"+"*.mp4 >"+"mylist.txt"};
processBuilder.command(cmd);
I get some error:
/bin/bash: line 1:
/home/gilles/eclipse-workspace/informationGewinnungApp/videotool/outputs/./info.mp4:
cannot execute binary file: Exec format error 126
The \n in your string is expanded into a newline. Hence bash sees two commands,
printf %s
..../info.mp4
Do it either as
String[] cmd = {"/bin/bash", "-c", "printf '%s' "+videoPath+"./"+"*.mp4 >"+"mylist.txt"};
Or
String[] cmd = {"/bin/bash", "-c", "echo "+videoPath+"./"+"*.mp4 >"+"mylist.txt"};
But: Why don't you want to use a bash child process, if you only want to create a new file containing a certain string? Wouldn't it be easier to do it directly from Java?
I'm working with the java ProcessBuilder class to start an instance of nmap on my windows workstation.
The following code produces an exception:
java.io.IOException: Cannot run program "C:\Program Files (x86)\Nmap\nmap.exe -T4 -A -v --max-scan-delay 0ms --min-rate 1000000 --max-retries 0 -oX - 192.168.1.1 ": CreateProcess error=2, The system cannot find the file specified
The value of command is:
C:\Program Files (x86)\Nmap\nmap.exe -T4 -A -v --max-scan-delay 0ms --min-rate 1000000 --max-retries 0 -oX - 192.168.1.1
Running the command is generated right in a command window executes properly.
Any ideas?
String command = this.getCommand().toString();
ExecutionResults results = new ExecutionResults();
ProcessBuilder procBuilder = new ProcessBuilder(new String[]{command.toString()});
try {
Process e = procBuilder.start();
results.setErrors(this.convertStream(e.getErrorStream()));
results.setOutput(this.convertStream(e.getInputStream()));
You're passing the whole command (including parameters) as a single parameter of the ProcessBuilder constructor. It takes a String[], where the first item should be the path to the executable, and the other items should be the parameters. Try
command.split(" ")
instead of
new String[]{command.toString()}
Edit: I see that you have spaces in your path, that will break it :( you could try splitting the executable path and the arguments into two strings. And the constructor argument will be a String[] containing the path itself as the first item and then an array of the parameters split by space.
OR: if you don't mind not using the ProcessBuilder.. this is much simpler:
Process e = Runtime.getRuntime().exec(command);
It is simple from the exception itself,
java.io.IOException: Cannot run program "C:\Program Files (x86)\Nmap\nmap.exe -T4 -A -v --max-scan-delay 0ms --min-rate 1000000 --max-retries 0 -oX - 192.168.1.1 ": CreateProcess error=2, The system
Process builder taking the whole line thinking it is as an executable and trying to run that. Just use the below example,
ProcessBuilder procBuilder = new ProcessBuilder(new String[]{"C:\Program Files (x86)\Nmap\nmap.exe"});
procBuilder.start();
This will work fine. So this is not an issue what you think that java is unable to find the executable. It is taking whole line as an executable. Better you do like below,
ProcessBuilder procBuilder = new ProcessBuilder(new String[]{"C:\Program Files (x86)\Nmap\nmap.exe", "-T4", "-A"}); //add all params
procBuilder.start();
Try this, it should work fine
As the other answers have indicated, your immediate problem is that you're passing an entire command line as if it's the name of a program to run. CreateProcess is looking for a program named "C:\Program Files (x86)\Nmap\nmap.exe -T4 -A etc" and failing to find it.
If you have a string containing a command to run (a program name with arguments, redirection, and so on), then the simplest way to run it is to launch it via the command line processor. Here's a simple example of doing that:
public static void main(String[] args) throws IOException, InterruptedException {
String command = "dir /w";
ProcessBuilder procBuilder = new ProcessBuilder(new String[]{"cmd", "/c", command});
procBuilder.redirectInput(Redirect.INHERIT);
procBuilder.redirectOutput(Redirect.INHERIT);
procBuilder.redirectError(Redirect.INHERIT);
Process p = procBuilder.start();
int ecode = p.waitFor();
System.err.println("Exit code " + ecode);
}
It might be because of a couple of reasons from my experience
Firewall would be removed certain files necessary for the installer. So would suggest to uninstall and reinstall
Can also be because of spaces in the folder path, would suggest to install in new folder which does not have spaces in the path.
I have an application that has a directory on the SD card. The application saves notes in a new subdirectory. I want to delete the whole subdirectory using shell command "rm -r" but the application throws an exception:
04-02 23:14:23.410: W/System.err(14891): java.io.IOException: Error running exec(). Command: [cd, /mnt/sdcard/mynote, &&, rm, -r, aaa] Working Directory: null Environment: null
Can anyone help me?
This happens because you used Runtime.exec(String). Never use this function. It's hard to predict and only works in trivial cases. Always use Runtime.exec(String[]).
Since cd and && are not commands but shell features, you need to manually invoke a shell for them to work:
Runtime.getRuntime().exec(new String[] {
"sh", "-c", "cd /mnt/sdcard/mynote && rm -r aaa"
});
On a related note, you should never pass String data unescaped to shells. For example, this is wrong:
// Insecure, buggy and wrong!
String target = "aaa";
Runtime.getRuntime().exec(new String[] {
"sh", "-c", "cd /mnt/sdcard/mynote && rm -r " + target
});
The correct way is to pass data as separate parameters to the shell, and reference them from your command:
// Secure and correct
String target = "aaa";
Runtime.getRuntime().exec(new String[] {
"sh", "-c", "cd /mnt/sdcard/mynote && rm -r \"$1\"", "--", target
});
For example, if a file is named * or My file, the incorrect version will delete a whole bunch of completely unrelated files. The correct version does not.
I am using Runtime.getRuntime().exec() to run a shell script from Java code. The code works fine when I pass the parameter as string
Runtime.getRuntime().exec("sh test.sh")
Since I have to pass additional arguments which are paths with spaces, so I replaced String with String array.
String[] cmd = {"sh test.sh", "/Path/to my/resource file"};
Runtime.getRuntime().exec(cmd)
I also tried with
String[] cmd = {"sh test.sh"};
Runtime.getRuntime().exec(cmd)
But neither of them worked. It's throwing an exception:
java.io.IOException: Cannot run program "sh test.sh":
java.io.IOException: error=2, No such file or directory
Why is the same script file when passed as String worked and when used with String array is throwing exception? How can I make this work with string array as argument to Runtime.exec()?
First string became the command. There is no file 'sh test.sh' to be executed.
Change
String[] cmd = {"sh test.sh", "/Path/to my/resource file"};
to
String[] cmd = {"sh", "test.sh", "/Path/to my/resource file"};
(In general use process builder API)
I want to run nm command in linux through java.
I tried this code :
command = "nm -l file1.o > file1.txt";
Process p = Runtime.getRuntime().exec(command);
But it's not working, what is wrong with the code?
That is not an executable, it is in fact a shell script.
If you invoke the shell with -c, then you can execute your command:
/bin/sh -c "command > here"
Here's what you need to do:
String command = "nm -l file1.o > file1.txt";
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command});
The following "simple answer" WON'T WORK :
String command = "/bin/sh -c 'nm -l file1.o > file1.txt'";
Process p = Runtime.getRuntime().exec(command);
because the exec(String) method splits its the string naively using whitespace as the separator and ignoring any quoting. So the above example is equivalent to supplying the following command / argument list.
new String[]{"/bin/sh", "-c", "'nm", "-l", "file1.o", ">", "file1.txt'"};
An alternative to pipe would be to read the stdout of your command, see Java exec() does not return expected result of pipes' connected commands for an example.
Instead of redirecting the output using "> file.txt" you would read whatever the output is and write it to a StringBuffer or OutputStream or whatever you like.
This would have the advantage that you could also read stderr and see if there were errors (like no space left on device etc.). (you can also do that using "2>" using your approach)