Java ProcessBuilder Error When Space(s) In Path - java

So I spent half a day trying to get this to work with no positive result. I am using a Java ProcessBuilder to execute some .exe file with a couple of arguments, but the file-path contain space(s) and somehow I can't get it to work properly. I have checked a number of other SO posts and implemented solutions like surround code with escaped quotes and splitting it up in command and arguments etc. My code is below:
try {
ProcessBuilder pBuilder = new ProcessBuilder(
// Main Command.
"C:\\namewith space\\database\\postgres_db\\bin\\pg_ctl.exe",
// Command Parameters.
"start",
"-D C:\\namewith space\\database\\database",
/*The quotes in the next argument are necessary, the -o stands for 'options' and everything between the quotes are the actual database parameters which to start the Database with.*/
"-o \"-p 15000\"",
"-l C:\\namewith space\\database\\postgres_db\\bin\\postgres_log.txt");
File log = new File("\"C:\\folder\\log.txt\"");
pBuilder.redirectErrorStream(true);
pBuilder.redirectOutput(Redirect.appendTo(log));
Process p = pBuilder.start();
} catch (IOException ex) {
System.out.println("Exception Occurred: " + ex);
}
I have tried so far:
Surrounding and not surrounding each/any of the paths in the above code with escaped quotes just in case that matters (something tells me it does...).
Using the Runtime.getRuntime().exec("full command with/without any/all escaped quotes"); method, but when searching on SO I found out everyone is saying you should use the ProcessBuilder instead.
Adding parts of the above code together in different ways in the ProcessBuiler's first command String, like "\"C:\\namewith space\\database\\postgres_db\\bin\\pg_ctl.exe\" start"
The files are 100% located at the given paths, I checked this by hand and by pasting the paths in the File Explorer over 10 times.
Splitting up the command into setting the working directory of the command to C:\namewith space\ and then adding the args without that part.
The error (via the System.out.println("Exception Occurred: " + ex); ) I keep getting is: java.io.IOException: Cannot run program "C:\namewith space\database\postgres_db\bin\pg_ctl.exe": The filename, directory name, or volume label syntax is incorrect.
Please let me know if you need any extra parts/code and I will do my best to provide it in detail.

Try to break the problem down.
First just read the absolute path shown in the IOException into a File object and call the exists() method to check that the file really exists and the JVM has access to it.
If that didn't work, fix your path or the access permissions. If the file really exists and you can access it then create the ProcessBuilder without any parameters, just with absolute path to your exe.
You shouldn't get the IOException now. Then add the parameters one by one. If you find one that breaks the thing, then fix the parameter (maybe the double quotes are missing) and go onto the next until you finish.

Related

Execute shell command from java in specific folder

I have a C code which I have compiled and added to path in order to be able to execute it form anywhere (I've double checked that I can do that)
Now I want to do a GUI to work with it in an easier way. I ask the user to input a file and an output directory.
In a click button I put the code to execute the command from the GUI:
String command = "myprogram -e " + file;
new ExecuteShellInstruction().main(command,jTextOutputDirectory.getText());
I execute the code in other class:
p = Runtime.getRuntime().exec(command, null, new File(directory));
But I always get this error:
java.io.IOException: Cannot run program "myprogram" (in directory "/Users/user_name/Documents/folder/example"): error=2, No such file or directory
I've checked that if I write exactly the same from the same folder there is no problem.
Any idea of what I'm doing worng?, If obtained this way of doing it from a question which was marked as correct, maybe I'm missing something, but I've already been 1 hour trying things and nothing seems to work.
Thank you!
Finally I found a solution.
It seems like you have to tell that your application can be executed by adding "./" at the beginning. Something like this:
String command = "./myprogram -e " + file;

Run batch File using Java (space in arguements issue)

I am trying to run a batch file from Java. The following works:
Process p1 = java.lang.Runtime.getRuntime().exec("D:\\Users\\xx\\Documents\\NetBeansProjects\\Test1\\New folder\\batch.bat");
The following does not work:
Process p1 = java.lang.Runtime.getRuntime().exec("D:\\Users\\xx\\Desktop\\Jar Test\\New folder\\batch.bat");
The error I get is "Windows cannot find 'D:\Users\xx\Desktop\Jar'. Make sure you typed the name correctly, and then try again."
Although there are spaces in both paths, for some reason the second one is not running. I have read numerous threads about escaping spaces, but none of the solutions there worked with me.
Note: I have tried to use process builder but I am also facing the same issue.
Try
Process p1 = Runtime.getRuntime().exec(new String[] {"D:\\Users\\xx\\Documents\\NetBeansProjects\\Test1\\New folder\\batch.bat"});
This form of exec() wont tokenize your input by spaces for you.

Runtime.exec(String) limiting String

I'm trying to use the Java function Runetime.exec(String) to run a program in the startup folder of a windows 7 computer like so:
Runtime.getRuntime().exec(runner.getPath() + "\\run.bat");
And when I run this I get an error saying the command cannot be run:
Exception in thread "main" java.io.IOException: Cannot run program ""C:\Users\ly
ndsey\AppData\Roaming\Microsoft\Windows\Start": CreateProcess error=2, The syste
m cannot find the file specified
As you can see, the file name is cut off at the "\Windows\Start" when it should continue to "\Windows\Startup\run.bat".. Is there an alternative I can use?
Considering runner as a File instance, this should work.
Desktop.getDesktop().open(new File(runner, "run.bat"));
It uses Desktop class instead of Runtime, so you don't have to convert your File (runner) to its String representation (which is error prone). Runner is now used 'as is' as the parent directory of the "run.bat" you want to execute.
Other advantage of Desktop class : you can now open any file you want.
As an alternative you can use ProcessBuilder. I feel ProcessBuilder is more safe than Runtime.getRuntime().exec http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
String[] command = {"CMD", "/C", "dir"};
ProcessBuilder pb = new ProcessBuilder( command );
//set up your work directory if needed
pb.directory(new File("c:\\path"));
Process process = pb.start();
as i can see from the error you give, and i hope it's a copy past, you string runner.getPath() for some reason start and end with "\"" which make the whole path invalid. check that and remove it if needed
if you have the file already and you just need it's path you can use
runner.getAbsolutePath()
also, if runner is a file, getPath will give you the file path including the path, so your code will surely won't work. instead use:
String path = runner.getPath();
path = path.substring(0, path.lastIndexOf("\\")) + "\\run.bat";
Runtime.getRuntime().exec(path);
You should avoid the exec(String) method, which attempts to parse the entire string into command + arguments. The safe option is exec(String[]), which presupposes the first array element is the command and the rest are arguments.
So, writing
Runtime.getRuntime.exec(new String[] { yourCommandString })
is a surefire way of getting the right message across.

How to read/open files with space in name

I'm trying to develop a server for mplayer using Java but I can't open files that have spaces in name (e.g. "File with space.mp3").
I'm following this tutorial here. The problem is, every time I try to open a file with spaces in name the getInputStream() read only the string before the space, generating a "file not found" error.
The path are correct in command, I tried even different formats (e.g. "File\ with\ space.mp3", "$PATH/File with space.mp3", etc), but nothing works.
What can I do to get data properly from getInputStream? How to avoid getInputStream to block when it founds a space in the String?
Ps. I use a linux system and the codes are the same as the link above (ctrl+c , ctrl+v).
thanks for the help.
The problem is the use of Runtime#exec. It thinks that the space in the file is another parameter.
Process mplayerProcess = Runtime.getRuntime().exec("/path/to/mplayer -slave -quiet -idle file/to/play.avi");
Instead, you should use ProcessBuilder which allows you to specify each parameter as a separate String eliminating the need to mess about with quotes.
ProcessBuilder pb = new ProcessBuilder("/path/to/mplayer", "-slave", "-quiet", "-idle", "file/to/play.avi");
// Other configuration options...
Process p = pb.start();

Redirection with Runtime.getRuntime().exec() doesn't work

I need to execute a command from a program. The command line is ok, I tried it in the terminal, but it doesn't work in the program.
I add a copy from my code:
File dir = new File("videos");
String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
System.out.print("No existe el directorio\n");
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
//Recojo el momento exacto
System.out.print("\n" +filename);
Process p = Runtime.getRuntime().exec("exiftool -a -u -g1 -j videos/"+filename+">metadata/"+filename+".json");
}
The program must get the name of all of the files in a folder (filename) and extract the metadata of theese videos, writting them on a .json files in the folder 'metadata'.
Where is the problem?
The problem is, the redirection character (>) is a shell-based construct, not an executable. So unless you're running this command through something like bash (which you're not), it's going to be interpreted as a literal character argument to your exiftool invocation.
If you want to get this to work, you have two options:
Get bash to do it - pass the whole command line as an argument to bash -c. This might need some heroic escaping, although in your case it looks OK.
Do the redirection yourself within Java. Invoke the command without the redirected output (i.e. everything up to the > sign), then read from the process' outputstream and write all the contents to the appropriate file.
The latter approach sounds like more work initially, but when you consider that you need to always read a Process' output anyway (see the javadocs, second paragraph), it's actually very little extra on top of that. You're simply sending this output to a file instead of throwing it away.
If you have Java 7, it's easier:
Process p = new ProcessBuilder()
.command("exiftool", "-a", "-u", "-g1", "-j",
new File("videos", filename).toString())
.redirectOutput(new File("metadata", filename + ".json"))
.start();
This falls under "solution 2", but the runtime library takes care of the boilerplate.

Categories

Resources