簡體   English   中英

如何在Java中執行cmd命令?

[英]How to execute cmd command in java?

我要執行此命令

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"

我嘗試了這個:

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\""});

我沒有任何錯誤,但是沒有任何反應。 我的錯誤在哪里?

閱讀以下內容: http : //www.javaworld.com/jw-12-2000/jw-1229-traps.html並逐步完成每個建議。

Process API臭名昭著。

您應該添加“ / 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\""})

嘗試檢查您的返回值:

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

這只是示例代碼,我沒有進行任何測試。

這應該給您一些有關錯誤的提示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM