2014-07-17 2 views
0

내 Java 스윙 응용 프로그램에서 메모장 파일을 인쇄하려고합니다. 그러나 나는 그것을 작동시킬 수없는 것 같습니다.java에서 cmd 명령을 사용하여 파일 인쇄

이 명령을 명령 프롬프트에 입력하면 주어진 메모장 파일이 인쇄됩니다. start /min notepad /p C:\score-programma\boem.txt

그러나 자바에서이 작업을 시도하면 파일을 찾을 수 없다고 말합니다.

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

내가 실행하는 자바 코드 :

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"};

이 같은 오류를 생성하지 않습니다 시도했지만 아무 반응이 없습니다. 여기에 무엇이 누락되어 있습니까?

답변

2

startcmd에 대한 내장 명령입니다. 실행 파일이 아닙니다. 당신은 실행해야 할 것 :

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

/c 플래그는 다른 프로그램을 실행하려고하는 그 cmd 얘기하는 것이 필요하다. 전달하지 않으면 start, notepad 등이 cmd 자체에 대한 인수로 처리됩니다.

0

당신은 .. 다음과 같은 코드를 사용하여 추가 "최소화"설정

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); 
     } 
    } 
} 
에 대한 몇 가지 자바 연구를 수행 할 수 있습니다
관련 문제