【问题标题】:Using cmd commands in java to print a file在java中使用cmd命令打印文件
【发布时间】:2014-07-17 14:54:49
【问题描述】:

我试图从我的 java swing 应用程序中打印一个记事本文件。 但是我似乎无法让它工作。

当我在命令提示符中输入此命令时,它会打印给定的记事本文件。 start /min notepad /p C:\score-programma\boem.txt

但是,当我尝试在 java 中执行此操作时,它说找不到文件。

java.io.IOException: Cannot run program "start": CreateProcess error=2, Het systeem kan het opgegeven bestand niet vinden at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at nl.daalhuisen.model.Model.print(Model.java:184) at Apl$1$1.actionPerformed(Apl.java:86) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.io.IOException: CreateProcess error=2, Het systeem kan het opgegeven bestand niet vinden at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 41 more

这是我运行的 java 代码:

String[] args = {"start", "/min", "notepad", "/p", "C:\score-programma\boem.txt"}; Runtime.getRuntime().exec(args);

我也试过
String[] args = {"cmd.exe", "start", "/min", "notepad", "/p", "C:\score-programma\boem.txt"};

这不会产生同样的错误,但没有任何反应。 我在这里错过了什么?

【问题讨论】:

    标签: java cmd


    【解决方案1】:

    startcmd 的内置命令。它不是可执行文件。你必须运行:

    cmd /c start notepad /p c:\etc...
    

    /c 标志对于告诉cmd 您正在尝试运行其他程序是必要的。如果您不传递它,那么startnotepad 等将被视为 cmd 本身的参数。

    【讨论】:

      【解决方案2】:

      您可能想要使用类似下面的代码......并进行一些 Java 研究以添加“最小化”设置

      package com.example.command;
      
      import java.io.*;
      
      public class JavaRunCommand {
      
          public static void main(String args[]) {
              String command = "notepad /p ";
              String filepath = "C:\\Temp\\readme.txt";
              String s = null;
      
              try {
                  // using the Runtime exec method:
                  Process p = Runtime.getRuntime().exec(command + filepath);
                  BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                          p.getInputStream()));
                  BufferedReader stdError = new BufferedReader(new InputStreamReader(
                          p.getErrorStream()));
      
                  // read the output from the command
                  System.out.println("Here is the standard output of the command:\n");
                  while ((s = stdInput.readLine()) != null) {
                      System.out.println(s);
                  }
      
                  // read any errors from the attempted command
                  System.out
                          .println("Here is the standard error of the command (if any):\n");
                  while ((s = stdError.readLine()) != null) {
                      System.out.println(s);
                  }
      
                  System.exit(0);
              } catch (IOException e) {
                  System.out.println("exception happened - here's what I know: ");
                  e.printStackTrace();
                  System.exit(-1);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-08
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-05
        • 1970-01-01
        相关资源
        最近更新 更多