java process builder-无法运行简单程序

hyrbngr7  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(326)

我有一个叫做 darknet . 这是一个用暗黑制作的c程序。
我想管理这个公司 darknet 文件夹中的程序 Darknet 看起来是这样的:

我要跑了 darknet 使用java process builder,但在运行以下代码时没有得到响应:

// Arguments
    String darknetNamePath = darknet.getValue().getFilePath().replace("Darknet/", "./");
    String configurationFlag = configuration.getValue().getFilePath().replace("Darknet/", "");
    String weightsFlag = weights.getValue().getFilePath().replace("Darknet/", "");
    String imageFlag = "data/cameraSnap.png";
    String thresholdFlag = "-thresh " + thresholds.getValue();

    // Process builder
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.directory(new File("Darknet")); // We need to stand inside the folder "Darknet"
    String commandString = "detect " + configurationFlag + " " + weightsFlag + " " + imageFlag + " " + thresholdFlag;
    System.out.println("darknetNamePath  = " + darknetNamePath);
    System.out.println("commandString  = " + commandString);
    processBuilder.command(darknetNamePath, commandString);
    Process process = processBuilder.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    int exitCode = process.waitFor();
    System.out.println("\nExited with error code : " + exitCode);

这是我的输出。为什么不适合我?

darknetNamePath  = ./darknet
commandString  = detect cfg/yolov2-tiny.cfg weights/yolov2-tiny.weights data/cameraSnap.png -thresh 0.8

Exited with error code : 0

但当我打电话的时候 darknet 文件通过终端,然后它的工作。

./darknet detect cfg/yolov2-tiny.cfg weights/yolov2-tiny.weights data/cameraSnap.png -thresh 0.6

更新2:
这是我的最新消息。

// Arguments
    String darknetNamePath = darknet.getValue().getFile().getAbsolutePath();
    String configurationFlag = configuration.getValue().getFilePath().replace("Darknet/", "");
    String weightsFlag = weights.getValue().getFilePath().replace("Darknet/", "");
    String imageFlag = "data/cameraSnap.png";
    String thresholdFlag = "-thresh " + thresholds.getValue();

    // Process builder
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command(darknetNamePath, "detect", configurationFlag, weightsFlag, imageFlag, thresholdFlag);
    Process process = processBuilder.start();
    if (process.getInputStream().read() == -1) {
        System.out.println(darknetNamePath);
        System.out.println("detect");
        System.out.println(configurationFlag);
        System.out.println(weightsFlag);
        System.out.println(imageFlag);
        System.out.println(thresholdFlag);
        System.out.printf("ERROR!");
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    int exitCode = process.waitFor();
    System.out.println("\nExited with error code : " + exitCode);

输出:

/home/dell/Dokument/GitHub/Vaadin-DL4J-YOLO-Camera-Mail-Reporter/Vaadin-DL4J-YOLO-Camera-Mail-Reporter/Darknet/darknet
detect
cfg/yolov2-tiny.cfg
weights/yolov2-tiny.weights
data/cameraSnap.png
-thresh 0.3
ERROR!
Exited with error code : 0

更新3:
这样做有效:

// Arguments
            String darkPath = darknet.getValue().getFilePath().replace("Darknet/", "./"); // We need to call ./darknet, not absolute path
            String configurationFlag = configuration.getValue().getFilePath().replace("Darknet/", "");
            String weightsFlag = weights.getValue().getFilePath().replace("Darknet/", "");
            String imageFlag = "data/camera.png";
            String thresValue = String.valueOf(thresholds.getValue());

            // Process builder
            ProcessBuilder processBuilder = new ProcessBuilder();
            processBuilder.directory(new File("Darknet")); // Important
            processBuilder.command(darkPath, "detect", configurationFlag, weightsFlag, imageFlag, "-thresh", thresValue);
            processBuilder.redirectErrorStream(true); // Important
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            int exitCode = process.waitFor();
            System.out.println("\nExited with error code : " + exitCode);
8wigbo56

8wigbo561#

您的命令必须将所有参数分解为单独的部分—包括 thresholdFlag . 最好检查可执行文件是否存在。如果没有,您应该检查它的位置或修复路径变量以确保它可以被定位:

File darkpath = new File(darknetNamePath);
String [] cmd = new String[] { darkpath.getAbsolutePath(), "detect", configurationFlag, weightsFlag, imageFlag, "-thresh", String.valueOf(thresholds.getValue()) };

System.out.println("Path: "+darkpath+ " exists="+darkpath.exists());
System.out.println("exec "+Arrays.toString(cmd));

processBuilder.command(cmd);

处理stderr也是值得的,最简单的方法是在调用之前重定向stderr=>stdout processBuilder.start() ```
processBuilder.redirectErrorStream(true);

如果希望java启动可执行文件而不在绝对路径前加前缀,则它需要位于以下目录之一:

System.out.println("PATH COMPONENTS FOR JAVA LAUNCH:");
Arrays.asList(System.getenv("PATH").split(File.pathSeparator)).forEach(System.out::println);

sulc1iza

sulc1iza2#

您正在使用 ProcessBuilder 走错了路。command方法将可执行文件和参数作为单独的字符串,而不是路径,然后是另一个包含实际命令及其所有参数的字符串。没有shell参与对命令进行分词,因此您可以将所有不同的参数作为一个参数传递。
我没有darknet,所以这里有一个使用unix的简单命令 echo 命令:

import java.io.*;

public class ProcessBuilderTest {
    public static void main(String[] args) throws Exception {
        ProcessBuilder processBuilder = new ProcessBuilder();
        String[] command = {"/bin/echo", "hello", "world"};
        processBuilder.command(command);
        Process process = processBuilder.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        int exitCode = process.waitFor();
        System.out.println("Exited with error code : " + exitCode);
    }
}

当我运行这个时,我得到:

robert@saaz:~$ java ProcessBuilderTest.java
hello world
Exited with error code : 0

我不明白为什么你的命令没有出错。如果我给出了一个错误的命令(例如,“echo”后面的尾随空格),我会得到一个异常:

Exception in thread "main" java.io.IOException: Cannot run program "/bin/echo ": error=2, No such file or directory

这可能是特定于操作系统的。或者你还有其他的 darknet 可执行文件。

相关问题