0

I have some console utility. I start process to execute this utility with some parameters. One parameter is filename.

If filename is not contains spaces, all worked. But with spaces I get error from utility: "no such file or directory". If I set filename parameter, still do not work. But interested: if I call utility from command line (not from android/java), all worked. If I replace space on %20, anyway do not work:

1) filename (original): util dir\some test.txt - not worked (java, cmd). It's normal.

2) filename (quates): util "dir\some test.txt" - work in cmd and do not work in java.

3) filename (encode): util "dir\some%20test.txt" - not worked (java, cmd).

PS: utility is ffmpeg

Yura Shinkarev
  • 5,134
  • 7
  • 34
  • 57

4 Answers4

0

Use dir\\some test.txt or dir/some test.txt in Java.

Please Note: \ is an special character(escape) in Java. If you need to use \ as literal as in your example, it should be escaped an addition \ character.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

When you are starting the program each space is registered as a new 'arg' so splitting it into spaces means that the program thinks there are more args and not reading the file name the way you want it to be read. The quotes only work in CMD because the syntax is designed to let it to so. You need an escape key try double backslash "\\ " and look here llegal-escape-character-followed-by-a-space.

Community
  • 1
  • 1
KosherBacon
  • 172
  • 1
  • 11
0

I assume that you are trying to run util "dir\some test.txt" in Java as follows:

System.exec("util \"dir\\some test.txt\"");

or something like that. If so, then the problem is in the simplistic way that the exec method splits the command string into arguments. It simply treats any sequence of white-space characters as argument separators irrespective of quoting. So, the above is actually passing the util command arguments "dir\some and test.txt" ... which will cause it to not find the files.

The best way to deal with this is to use the overload of exec where you pass an array of Strings; e.g.

System.exec(new String[]{"util", "dir\\some test.txt"});

For the record:

  • util "dir\some test.txt" works in CMD because the CMD language supports quoting using double quotes. You could do something similar with a POSIX shell.

  • util "dir\some%20test.txt" fails because the CMD language does not understand percent-escaping. Percent-escaping is used in URLs ... and unless the command in question expects a URL argument, it will treat the % character as an ordinary filename character.

Finally, "randomly" trying different kinds of quoting and escaping is not a good approach to problem solving. You should really try to understand the problem before you attempt to solve it ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I know about special chars. I do not set filename manual. I have some file, use getAbsoluteFile() and all backslashes ok.

I execute process as:

    ProcessBuilder pbuilder = new ProcessBuilder(cmd);
    Process proc = pbuilder.start();

Where cmd is my command: util filename.

UPD:

Now use ProcessBuilder with arguments String[]. Worked.

Yura Shinkarev
  • 5,134
  • 7
  • 34
  • 57