gpt4 book ai didi

java - 使用 Runtime.getRuntime().exec 从定义的目录执行文件

转载 作者:太空狗 更新时间:2023-10-29 23:01:27 31 4
gpt4 key购买 nike

我只想从特定文件夹执行我的文件。在我的例子中/data/data/my-package/files/。所以我试过了:

 Process process2=Runtime.getRuntime().exec("cd /data/data/my-package/files/");
process2.waitFor();
process2=Runtime.getRuntime().exec("./myfile");

这是行不通的。谁能告诉我这样做的正确方法。谢谢

最佳答案

应该可以使用 Runtime.exec(String command, String[] envp, File dir) 调用具有特定工作目录的可执行文件

如下:

Process process2=Runtime.getRuntime().exec("/data/data/my-package/files/myfile",
null, new File("/data/data/my-package/files"));

可能没有myfile的完整路径

Process process2=Runtime.getRuntime().exec("myfile",
null, new File("/data/data/my-package/files"));

Context#getFilesDir() 而不是对路径进行硬编码也应该有效,并且比您自己指定路径更安全/更清晰,因为不能保证 /data/data/..始终是所有设备的正确路径。

Process process2=Runtime.getRuntime().exec("myfile",
null, getFilesDir()));

cd somewhere 的问题在于目录已针对不同进程更改,因此在新进程中第二次调用 exec 时看不到更改。

关于java - 使用 Runtime.getRuntime().exec 从定义的目录执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10689193/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com