如何使java getRuntime().exec()运行带有参数的命令行程序? [英] How to get java getRuntime().exec() to run a command-line program with arguments?

查看:227
本文介绍了如何使java getRuntime().exec()运行带有参数的命令行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个使用Runtime.getRuntime().exec()方法的Java程序,该方法使用命令行来运行程序"tesseract"的实例.

I've been trying to write a java program that uses the Runtime.getRuntime().exec() method to use the command-line to run an instance of the program "tesseract".

在某些背景下,Tesseract是一个免费的开源程序,用于对图片执行OCR(光学字符识别).它接收一个图片文件并输出一个文本文件.这是一个使用此命令来运行

Some background, Tesseract is a free open source program that is used to perform OCR (Optical Character Recognition) on pictures. It takes in a picture file and outputs a text document. It is a command-line program that uses this command to run

(在命令提示符外壳中)

(from within the command prompt shell)

tesseract imageFilePath outFilePath [optional arguments] 

示例:

tesseract "C:\Program Files (x86)\Tesseract-OCR\doc\eurotext.tif" "C:\Users\Dreadnought\Documents\TestingFolder\out"

第一个参数调用tesseract程序,第二个参数是图像文件的绝对路径,最后一个参数是输出文件的路径和名称. Tesseract只需要输出文件的名称,不需要扩展名.

the first argument calls the tesseract program, the second is the absolute path to the image file and the last argument is the path and name of what the output file should be. Tesseract only requires the name of the output file it does not require the extension.

在命令提示符下工作非常完美.但是,我想从Java程序运行它,但遇到了一些错误.

Working from the command prompt this works perfect. However, I was wanting to run this from a java program and was running into some errors.

我发现这段代码作为起点非常有帮助

I found this this code to be very helpful as a starting off point

public class Main
{
   public static void main(String args[])
   {
      try
      {
         Runtime rt = Runtime.getRuntime();
         String cmdString = "cmd /c dir";

         System.out.println(cmdString);
         Process pr = rt.exec(cmdString);

         BufferedReader input = new BufferedReader(new InputStreamReader(
                                                   pr.getInputStream()));

         String line = null;

         while ((line = input.readLine()) != null)
         {
            System.out.println(line);
         }

         int exitVal = pr.waitFor();
         System.out.println("Exited with error code " + exitVal);

      }
      catch (Exception e)
      {
         System.out.println(e.toString());
         e.printStackTrace();
      }
   }
}

它输出dir命令的结果.但是当我这样修改它时

It prints out the result of the dir command. However when I modified it like so

public class Main
{
   public static void main(String args[])
   {
      try
      {
         Runtime rt = Runtime.getRuntime();
         String imageFilePath = "\"C:\\Program Files (x86)\\Tesseract-OCR\\doc\\eurotext.tif\"";
         String outputFilePath = "\"C:\\Users\\Dreadnought\\Documents\\TestingFolder\\eurotext-example\"";
         String[] commands = {"cmd", "/c", "tesseract", imageFilePath, outputFilePath };

         Process pr = rt.exec(commands);

         BufferedReader input = new BufferedReader(new InputStreamReader(
               pr.getInputStream()));

         String line = null;

         while ((line = input.readLine()) != null)
         {
            System.out.println(line);
         }

         int exitVal = pr.waitFor();
         System.out.println("Exited with error code " + exitVal);
      }
      catch (Exception e)
      {
         System.out.println(e.toString());
         e.printStackTrace();
      }
   }
}

它唯一输出的是Exited with error code 1.如果流程以错误结束,这是预期的输出.

The only thing it outputs is Exited with error code 1. This is the expected output if the Process ended with an error.

我什至尝试通过"cmd /c tesseract \"C:\\Program Files (x86)\\Tesseract-OCR\\doc\\eurotext.tif\" \"C:\\Users\\Dreadnought\\Documents\\TestingFolder\\eurotext-example\"",但最终还是遇到了相同的错误.

I have even tried passing "cmd /c tesseract \"C:\\Program Files (x86)\\Tesseract-OCR\\doc\\eurotext.tif\" \"C:\\Users\\Dreadnought\\Documents\\TestingFolder\\eurotext-example\"" and I ended up having the same error.

根据在getRuntime().exec中使用引号,我认为问题在于我当时曾尝试过对引号进行转义,所以这就是我传入String数组的原因.但我仍然得到Exited with error code 1.

According to Using Quotes within getRuntime().exec I thought problem was that I was that i had tried to escape the quotes, so that is why I passed in a String array. But I am still getting the Exited with error code 1.

是否可以使用java Runtime.getRuntime().exec()命令执行命令行程序?

Is it possible to execute a command-line program with the java Runtime.getRuntime().exec() command?

编辑:问题仍然存在

我尝试不像下面的Evgeniy Dorofeev和Nandkumar Tekale所建议的那样使用"cmd/c"思维.但是,我得到了另一种错误:

I have tried not using "cmd /c" thinking along the same line of reasoning as Evgeniy Dorofeev and Nandkumar Tekale suggested below. However, I get a different sort of error:

java.io.IOException: Cannot run program "tesseract": CreateProcess error=2, The system cannot find the file specified
java.io.IOException: Cannot run program "tesseract": CreateProcess error=2, The system  cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at Main.main(Main.java:15)
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>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
... 4 more

也许这可以提供更多信息?我真的对导致此问题的原因感到好奇.不管我是否在参数中添加转义的引号,问题都是相同的.

Maybe this gives more information? I am really curious about what is causing this problem. Also the problem is the same whether or not I add the escaped quotations to my arguments.

编辑2 :一时兴起,我提供了tesseract可执行文件的绝对路径,而没有使用cmd /c就像是吸引人.我想问题是Runtime.getRuntime().exec()不能调用环境变量吗?

EDIT 2: On a whim I provided an absolute path to the tesseract executable and not using the cmd /c worked like a charm. I guess the question is can Runtime.getRuntime().exec() not call environment variables?

推荐答案

tesseract是外部命令,因此您无需将其与cmd一起使用.将tesseract添加到环境变量.使用直接命令:

well tesseract is external command so you do not need to use it with cmd. Add tesseract to environment variables. Use direct command as :

String[] commands = {"tesseract", imageFilePath, outputFilePath };

存在状态1表示功能不正确.请参见进程退出状态

Exist status 1 means Incorrect function. See process exit status

这篇关于如何使java getRuntime().exec()运行带有参数的命令行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆