shp2pgsql, psql command not found through ProcessBuilder Java

一曲冷凌霜 提交于 2019-12-11 08:41:45

问题


The following command works well in command line

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

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

回答1:


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

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


来源:https://stackoverflow.com/questions/52296322/shp2pgsql-psql-command-not-found-through-processbuilder-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!