【问题标题】:communicating with the process while executing a shell command with java在使用 java 执行 shell 命令时与进程通信
【发布时间】:2017-09-19 07:12:14
【问题描述】:

我要从 java 执行一个 shell 命令,我需要在执行命令时将参数传递给输出流..

下面是shell命令

./darknet detect cfg/yolo-voc.2.0.cfg backup/yolo-voc_20000.weights

执行此命令时,它会在终端中生成图像文件的路径,我可以提供图像的路径,如下所示

Loading weights from backup/yolo-voc_21000.weights...Done!
Enter Image Path:

当从终端执行时,我可以在那里提供路径。

我设法使用 java 进程执行了这个命令,并且当我为命令提供图像 uri 时,我可以获得输出。这是代码

     public static void execCommand(String command) {
    try {

        Process proc = Runtime.getRuntime().exec(command);
        // Read the output
        BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

        String line = "";
         //reader.readLine();
        while ((line = reader.readLine()) != null) {
            System.out.print(line + "\n");
            s.add(line);
        }
//            proc.waitFor();
    } catch (IOException e) {
        System.out.println("exception thrown: " + e.getMessage());
    } 
}

但我想要的是在运行时提供图像路径而不是命令执行的开始.. 尝试如下写入输出流仍然没有运气

public static void execCommand(String command) {
    try {
        Process proc = Runtime.getRuntime().exec(command);
        // Read the output
        BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

        String line = "";
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
             writer.append("data/test2.jpg");
          writer.newLine();
         //reader.readLine();
        while ((line = reader.readLine()) != null) {
            System.out.print(line + "\n");
            s.add(line);
        }
//            proc.waitFor();
    } catch (IOException e) {
        System.out.println("exception thrown: " + e.getMessage());
    } 
}

【问题讨论】:

  • 只是一个想法——你正在调用的程序是否真的从标准输入读取它的输入?如果您使用命令行中的 shell 重定向将文件名导入其中,它是否可以按您的意愿工作?如果没有,那么您在 Java 中所做的很可能也不会起作用。命令行实用程序使用低级方法读取键盘数据并不少见。
  • 是的,当我通过 jvm 调用 ./darknet detect cfg/yolo-voc.2.0.cfg backup/yolo-voc_20000.weights data/test2.jpg 时,它会成功返回输出。但我想稍后给出路径。因为暗网首先加载权重文件。然后我可以将图像交给暗网处理。如果我也给出文件名,它将仅针对该特定 jpg 执行并终止。但我想在加载权重文件后处理更多图像的过程
  • 对不起,我表达的不是很好。我的意思是,如果你这样做 echo /path/to/something | ./darknet detect... 会发生什么?如果它仍然提示输入文件名,那么它没有从标准输入获得输入,你将无法通过模拟标准输入使其在 Java 中工作。
  • echo data/test2.jpg | ./darknet detect cfg/yolo-voc.2.0.cfg backup/yolo-voc_20000.weights 完美运行.. 它不需要其他输入它就像这样./darknet detect cfg/yolo-voc.2.0.cfg backup/yolo-voc_20000.weights data/test2.jpg

标签: java linux shell ubuntu darknet


【解决方案1】:

您需要调用writer.flush() 才能将某些内容实际输出到下划线InputStream

因此您的代码应如下所示:

public static void execCommand(String command) {
    try {
        Process proc = Runtime.getRuntime().exec(command);

        // Read the output
        BufferedReader reader = new BufferedReader(new 
            InputStreamReader(proc.getInputStream()));

        String line = "";
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
        writer.append("data/test2.jpg");
        writer.newLine();
        // **** add flush here ****
        writer.flush();
        // and remember to close your resource too
        writer.close();
        //reader.readLine();
        while ((line = reader.readLine()) != null) {
            System.out.print(line + "\n");
            s.add(line);
        }
        // ***** close your reader also ****
        reader.close();
        //            proc.waitFor();
    } catch (IOException e) {
        System.out.println("exception thrown: " + e.getMessage());
    } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2012-10-05
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多