简体   繁体   English

运行时exec无法在appdata中运行程序?

[英]Runtime exec cannot run programs in appdata?

I have been messing around with running some .exe files and it appears as if there is something blocking it from running it in appdata? 我一直在运行一些.exe文件,似乎好像有什么阻止它在appdata中运行?

Runtime.getRuntime().exec(System.getenv("APPDATA") + "test.exe");

This is the error I get 这是我得到的错误

java.io.IOException: Cannot run program "C:\Users\Cole": CreateProcess error=2, The system cannot find the file specified

You should not use the plain exec(String) method as it requires OS specific escaping. 您不应使用普通的exec(String)方法,因为它需要特定于操作系统的转义。 If you use the string array version it should find the executable. 如果使用字符串数组版本 ,则应找到可执行文件。

It is also a good idea to check if the variable exists and if it ends with a \\ before concatenating it with the filename. 在将变量与文件名连接之前,检查变量是否存在以及是否以\\结尾也是一个好主意。 Or better use the hierachical File constructor: 或更好地使用分层File构造函数:

String appdata = System.getenv("APPDATA");
if (appdata == null || appdata.trim().isEmpty())
  appdata=".";
String fileName = new File(appdata, "test.exe").getAbsolutePath();
Runtime.getRuntime().exec(new String[]{fileName /*, noargs */});

An easy way to do this is to construct the path using a File object. 一种简单的方法是使用File对象构造路径。

final String f = new File(System.getenv("APPDATA"), "test.exe").toString();
final Process p = Runtime.getRuntime().exec(new String[] { f });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在命令中使用带有空格的 Runtime.exec 时“无法运行程序” - “Cannot run program” when using Runtime.exec with spaces in command java Runtime.exec运行shell脚本 - 无法打开文件 - java Runtime.exec to run shell script - cannot open file Runtime.getRuntime()。exec - >无法运行程序CreateProcess error = 2,系统找不到指定的文件 - Runtime.getRuntime().exec -> Cannot run program CreateProcess error=2, The system cannot find the file specified Runtime.getRuntime.exec 139 (SIGSEGV) 来自 Java 用于 C 程序 - Runtime.getRuntime.exec 139 (SIGSEGV) from Java for C programs 如何与Runtime.getRuntime()。exec(command)程序进行交互? - How can I interact with Runtime.getRuntime().exec(command) programs? 无法从runtime.getruntime.exec()运行/获取输出(android java) - cannot run/get output from runtime.getruntime.exec() (android java) 在程序文件名中使用带有空格的 Runtime.exec 时“无法运行程序” - “Cannot run program” when using Runtime.exec with spaces in program filename Runtime.getRuntime().exec() 导致 java.io.IOException: Cannot run program... no such file or directory - Runtime.getRuntime().exec() results in java.io.IOException: Cannot run program ... no such file or directory Runtime.getRuntime().exec(command) - 无法运行程序,错误=2,没有那个文件或目录 - Runtime.getRuntime().exec(command) - Cannot run program, error=2, No such file or directory 使用 java runtime exec 运行连续命令 Linux - Run consecutive Commands Linux with java runtime exec
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM