【问题标题】:How to use java for running an executable file portace to generate svg file from bitmap file如何使用java运行可执行文件portace从位图文件生成svg文件
【发布时间】:2019-09-24 16:02:29
【问题描述】:

我已经查看了如何通过运行时进程构建器在 java 中运行可执行文件,但它不起作用。 我的代码如下...

        String command = "potrace --svg mb-finer-19.pbm -o mb-finer-19.svg";
        try {
            File f = new File("C:\\webstudio\\potrace113win32");
            Process process = Runtime.getRuntime().exec(command, null, f);
            System.out.println("the output stream is " + process.getOutputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String s;
            while ((s = reader.readLine()) != null) {
                System.out.println("The inout stream is " + s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

但我回来了

java.io.IOException: Cannot run program "potrace" (in directory "C:\webstudio\potrace113win32"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:620)
    at java.lang.Runtime.exec(Runtime.java:450)
    at shellcommands.RunPotrace.main(RunPotrace.java:22)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
    at java.lang.ProcessImpl.start(ProcessImpl.java:137)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)```

根据 javadocs,我在哪里出错了? 可执行的 portace.exe 与映像 mb-finer-19.pbm 一起位于目录中 非常感谢您的帮助。

【问题讨论】:

    标签: java potrace


    【解决方案1】:

    我运行了以下,它工作...

    String command = "C:\\webstudio\\potrace113win32\\potrace.exe --svg mb-finer-19.pbm -o mb-finer-19.svg";
    

    显然,如果它不在系统路径中,则必须指定整个路径。很抱歉在提问之前没有先尝试这个。

    【讨论】:

    • 是的,实际上您在exec() 命令中指定的目录是工作 目录,而不是可执行文件所在的目录。
    • 我怀疑实际的问题是您试图运行 potrace 而不是 potrace.exe
    【解决方案2】:

    这是运行此类进程的正确方法:

        Path imagesDir = Paths.get(
            System.getProperty("user.home"), "Documents");
    
        Path inputFile = imagesDir.resolve("mb-finer-19.pbm");
        Path outputFile = imagesDir.resolve("mb-finer-19.svg");
    
        ProcessBuilder builder = new ProcessBuilder(
            "C:\\webstudio\\potrace113win32\\potrace.exe",
            "--svg",
            inputFile.toString(),
            "-o",
            outputFile.toString());
    
        builder.inheritIO();
    
        Process process = builder.start();
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            throw new IOException("Got error code " + exitCode
                + " from command " + builder.command());
        }
    

    inheritIO() 将导致子进程的所有输出出现在调用它的 Java 程序的输出中,无需自己消耗进程的 InputStream 和 ErrorStream。

    使用单个命令行参数而不是单个命令字符串的一个重要好处是可以正确处理包含空格的文件名。这使您的代码更具可移植性,因为它适用于任何有效文件和任何有效目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      相关资源
      最近更新 更多