简体   繁体   English

尝试从Java中的Runtime对象执行exe时出错

[英]error when trying to execute exe from Runtime object in java

I am running windows 7 and have an exe file I am trying to run using the following command in Java: 我正在运行Windows 7,并且正在尝试使用Java中的以下命令运行一个exe文件:

File dir = new File("C:\\PATH\\TO\\DIR");
String[] cmdArray = {"file.exe"};

if(dir.exists()){
    for(String s : dir.list()){
        if(s.equals(cmdArray[0]))
            System.out.println("File exists!");
    }
}

Runtime.getRuntime().exec(cmdArray,null,dir);

The exec(...) command, however, gives me this error: exec(...)命令给我这个错误:

Cannot run program "file.exe" (in directory "C:\PATH\TO\DIR"): CreateProcess error=2, The system cannot find the file specified

The above code does print out "File Exists!" 上面的代码确实打印出“文件已存在!” so the file is there and Java knows it is there. 因此文件就在那里,Java知道它在那里。 I dont know how this is possible. 我不知道这怎么可能。

Also, I need to use Runtime.getRuntime().exex(...). 另外,我需要使用Runtime.getRuntime()。exex(...)。 This call is actually inside another method that I am not suppose to change so alternative solutions will not work for me. 该调用实际上在另一个我不希望更改的方法内,因此替代解决方案对我不起作用。 Thanks in advance! 提前致谢!

EDIT: I ran the file in a cmd window without admin privileges and got the following message: 编辑:我在没有管理员权限的cmd窗口中运行文件,并收到以下消息:

Error in Opening Configuration File in Read Mode

So I assume I have permission issues. 所以我想我有权限问题。 How would I change the permissions in order for me to be able to run it? 我将如何更改权限才能运行它?

EDIT2: 编辑2:

I tried changing cmdArray to this: 我尝试将cmdArray更改为此:

String[] cmdArray = {"C:\\PATH\\TO\\DIR\\file.exe"};

and it worked like a charm. 它就像一种魅力。 One question though. 不过有一个问题。 Why did this work but not the other way? 为什么这样做有效,但反之则不行呢? I assumed that Runtime.exec() ran at a high level like so in a cmd wibndow: 我假设Runtime.exec()像cmd wibndow一样以较高的级别运行:

pushd dir
cmdArray[0] cmdArray[1] cmdArray[2] ... cmdArray[length - 1]
popd dir

Is this the wrong assumption? 这是错误的假设吗?

您在C驱动器中存在权限问题。通过在C驱动器中手动执行"file.exe"进行检查。更好的解决方案是将"file.exe"保留在C驱动器以外的其他位置并从那里执行。

Perhaps, You will need to include your directory to your path . 也许,您需要将目录包括到path中 You may take a look at this question and this tutorial 您可以看看这个问题本教程

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM