3

I'm doing some benchmark testing of command line execution of postgres queries vs. JDBC using the Lehigh Benchmark Dataset as test data, including their test queries.

Everything else is working fine, except when I try to execute a single string query using psql.

The query is the string I'm creating and executing using

Runtime.getRuntime().exec("time ./psql  -U lehigh -d lehigh  -c 'SELECT u.id FROM undergraduatestudent u;';")

It works fine on the mac command line, but when run from within Java, I get the following errors:

---> psql: warning: extra command-line argument "u.id" ignored
---> psql: warning: extra command-line argument "FROM" ignored
---> psql: warning: extra command-line argument "undergraduatestudent" ignored
---> psql: warning: extra command-line argument "u;';" ignored
---> ERROR:  unterminated quoted string at or near "'SELECT"
---> LINE 1: 'SELECT
--->         ^

Essentially everything after "SELECT" isn't being recognized as part of the query. There are 14 queries where this is happening, but this is the shortest one, and will suffice to solve the problem.

I've tried moving the -U and -d to afterwards, as was suggested in a different post, but I got the same results.

I'm running this in Eclipse on a Mac in Capitan (though I don't believe either of these things are having an effect on things).

I'm aware of the -f option, but I have to time this per query, so I prefer the -c option.

Jason St. Jacques
  • 383
  • 1
  • 3
  • 14
  • Does this resemble your problem ? http://stackoverflow.com/questions/8413254/using-quotes-and-double-quotes-in-java-runtime-getruntime-exec – Marged Dec 28 '15 at 23:48
  • No it doesn't. I did see that one and tried different combinations of quotes to make sure it wasn't related. I believe this to be a java specific problem. – Jason St. Jacques Dec 29 '15 at 13:34
  • Try to call this with any other program which accepts random parameters and then call ps to see how Java handles this – Marged Dec 29 '15 at 13:43

1 Answers1

0

As far as I am aware, exec(String) uses spaces as a delimiter, so your query isnt being treated as a single arguments, but rather as a set of arguments ('SELECT, u.id, FROM, undergraduatestudent, and u;').

Try using the exec(String[]) version, where the String[] is like this:

{"time", "./psql", "-U", "lehigh", "-d", "lehigh",  "-c", "'SELECT", "u.id", "FROM", "undergraduatestudent u;'"}`

Or, better yet, use a ProcessBuilder:

new ProcessBuilder(new String[]{"time", "./psql", "-U", "lehigh", "-d", "lehigh",  "-c", "'SELECT", "u.id", "FROM", "undergraduatestudent u;'"}).start();
Jarrett Spiker
  • 361
  • 3
  • 13
  • This ended up working, though it's not returning the system command line times that I was interested in. I'm using Java's stopwatch though, and the difference is insignificant in terms of my measurement anyway. Still not sure why my other string queries of similar style are working fine as they are, but I'm going to change them all to use ProcessBuilder for consistency's sake. One thing I overlooked, but figured out was making sure there are no spaces on any of the substrings in the Array. ProcessBuilder doesn't like that. Thanks again. – Jason St. Jacques Dec 30 '15 at 00:50
  • No problem, sorry to hear it isnt returning what you expected...Im afraid I dont know how to fix that. I should have pointed out that you have to watch out for spaces in the array, I had this same problem the day before you posted an didnt think to mention it for some reason... – Jarrett Spiker Dec 30 '15 at 05:08