2016-04-02 9 views
0

私はJavaコードからマイクロソフトのパワーポイントを開きたいが、その前に既にオープンされているかどうかをチェックしたい。powerpointがすでにJavaで開いているかどうかを確認しますか?

は、今まで私はこの

Process p=Runtime.getRuntime().exec("cmd.exe /C start powerpnt"); 

このコマンドは、オープンの新しいパワー・ポイント・ウィンドウごとに書くが、私はそれが存在する場合、既に1を開いて表示したいです。ここ

おかげ

答えて

1

は、プロセスをチェックするための完全なコードは、(すでに開いているかいない 機能CheckApplicationIsOpen.isOpenApplicationです)状態を返しているPowerPointのexeファイルをチェックし、メインに 「開く」または「閉じる」スタートですPowerPointが開いている場合

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package checkapplicationisopen; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author ANGEL 
*/ 
public class CheckApplicationIsOpen { 

    /** 
    * @param args the command line arguments 
    */ 


    /*** 
    * 
    * @param applicationName : whatever application you want to check if it is already open or not 
    * @return the result status if it is open then 'open', otherwise 'close' 
    */ 
    public static String isOpenApplication(String applicationName) 
    { 
     String result=""; 
     try { 
      String line; 
      String pidInfo =""; 

      Process p =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe"); 

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

      while ((line = input.readLine()) != null) { 
       pidInfo+=line+"\n"; 
      } 
      System.out.println("Data : "+pidInfo); 
      input.close(); 

      if(pidInfo.contains(applicationName)) 
      { 
       System.out.println(applicationName+"is open"); 
       result="open"; 
      } 
      else 
      { 
       System.out.println("PowerPoint is Not Open"); 
       result="close"; 
       // 

      } 

     } catch (IOException ex) { 
      Logger.getLogger(CheckApplicationIsOpen.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return result; 
    } 

    public static void main(String[] args) { 

     try 
     { 
      if(!CheckApplicationIsOpen.isOpenApplication("POWERPNT.EXE").equals("open")) 
      { 
       Process p1=Runtime.getRuntime().exec("cmd.exe /C start powerpnt"); 
      }else 
      { 
       System.out.println("Application is Open"); 
      } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

おかげで多くのことをタスクマネージャのインスタンスを使用することのないsandip.Butまた、私は何もあればしたいんhappens.I PowerPointが、その後実行されている場合、それはあなたのコード内top.hereに表示されなければならないことを望みます可視性よりも開いているパワーポイントを真に設定する必要があります。 –

+0

PowerPointは単独のインスタンスのみを許可するので、現在開いているインスタンスがある場合はそのインスタンスを取得するか、新しいインスタンスを開始します。注意すべき唯一の奇妙な点は、PowerPnt.EXEのインスタンスがクラッシュして完全には閉じられていないか、ユーザーがWindowsエクスプローラでプレビューペインが表示されていて、PPTが開いているためです。プレビューウィンドウ –

関連する問題