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