1

I want to execute this command

D:/Projects/GDAL/ogr2ogr.exe -f "MapInfo File" D:/Projects/GDAL/r/output_test.tab PG:"host=localhost user=postgres password=123456 dbname=postgis" -sql "SELECT * from filedata WHERE num=1"

I tried this:

Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "D:/Projects/GDAL/ogr2ogr.exe -f \"MapInfo File\" D:/Projects/GDAL/r/output_test.tab PG:\"host=localhost user=postgres password=123456 dbname=postgis\" -sql \"SELECT * from filedata WHERE num=1\""});

I got no errors, but nothing is happening. Where is my mistake?

Kliver Max
  • 5,107
  • 22
  • 95
  • 148

3 Answers3

4

Read this: http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and go through each suggestion step by step.

The Process API is notorious for gotchas.

Dan Gravell
  • 7,855
  • 7
  • 41
  • 64
1

You should add '/C':

Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "D:/Projects/GDAL/ogr2ogr.exe -f \"MapInfo File\" D:/Projects/GDAL/r/output_test.tab PG:\"host=localhost user=postgres password=123456 dbname=postgis\" -sql \"SELECT * from filedata WHERE num=1\""})
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

Try to check your return values:

String command = ""; // Your command
Process installproc = installcommand.execute();

StringBuilder out = new StringBuilder();
StringBuilder err = new StringBuilder();

installproc.waitForProcessOutput(out, err);
if (out.length() > 0) System.out.println("out:\n$out".toString());
if (err.length() > 0) System.out.println("err:\n$err".toString());

if(installproc.exitValue() != 0) {
throw new Exception("");
}

This is just sample code I did not test it in any way.

This should you give some hints about what the error is.

Christian
  • 3,503
  • 1
  • 26
  • 47