Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.exec() method not executing correct command?

 
Kevin Brydges
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building a simple app to execute a database application (UNIX) that takes several parameters. The full text of the command I'm executing is below:

tbuild -f /homecses/micsedev/mods/tpt/case_coop_stage.tpt -v /homecses/micsedev/connections/cse_batch_drbc.con -R /homecses/micsedev/logfiles/tbuildlogs/chkpoint -L /homecses/micsedev/logfiles/tbuildlogs -u "FloadIn='/homecses/micsedev/data/in/case_coop.dat'" case_coop_stage


When I paste this exact command into my OS, it runs without issue. However, if I use this exact command as a string in my Java app and pass it into Runtime.getRuntime().exec([command]), I get an error message back from the tbuild application which indicates that certain arguments are getting removed. Specifically, it appears that my "FloadIn" value is not being used (the value passed to the -u switch). That value requires that the double quote characters be included (and when I build the command, I make sure to escape the quote characters, and have verified this through println()).

I know that commands are broken by whitespace when passed to the exec() method as a full string, but with this command there is no additional whitespace to break on. How can I tell exactly what command is actually being passed to my OS? After building the above command, I have the following code:



Again, when I manually execute the command that the println() prints, I have no issue. But when the exec() method uses that command, it fails (not through Java, but through the tbuild program I am trying to execute).

Any ideas?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post your exact code on how you build the string?
 
Kevin Brydges
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this example, args[0] is "case_coop".



I also tried using a ProcessBuilder, and had the exact same results:

 
Stephan van Hulst
Bartender
Pie
Posts: 5415
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the exec() documentation: http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String[]).

The parameters need to be added as separate array elements.

[edit]

Forum breaks my links -_-;
 
Kevin Brydges
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Take a look at the )]exec() documentation. The parameters need to be added as separate array elements.


That's why I tried the ProcessBuilder method, which gave the same error result.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure you still need those quotes in "\"FloadIn='" + DAT_PTH + "/" + args[0] + ".dat'\"". I think that those quotes are for the shell to indicate the following should be treated as one single parameter, and not be processed. Try removing them:
 
Kevin Brydges
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I'm not sure you still need those quotes in "\"FloadIn='" + DAT_PTH + "/" + args[0] + ".dat'\"". I think that those quotes are for the shell to indicate the following should be treated as one single parameter, and not be processed. Try removing them:


That was it! Thank you very much everyone.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic