2012-07-19 24 views
1


我有一個Java程序,就像裹在Apache共同守護進程(prunsrv),但一個窗口服務我有2個問題:
1.服務公司使用交互式桌面配置可以查看擺動對話框並嘗試應用程序中的圖標。但是,idalogs和try圖標不會出現。
2.交互式桌面只適用於本地系統帳戶應用程序無法讀取當前登錄的用戶,此用戶名對於應用程序是必需的Java作爲Windows服務與交互式桌面支持和閱讀CURREN登錄用戶

然後我需要解決這2個問題,謝謝,我粘貼代碼從主類

package widget; 

import java.awt.AWTException; 
import java.awt.Image; 
import java.awt.Label; 
import java.awt.MenuItem; 
import java.awt.PopupMenu; 
import java.awt.SystemTray; 
import java.awt.Toolkit; 
import java.awt.TrayIcon; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowStateListener; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Properties; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 

import widget.controller.NotifyManager; 
import widget.utils.Logger; 


public class AWidget extends JFrame { 
    private static Properties props = null; 
    private static String parametersFile = "widget.properties"; 
    public static String mediaResourcesPath; 
    public static String appIcon; 
    public static int updateFrecuency = 5; 

    String username = ""; 
    TrayIcon trayIcon; 
    SystemTray tray; 

    static String activityImages[]; 

    AWidget() { 
     super("Italo Widget"); 

     mediaResourcesPath = getParameter("mediaResourcesPath"); 
     appIcon = getParameter("appIcon"); 

     String val = getParameter("updateFrecuency"); 

     try { 
      updateFrecuency = Integer.parseInt(val); 
     } catch (NumberFormatException e) { 
      Logger.getTrace().debug("No se pudo leer la frecuencia de actualización de los mensajes"); 
     } 

     updateFrecuency *= 1000; 

     username = System.getProperty("user.name"); 
     username = "Javier"; 

     System.out.println("creating instance"); 
     try { 
      System.out.println("setting look and feel"); 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception e) { 
      System.out.println("Unable to set LookAndFeel"); 
     } 

     JPanel panel = new JPanel(); 

    panel.add(new Label("Usuario: " + username)); 

    add(panel); 

     Image image = Toolkit.getDefaultToolkit().getImage(mediaResourcesPath + "//" + appIcon); 

     if (SystemTray.isSupported()) { 
      System.out.println("system tray supported"); 
      tray = SystemTray.getSystemTray(); 

      ActionListener exitListener = new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        System.out.println("Exiting...."); 
        System.exit(0); 
       } 
      }; 
      PopupMenu popup = new PopupMenu(); 
      MenuItem defaultItem = new MenuItem("Exit"); 
      defaultItem.addActionListener(exitListener); 
      popup.add(defaultItem); 
      defaultItem = new MenuItem("Open"); 
      defaultItem.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        setVisible(true); 
        setExtendedState(JFrame.NORMAL); 
       } 
      }); 
      popup.add(defaultItem); 

      trayIcon = new TrayIcon(image, "Tareas Italo", popup); 
      trayIcon.setImageAutoSize(true); 

     } else { 
      System.out.println("System tray not supported"); 
     } 
     addWindowStateListener(new WindowStateListener() { 
      public void windowStateChanged(WindowEvent e) { 
       if (e.getNewState() == ICONIFIED) { 
        try { 
         tray.add(trayIcon); 
         setVisible(false); 
         System.out.println("added to SystemTray"); 
        } catch (AWTException ex) { 
         System.out.println("unable to add to tray"); 
        } 
       } 
       if (e.getNewState() == 7) { 
        try { 
         tray.add(trayIcon); 
         setVisible(false); 
         System.out.println("added to SystemTray"); 
        } catch (AWTException ex) { 
         System.out.println("unable to add to system tray"); 
        } 
       } 
       if (e.getNewState() == MAXIMIZED_BOTH) { 
        tray.remove(trayIcon); 
        setVisible(true); 
        System.out.println("Tray icon removed"); 
       } 
       if (e.getNewState() == NORMAL) { 
        tray.remove(trayIcon); 
        setVisible(true); 
        System.out.println("Tray icon removed"); 
       } 
      } 
     }); 
     setIconImage(image); 

     try { 
      tray.add(trayIcon); 
      setVisible(false); 
      System.out.println("added to SystemTray"); 
     } catch (AWTException ex) { 
      System.out.println("unable to add to tray"); 
     } 

     setSize(300, 200); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public static void start(String[] args) { 
     File f = new File(""); 
     f = new File(f.getAbsolutePath() + "/conf/running.conf"); 
     f.delete(); 

     Logger.getTrace().debug("Iniciando"); 
     AWidget italoW = new AWidget(); 
     System.out.println("Instancia"); 

     new NotifyManager(italoW); 
    } 
    public static void stop(String[] args) { 
     try { 
      File f = new File(""); 
      f = new File(f.getAbsolutePath() + "/conf/running.conf"); 
      FileWriter fw = new FileWriter(f); 
      fw.append("false"); 
      fw.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     try { 
      if(args != null && args.length >0) { 
       if("stop".equals(args[0])) { 
        File f = new File(""); 
        f = new File(f.getAbsolutePath() + "/conf/running.conf"); 
        FileWriter fw = new FileWriter(f); 
        fw.append("false"); 
        fw.close(); 

       } else { 
        File f = new File(""); 
        f = new File(f.getAbsolutePath() + "/conf/running.conf"); 
        f.delete(); 
       } 
      } 
      Logger.getTrace().debug("Iniciando"); 
      AWidget italoW = new AWidget(); 
      System.out.println("Instancia"); 

      new NotifyManager(italoW); 

     } catch(Throwable th) { 
      Logger.getTrace().debug("ERROR:::" + th.getMessage()); 
     }   
    } 

    public void finish(){ 
     tray.remove(trayIcon); 
     dispose(); 
    }  
} 

感謝您的幫助。

+0

我想這一個是非常相似的問題,我昨天發佈: - [http://stackoverflow.com/questions/11549881/java-service-wrapper-to-create-service-for-java-ui - 應用](http://stackoverflow.com/questions/11549881/java-service-wrapper-to-create-service-for-java-ui-application) – Rohan 2012-07-19 11:09:46

回答

6

默認情況下,Windows出於安全原因不允許服務與桌面進行交互。 您必須創建兩個進程:一個用於唯一的服務部分(作爲服務運行,不進行任何交互),另一個進程作爲Windows應用程序標準運行。 並在它們之間建立溝通。

+0

是的,使用套接字通信來解決這個問題:),謝謝 – Buminda 2017-10-25 04:57:36

0

從什麼我可以從你的問題,你有可能被你的代碼重寫從System.properties電話接收到一個靜態字符串的用戶名引起了第二個問題,在這裏看到的理解:

username = System.getProperty("user.name"); 
username = "Javier"; //remove me 

至於你的其他問題,我會看看你使用的包裝。我敢肯定,它有一些API。 (我還沒有玩過服務......)。

相關問題