0

file = D:\Unix\tr.exe "Æ" "~" < "C:\SourceFiles\source.csv" > "D:\tgt"

When i execute this command using the below code in java

Process process = Runtime.getRuntime().exec(file);

am getting the following error

D:\Unix\tr.exe: too many arguments

PS : File contains Æ characters am trying to replace all those characters with ~

Any suggestions please ?

sri
  • 29
  • 1
  • 9
  • could you please post exactly what you feed into the exec command? The string literal constructed in the code sample will not work for java. – thst Apr 07 '15 at 13:13
  • @thst ya i knw, it ll look clumsy that's why i ve give like tat. Below i ve given the actual code, String strBatchFileName = "D:\\eclipse\\lib\\Unx\\tr.exe \"Æ\" \"~\" < \"C:\\SourceFiles\\source.csv\" > \"D:\\copy_A_104\""; – sri Apr 07 '15 at 13:19
  • Why don't you pass an array to avoid having to worry about quotes? – Martin Wilson Apr 07 '15 at 13:22
  • 1
    Did you solve your issue? It is good practice to accept a helping answer or to provide more details if the issue remains unsolved. – thst Apr 08 '15 at 07:55

2 Answers2

0

You have to program the io redirection yourself. This is usually the bash that takes care of it.

The result of the exec() call is a process. This process can then be used to get the STDIN of the process. Send the data to that process using that stream.

The way you call it, you send another command line option to tr, which is the < and > redirectors and the filenames.

 Process process = System.getRuntime().exec(strBatchFileName);
 OutputStream stdin = process.getOutputStream();
 sendFileToStream(out);
 InputStream stdout = process.getInputStream();
 loadResultFromStream(stdout);

This is a pseudocode example, where sendFileToStream(...) feeds the input file into the TR process, and the loadResultFromStream(...) will load the result.

You may need to utilize threads to feed and read if the data is larger than the stream's buffer.

The question is kind of a duplicate and you will find a ProcessBuilder example here: Runtime's exec() method is not redirecting the output

Community
  • 1
  • 1
thst
  • 4,592
  • 1
  • 26
  • 40
0

You're passing unicode in the CMD, I guess that causes the problem. Set chcp xxx on the CMD and try.

refer below link for chcp codes

https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/chcp.mspx?mfr=true

Saravana
  • 12,647
  • 2
  • 39
  • 57