Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javaexec的额外psql命令行参数_Java_Postgresql_Command Line_Psql - Fatal编程技术网

使用javaexec的额外psql命令行参数

使用javaexec的额外psql命令行参数,java,postgresql,command-line,psql,Java,Postgresql,Command Line,Psql,我正在使用Lehigh基准数据集作为测试数据,对postgres查询的命令行执行与JDBC进行一些基准测试,包括它们的测试查询 除了尝试使用psql执行单个字符串查询外,其他一切都正常工作 查询是我正在创建和执行的字符串 Runtime.getRuntime().exec("time ./psql -U lehigh -d lehigh -c 'SELECT u.id FROM undergraduatestudent u;';") 它在mac命令行上运行良好,但在Java中运行时,会出现

我正在使用Lehigh基准数据集作为测试数据,对postgres查询的命令行执行与JDBC进行一些基准测试,包括它们的测试查询

除了尝试使用psql执行单个字符串查询外,其他一切都正常工作

查询是我正在创建和执行的字符串

Runtime.getRuntime().exec("time ./psql  -U lehigh -d lehigh  -c 'SELECT u.id FROM undergraduatestudent u;';")
它在mac命令行上运行良好,但在Java中运行时,会出现以下错误:

---> 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
--->         ^
基本上,“SELECT”之后的所有内容都不会被识别为查询的一部分。有14个查询发生了这种情况,但这是最短的一个,足以解决问题

我试着把-U和-d移到后面,正如在另一篇文章中建议的那样,但我得到了相同的结果

我在Capitan的Mac上运行Eclipse(尽管我不相信这两种东西都会对事情产生影响)

我知道-f选项,但我必须为每个查询计时,所以我更喜欢-c选项。

据我所知,exec(String)使用空格作为分隔符,因此您的查询不会被视为单个参数,而是一组参数(“SELECT、u.id、FROM、undergradestudent和u;”)

尝试使用exec(字符串[])版本,其中字符串[]如下所示:

{"time", "./psql", "-U", "lehigh", "-d", "lehigh",  "-c", "'SELECT", "u.id", "FROM", "undergraduatestudent u;'"}`
或者,最好使用ProcessBuilder:

new ProcessBuilder(new String[]{"time", "./psql", "-U", "lehigh", "-d", "lehigh",  "-c", "'SELECT", "u.id", "FROM", "undergraduatestudent u;'"}).start();

这和你的问题相似吗?不,没有。我确实看到了,并尝试了不同的引用组合,以确保它没有关联。我认为这是一个java特有的问题。试着用任何其他接受随机参数的程序调用它,然后调用ps,看看java如何处理这个问题,尽管它没有返回我感兴趣的系统命令行时间。不过,我使用的是Java的秒表,无论如何,就我的测量而言,差异是微不足道的。仍然不确定为什么我的其他类似样式的字符串查询可以正常工作,但为了一致性起见,我将把它们全部更改为使用ProcessBuilder。有一件事我忽略了,但我发现了,那就是确保数组中的任何子字符串上都没有空格。ProcessBuilder不喜欢这样。再次感谢。没问题,很抱歉听到它没有返回您所期望的…恐怕我不知道如何修复它。我应该指出的是,你必须注意数组中的空格,在你发布一篇文章的前一天,我也遇到了同样的问题,因为某种原因,我不想提及它。。。