This discussion is archived
3 Replies Latest reply: Dec 13, 2012 6:08 AM by bdzevel RSS

Java commandline interfaces seem to have some limitations?

bdzevel Newbie
Currently Being Moderated
Hello,

The code I'm using used to use the "old-school" process stuff (Runtime.getRuntime().exec("...")) when I noticed this issue, but I have since moved over to the "new-school" ProcessBuilder class (ProcessBuilder.start()).

Basically, what I'm doing is dynamically generating a commandline command based on some user inputs. It happens to be a netezza command ("nzload"), but I don't think this really matters too much.

The command that was being generated is as follows:

nzload -host "ipaddress" -u "username" -pw "password" -db "databasename" -t "tablename" -securityLevel preferredSecured -df "/pretty/long/path/to/data/file.csv" -logFileSize 2000 -delim , -quotedValue NO
(P.S. I got that NZLOAD command by adding an "echo" in front of "nzload" in my processbuilder. The standard output, then, contained my command which I could copy/paste into a shell to test outside of Java.)

If I copy/paste this line into the shell on the machine this code is running on, it works perfectly; however (and remember, this line was generated by my code), running this using ProcessBuilder.Start() gives me weird errors. From the start, it seemed like the errors had something to do with the quotes. I've tried double-quotes, single-quotes, and eventually I took all of the quotes out. My experience with this so far is that if I have even one set of quotes in there, the NZLOAD utility returns strange errors like "can't make connection" or something about the security level not being correct. It seems to me that maybe the quotes are somehow getting mismatched in the Java so it's trying to take "more" than what I want it to for, say, the "hostname" parameter. Regardless, these errors are B.S. since I can run the exact same command in commandline with success (no nonsense copy/paste).

For reference, my processbuilder code is something like this:

ProcessBuilder pb = new ProcessBuilder();

pb.command().add("nzload");
String hn = this.Host;
*if (hn != null && hn.trim().length() > 0) {*
pb.command().add("-host");
pb.command().add(hn);
*}*

*if (user.trim().length() > 0) {*
pb.command().add("-u");
pb.command().add(user.trim());
pb.command().add("-pw");
pb.command().add(pass.trim());
*}*
*//...*
Process p = pb.start();
rv = p.waitFor();
*//grab the standard error / input and display it*
(Obviously this is a snippet without any quotes whatsoever. Beforehand, I had quotes around all of those fields, such as pb.command().add("\"" + hn + "\"");.)

1) How can I debug this issue so that it's more generic and easily understandable (instead of using our nzload command which none of you guys probably know anything about)?

2) Are there any known limitations/issues with running shell commands using Java?

Thank you guys!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points