Java作为Windows服务,具有交互式桌面支持和阅读当前记录的用户 [英] Java as windows service with interactive desktop support and read curren logged user

查看:74
本文介绍了Java作为Windows服务,具有交互式桌面支持和阅读当前记录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个Java程序,其工作方式类似于用apache Common Deamon(prunsrv)包装的Windows服务,但是我有2个问题:

1.该服务的配置使用Interactive桌面可以查看摆动对话框并从应用程序尝试图标。但是idalogs and try图标不会出现。

2.交互式桌面如何仅与本地系统帐户一起使用,应用程序无法读取当前登录的用户,该用户名是应用程序所必需的


I have a java program that works like a windows service wrapped with apache Common Deamon (prunsrv) but i have 2 problems:
1. The service its configured with Interactive desktop to can see the swing dialogs and try icon from application. But the idalogs and try icon doesn´t appears.
2. How the interactive desktop only works with Local System Account the application can´t read the current logged user, this username its necessary for application

然后我需要解决这2个问题,谢谢,我粘贴了主类的代码

Then i need to resolve this 2 issues, thanks, i paste the code from main class

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();
    }       
}

感谢您的帮助。

推荐答案

默认情况下,出于安全原因,Windows不允许服务与桌面进行交互。
必须创建两个进程:一个用于唯一的服务部分(作为服务运行,没有任何交互),另一个用于Windows应用程序标准运行。
并在它们之间建立通信。

By default Windows not allow to a service to interact with Desktop for security reason. you must create two process : one for the only service part (run as service, without any interacting), and another process run as Windows application standard. and create a communication between them.

这篇关于Java作为Windows服务,具有交互式桌面支持和阅读当前记录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆