简体   繁体   中英

Extra psql command-line arguments using Java exec

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM