2

I am new to Java and trying to convert one of my project from C to Java in order to combine it with another Java program. I'm having difficulty to get correct result when I use Runtime.exec(). I have the following program segment and Java ignores to process the given command.

command1 = "mv output/tsk/dir1/metabolic\\ waste.txt output/converted/file16.txt";                                               
r2 = Runtime.getRuntime();
p2 = r2.exec(command1);
p2.waitFor();

The problem here is the filename "metabolic waste.txt". The same command work when there is no space. I know I have to use escape char for space and I do it. I'm working on Ubuntu btw.

I also tried using

String[] command1 = new String[] {"mv output/tsk/dir1/metabolic\ waste.txt", "output/converted/file16.txt";

but it didn't work.

p.s. the given code is just an example. I don't only use linux mv command. I also run some of the command line tools such as pdf2txt. I still have the same problem of running commands if there is any space in the filename.

SOLVED: I've solved my problem. It's ridiculous that I had to remove escape character and use string array. So, NO ESCAPE CHARACTER for space. The following code just worked for this example and for more general.

source_filepath = "output/tsk/dir1/metabolic waste.txt";
dest_filepath = "output/converted/file16.txt";
String[] str2= {"mv", source_filepath, dest_filepath};
r2 = Runtime.getRuntime().exec(str2);
p2.waitFor();
Lorderon
  • 131
  • 1
  • 3
  • 13

2 Answers2

0

You can enclose the filename in double quotes as follows :

  String srcFile = "output/tsk/dir1/metabolic\\ waste.txt"
  command1 = "mv " + srcFile +" output/converted/file16.txt";                     
Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • Can i ask you why you are not using the FIle API for doing this task ? – Kakarot May 14 '14 at 16:45
  • @Karakot This is just an example of my error. As suggested above, I could use simpler techniques but I actually run some command line tools. I just gave a simple example of my problem. For example, I have the same problem for running the following command: filePath = "file\\ name\\ has\\ space.docx"; command1 = "docx2txt.sh " + filePath; r2 = Runtime.getRuntime(); p2 = r2.exec(command1); p2.waitFor(); – Lorderon May 14 '14 at 16:54
  • So this output folder is inside the folder from where you are running this piece of code ? – Kakarot May 14 '14 at 18:45
  • Yes, it's. There is nothing wrong with the file path. I've tested the command1 on terminal it works perfectly fine. It doesn't work when pass it from my Java code. – Lorderon May 14 '14 at 19:02
  • I tried it too : I am getting the following error mv: target `output/converted/file16.txt' is not a directory – Kakarot May 14 '14 at 19:04
  • I think you need a clear example. For example, can you get the following code running assuming you have a file named "hello world.txt": 'Runtime r = Runtime.getRuntime.exec("cp /pathTo/hello\\ world.txt") /home/username/Desktop/' – Lorderon May 14 '14 at 19:18
0

You have to escape the escape, or enclose the path in quotes:

String[] command1 = new String[] {"mv output/tsk/dir1/metabolic\\ waste.txt", "output/converted/file16.txt"};
String[] command1 = new String[] {"mv \"output/tsk/dir1/metabolic waste.txt\"", "output/converted/file16.txt"};

You have to use \\ because java also uses \ as an escape character, so "\\" really just contains one \

clcto
  • 9,530
  • 20
  • 42