TShopping

標題: How to use “cd” command using Java runtime on Linux? [打印本頁]

作者: woff    時間: 2014-7-24 02:35
標題: How to use “cd” command using Java runtime on Linux?
當用下面敘述時會錯誤
  1. String[] command = new String[]{"cd",path};
  2. Process child = Runtime.getRuntime().exec(command, null);
複製代碼
可改用
  1. String[] cmd = { "/bin/sh", "-c", "cd /var; ls -l" };

  2. Process p = Runtime.getRuntime().exec(cmd);
複製代碼
這樣就可以了

  1. String cmd = "cd /var; ls -l";

  2. Process p = Runtime.getRuntime().exec(new String{"/bin/sh","-c",cmd });
複製代碼

作者: 洋洋喜气    時間: 2014-12-3 15:23


   不会刷墙的吧





歡迎光臨 TShopping (http://www.tshopping.com.tw/) Powered by Discuz! X3.2