无法在Selenium WebDriver中通过Java代码运行exe文件

时间:2017-09-15 06:05:38

标签: java selenium webdriver runtime exe

我试图通过Java代码执行exe文件。我在下面写了一个简单的代码,但是出现了错误。尝试了多种解决方案但都徒劳无功。

我的代码:

package com.runExeFile;

import java.io.File;

public class ClassA {

    public static void main(String[] args) throws Exception {
         Runtime.getRuntime().exec("C:\\FlashBuild\\14_09_2017_play_27_0_r0_137\\FF_32Release\\Something.exe");
    }

}

我得到的错误:

Exception in thread "main" java.io.IOException: Cannot run program "C:\FlashBuild\14_09_2017_play_27_0_r0_137\FF_32Release\install_flash_player_27_plugin.exe": CreateProcess error=740, The requested operation requires elevation
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at com.runExeFile.ClassA.main(ClassA.java:9)
Caused by: java.io.IOException: CreateProcess error=740, The requested operation requires elevation
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)`enter code here`
    at java.lang.ProcessImpl.start(Unknown Source)

2 个答案:

答案 0 :(得分:1)

这是因为您需要以管理员身份运行该程序。以管理员身份运行程序是代码。错误740仅仅是因为这一点。 请参阅这些链接

CreateProcess error=740, The requested operation requires elevation

Java: run as administrator

import java.io.IOException;

public class RunAsAdminExample {
    public static void main(String[] args) throws IOException {
        Process myappProcess = Runtime.getRuntime().exec("powershell.exe Start-Process notepad.exe -verb RunAs");
    }
}

答案 1 :(得分:0)

我最近做过。我的方式是

try {
    File fileDirectory = new File("C:/someDirectory");
    Runtime.getRuntime().exec(new String[]{"cmd","/C","start someRunnable.exe"}, null, fileDirectory);
} catch (IOException e) {
    e.printStackTrace();
}

您需要指定要运行的目录,并启动命令提示符以运行可执行文件。