*
The moose likes Beginning Java and the fly likes Runtime.exec() method not executing correct command? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime.exec() method not executing correct command?" Watch "Runtime.exec() method not executing correct command?" New topic
Author

Runtime.exec() method not executing correct command?

Kevin Brydges
Greenhorn

Joined: Feb 01, 2008
Posts: 4
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

Joined: Oct 02, 2003
Posts: 11155
    
  16

can you post your exact code on how you build the string?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kevin Brydges
Greenhorn

Joined: Feb 01, 2008
Posts: 4
In this example, args[0] is "case_coop".



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

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3595
    
  14

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

Joined: Feb 01, 2008
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kevin Brydges
Greenhorn

Joined: Feb 01, 2008
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Runtime.exec() method not executing correct command?
 
Similar Threads
How can I clear the screen?
Runtime.exec()
runtime to clear console
Retrieving classes of a package
Mysql database restore through java application...?