Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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
使用Runtime.getRuntime()执行命令时,Java应用程序未读取文件_Java_Runtime_Filereader - Fatal编程技术网

使用Runtime.getRuntime()执行命令时,Java应用程序未读取文件

使用Runtime.getRuntime()执行命令时,Java应用程序未读取文件,java,runtime,filereader,Java,Runtime,Filereader,我有一个Java应用程序,它使用Runtime.getRuntime.exec(“命令”)执行命令然后它生成一个文件,我需要读取该文件才能找到一个字符串,到目前为止一切正常 问题是程序在执行命令后找不到给定的字符串,但是如果我注释掉行Runtime.getRuntime.exec(“命令”)在我第一次运行应用程序并进一步运行之后,文件被创建,它会正确地找到字符串。似乎由于某种原因,getRuntime()正在停止文件阅读器的工作。有人知道这个问题的解决办法吗 public static void

我有一个Java应用程序,它使用
Runtime.getRuntime.exec(“命令”)执行命令然后它生成一个文件,我需要读取该文件才能找到一个字符串,到目前为止一切正常

问题是程序在执行命令后找不到给定的字符串,但是如果我注释掉行
Runtime.getRuntime.exec(“命令”)在我第一次运行应用程序并进一步运行之后,文件被创建,它会正确地找到字符串。似乎由于某种原因,
getRuntime()
正在停止文件阅读器的工作。有人知道这个问题的解决办法吗

public static void main(String[] args) throws IOException{

    String[] command = new String[3];
    command[0] = "cmd.exe";
    command[1] = "/c";
    command[2] = "C:\\Users\\kjdah\\Desktop\\handle.exe -a > C:\\Users\\kjdah\\Desktop\\handles.txt";

    Runtime.getRuntime().exec(command);

    String toFind = "##?#USB#VID_04F2&PID_B2E1&MI_00#6&9f9977c&0&0000#";
    File file = new File("C:\\Users\\kjdah\\Desktop\\handles.txt");

    boolean found = false;
    String strLinePid = null;

    try {
        FileReader fstream = new FileReader(file);
        BufferedReader buffer = new BufferedReader(fstream);
        String strLine;

        while ((strLine = buffer.readLine()) != null)   {

            if(strLine.contains("pid:")){
                strLinePid = strLine;
            }

            if(strLine.contains(toFind)){
                found = true;
                break;
            }
        }

        buffer.close();
        fstream.close();

    }
    catch (Exception e){
        System.err.println("An error happened: " + e.getMessage());
        e.printStackTrace();
    }
`创建一个并发运行的新进程。您必须等待它完成对其结果的处理


这可以使用
运行时#exec
返回的
进程
方法来完成。此外,检查流程是否成功也是一个好主意。有关更多信息,请查看。

太快了!很好的一个mm759我刚刚保存了进程并添加了.waitfor();成功了。你让我开心,干杯