TShopping

 找回密碼
 註冊
搜索
TShopping 精選文章 Android 手機開發 查看內容

How to use “cd” command using Java runtime on Linux?

2014-7-24 02:38| 發佈者: woff| 查看: 409| 評論: 2|原作者: woff

摘要: 當用下面敘述時會錯誤String command = new String{cd,path}; Process child = Runtime.getRuntime().exec(command, null);複製代碼可改用String cmd = { /bin/sh, -c, cd /var; ls -l }; Process p = Runtime.getR ...
 
當用下面敘述時會錯誤
  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


   不会刷墙的吧

查看全部評論(2)



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2017-11-20 17:54 , Processed in 0.037997 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回頂部