Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java类中调用Autoit.exe文件_Java_Selenium_Selenium Webdriver - Fatal编程技术网

如何在java类中调用Autoit.exe文件

如何在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

我想调用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\\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
  • 下面是一个简单的示例,说明如何实现第2点和第3点:

    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