本文介绍了shp2pgsql,无法通过ProcessBuilder Java找到psql命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下命令在命令行中效果很好

The following command works well in command line

shp2pgsql -s 4326 /Users/abc.shp | psql -U user1 -h localhost -p 5432 -d postgis

但是,当我使用ProcessBuilder在Java中运行以下命令时,显示未找到命令.这是代码:

However when I run the following command in Java using ProcessBuilder it says command not found. Here is the code:

 public static void main(String arg[]) throws Exception {

    ProcessBuilder pb =
                   new ProcessBuilder("/bin/sh -c shp2pgsql /Users/abc.shp | psql -U user1 -h localhost -p 5432 -d postgis");
    Process p = pb.start();
    showOutput(p.getInputStream(), System.out);
    showOutput(p.getErrorStream(), System.err);

}

private static void showOutput(final InputStream src, final PrintStream dest) {
    new Thread(new Runnable() {
        public void run() {
            Scanner sc = new Scanner(src);
            while (sc.hasNextLine()) {
                dest.println(sc.nextLine());
            }
        }
    }).start();
 }

推荐答案

似乎Java无法理解您环境(psql或shp2pgsql ..)的路径在哪里

It seems that Java does not understand where the path of your environment (psql or shp2pgsql ..) is

您需要指定路径才能执行.通常在/usr/local/binusr/bin中.另外,请注意"/bin/sh"-c"的参数(此参数指定您要执行的命令为字符串格式)是分开的.只需修改以下代码段即可.它应该可以工作!

You need to specify the path so that it can execute. It is usually in /usr/local/bin or usr/bin. Also, note the argument for "/bin/sh and "-c" (this you specify the command your going to execute is in string format)is separate. Just modify the following snippet. It should work!!

String env = "/usr/local/bin/";
ProcessBuilder pb =
                   new ProcessBuilder("/bin/sh", "-c", env +"shp2pgsql /Users/abc.shp | "+env+"psql -U user1 -h localhost -p 5432 -d postgis");
    Process p = pb.start();

这篇关于shp2pgsql,无法通过ProcessBuilder Java找到psql命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:16