簡體   English   中英

Selenium WebDriver中無法通過Java代碼運行exe文件

[英]Could not run exe file through java code, in Selenium WebDriver

我正在嘗試通過Java代碼執行exe文件。 我在Eclipse下面編寫了一個簡單的代碼,但出現了錯誤。 嘗試了多種解決方案,但徒勞無功。

我的代碼:

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)

這是因為您需要以管理員身份運行該程序。 要以管理員身份運行該程序,這里是代碼。 錯誤740僅是因為如此。 看到這些鏈接

CreateProcess錯誤= 740,請求的操作需要提升

Java:以管理員身份運行

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");
    }
}

我最近做了。 我的方式是

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

您需要指定要運行的目錄以及啟動命令提示符以運行可執行文件的目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM