2012-03-08 8 views
1

다른 응용 프로그램을 실행할 수있는 Java 응용 프로그램을 작성하고 있습니다. 그렇게하기 위해, 나는 Process 클래스 객체를 사용했다. 그러나 할 때, 앱은 프로세스가 종료되기 전에 프로세스가 종료 될 때까지 기다린다. Java에서 외부 응용 프로그램을 실행할 수있는 방법이 있지만 끝내기를 기다리지 마십시오.java에서 외부 응용 프로그램을 실행하지만 완료 될 때까지 기다리지 마십시오

public static void main(String[] args) 
{ 
FastAppManager appManager = new FastAppManager(); 
appManager.startFastApp("notepad"); 
} 

public void startFastApp(String name) throws IOException 
{ 
    Process process = new ProcessBuilder(name).start(); 
} 

답변

0

다른 스레드에서 실행할 수 있습니다.

public static void main(String[] args) { 
     FastAppManager appManager = new FastAppManager(); 
     appManager.startFastApp("notepad"); 
    } 

    public void startFastApp(final String name) throws IOException { 
     ExecutorService executorService = Executors.newSingleThreadExecutor(); 
     executorService.submit(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        Process process = new ProcessBuilder(name).start(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } 
     }); 

    } 

당신은 당신의 필요에 따라 데몬 스레드를 시작 할 수 있습니다 :

ExecutorService executorService = Executors.newSingleThreadExecutor(new ThreadFactory() { 
     @Override 
     public Thread newThread(Runnable runnable) { 
      Thread thread = new Thread(); 
      thread.setDaemon(true); 
      return thread; 
     } 
    }); 
+0

나는 그가 프로그램을 시작한 후에 그의 lancher가 종료되기를 원했다고 생각합니다. –

2

ProcessBuilder.start()를 프로세스가 완료 될 때까지 기다리지 않습니다. 해당 동작을 얻으려면 Process.waitFor()를 호출해야합니다.

내가 넷빈즈에서 실행하면

public static void main(String[] args) throws IOException, InterruptedException { 
    new ProcessBuilder("notepad").start(); 
} 

가 여전히 실행 한 것으로 나타났습니다이 프로그램 작은 테스트를했다. java -jar로 명령 행에서 실행할 때 즉시 리턴합니다.

그래서 프로그램이 종료 될 때까지 기다리지 않고 IDE가 그렇게 보입니다.

+0

ProcessBuilder가 루프 내에서 실행되는시기는 어떻습니까? 완료가 다음 루프로 진행되기를 기다리는가? – sijo0703

+0

waitFor()를 사용하면됩니다. –

관련 문제