如何在java类中调用Autoit.exe文件
我想调用java类中的Autoit.exe文件。我将AutoIt.exe文件保存在可执行包中,并在UploadProfile.java类中读取它如何在java类中调用Autoit.exe文件,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我想调用java类中的Autoit.exe文件。我将AutoIt.exe文件保存在可执行包中,并在UploadProfile.java类中读取它 // Label: image driver.findElement(By.id("uploadimage")).click(); //for 1mb file uploading Runtime.getRuntime().exec("D:\\netxploretesting\\unifytreewebautomation
// Label: image
driver.findElement(By.id("uploadimage")).click();
//for 1mb file uploading
Runtime.getRuntime().exec("D:\\netxploretesting\\unifytreewebautomation\\src\\test\\java\\executables\\fileupload.exe"+" "+"E:\\images\\profile.JPG");
//assertion for message display
assertTrue(driver.findElement(By.xpath("//strong[contains(text(),\"You are exceeding the size limit, it can't be more\")]")).getText().matches("You are exceeding the size limit, it can't be more than 512 KB."));
System.out.println(driver.findElement(By.xpath("//strong[contains(text(),\"You are exceeding the size limit, it can't be more\")]")).getText());
我想更改“Runtime.getRuntime().exec”(“D:\netxploretesting\unifytreewebautomation\src\test\java\executables\fileupload.exe”+“”+“E:\images\profile.JPG”);”这段代码,因为当我在其他系统上执行时,如果您想根据系统更改运行可执行文件,我会更改文件路径。 然后在项目级别保存可执行文件。 然后调用System.getPropert(“user.dir”),这将返回您的项目路径。 然后添加包含可执行文件的包路径。如下图所示。
Runtime.getRuntime().exec(System.getProperty(“user.dir”)+“your package\file.exe”)您可以将exe与包一起分发并保存在源代码中,但实际上这是一种不好的做法。但是,如果您没有选择,我可以建议以下方式:
exe
文件放入resources
文件夹资源
复制到文件系统中的某个位置jar
(exe
文件将作为资源
放置到jar
)import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws InterruptedException, IOException {
// Copy file from resources to file system (path taken from the command line argument)
InputStream is = Main.class.getResourceAsStream("Autoit.exe");
File exeFile = new File(args[0]);
FileOutputStream fos = new FileOutputStream(exeFile);
byte bytes[] = new byte[1000];
int k = 0;
while((k = is.read(bytes)) != -1){
fos.write(bytes, 0, k);
}
fos.close(); // Do not forget to close the outputstream, otherwise your code will be holding the file and it won't be possible to execute it
// Below is just an example on how you can execute the file after copying
List<String> commands = new ArrayList<String>(); //
commands.add(args[0]); // Start file with argument
commands.add("E:\\images\\profile.JPG"); //
Process p = new ProcessBuilder().command(commands).start();
InputStream i = p.getInputStream(); //
Scanner scanner = new Scanner(i); //
while (scanner.hasNextLine()){ // Print the output of the file
System.out.println(scanner.nextLine()); //
} //
}
}
java -jar mysuper.jar c:/Folder_To_Store_Copy_Of_Autoit/Autoit.exe