2017-09-15 96 views
0

我想通過Java代碼執行一個exe文件。我在Eclipse中寫了一個簡單的代碼,但遇到了錯誤。嘗試了多種解決方案,但都是徒勞的。無法通過Java代碼運行的exe文件,在Selenium WebDriver

我的代碼:

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) 
+0

這是錯誤狀態:您必須以提升的權限運行程序。 – Jerrybibo

+1

[CreateProcess error = 740,請求的操作需要提升]的可能重複(https://stackoverflow.com/questions/5853529/createprocess-error-740-the-requested-operation-requires-elevation) –

回答

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

終於成功了下面的代碼行,但我仍然沒有得到它的工作原理。 Runtime.getRuntime()。exec(「rundll32 url.dll,FileProtocolHandler」+ PathOfExe); –

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

你需要指定要運行的目錄和啓動命令提示符來運行可執行文件。

相關問題