【问题标题】:Execute command using Java on Windows在 Windows 上使用 Java 执行命令
【发布时间】:2013-10-15 07:04:44
【问题描述】:

我想执行一个命令mspview -r "C:\\Users\\SS\\Desktop\\phantomjs-1.9.2-windows\\image.tif"。我怎样才能通过Java代码做到这一点?我正在尝试使用批处理文件执行此操作。当我在 RUN 的帮助下运行时,同样的命令。我得到正确的输出。我在批处理文件的帮助下执行了一个.exe 程序,其中包含以下代码C:\Users\SS\Desktop\phantomjs-1.9.2-windows\phantomjs.exe

【问题讨论】:

标签: java batch-file command


【解决方案1】:

你基本上是在问如何在 java 中运行 shell 命令,对吧?

Runtime.getRuntime().exec("whatever system call you want");

【讨论】:

    【解决方案2】:

    你需要使用 ProcessBuilder

    Process process = new ProcessBuilder(
    "C:\\PathToExe\\exe.exe","param1","param2").start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;
    
    System.out.printf("Output of running %s is:", Arrays.toString(args));
    
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
    

    已经在 stackoverflow 上找到的代码 Execute external program in java

    【讨论】:

    • 我知道这段代码,我可以执行 .exe。但问题是我想启动 Microsoft Office Document Imaging,即启动图像的 OCR。我可以通过执行 mspview -r "C:\\Users\\SS\\Desktop\\phantomjs-1.9.2-windows\\image.tif" 来做到这一点。但是我想通过上面的命令运行并执行它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多