简体   繁体   English

java.awt.Trayicon 在 java 应用程序安装为 windows 7 服务时不显示

[英]java.awt.Trayicon is not displaying when java application is installed as windows 7 service

I have installed my Java application as windows service on Windows 7(32 bit) OS.我已经在 Windows 7(32 位)操作系统上安装了我的 Java 应用程序作为 Windows 服务。 My application is expected to show java.awt.TrayIcon on notification area when service starts.我的应用程序应该在服务启动时在通知区域显示 java.awt.TrayIcon。 But it is no showing the Icon.但它没有显示图标。 The same application is working fine on Windows XP.相同的应用程序在 Windows XP 上运行良好。 Anybody has any idea about that?有人对此有任何想法吗?

A service on windows is not graphical. Windows 上的服务不是图形化的。 Because it's running without any user loggued.因为它在没有任何用户登录的情况下运行。 If you want to have a Tray Icon and a graphical window to manage your service, you have to write another program which communicate with your service (local network, dcom,...) and add this program to the session startup.如果你想要一个托盘图标和一个图形窗口来管理你的服务,你必须编写另一个与你的服务(本地网络、dcom 等)通信的程序,并将这个程序添加到会话启动中。 (It's in that way I've done my own java service on my computers). (就是这样,我在我的电脑上完成了我自己的 java 服务)。


Ok, I've worked under Windows server 2003. Maybe service can't have GUI since windows 2003?好的,我在 Windows server 2003 下工作过。也许自 Windows 2003 以来服务不能有 GUI?

The code you have to develop depends on what you want?您必须开发的代码取决于您想要什么? Just have a trayicon to monitor that te service process is still running?只是有一个托盘图标来监控服务进程是否仍在运行? Display a parameters windows when clicking on the systray?...单击系统托盘时显示参数窗口?...

I've put you a simple code (without exception management to clarify the code) to monitor that the service is still running and responding (this is a standard java code, no special lib need):我给你放了一个简单的代码(无异常管理来澄清代码)来监视服务仍在运行和响应(这是一个标准的java代码,不需要特殊的lib):

The systray program:系统托盘程序:

//if any Exception --> ERROR CODE
Socket socket = new Socket("localhost", 25146);
DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
    outToServer.writeBytes("ping\n");
    //Wait maximum 5s to have an answer from the service
    for (int i = 0; inFromUser.ready() == false && i < 5; ++i) {
        Thread.sleep(1000);
    }
    if (inFromUser.ready() == false) {
        //ERROR CODE (change systrat icon, display balloon tooltip,...)
    } else {
        pong = inFromUser.readLine();
        //Check the answer
    }   

    //Check only every second (don't flood yout computer ;))
    Thread.sleep(1000);
}
inFromUser.close();
outToServer.close();
socket.close();

The service program code:服务程序代码:

ServerSocket socket = new ServerSocket(this._port);
while (true) {
    Socket connection = socket.accept();
    Thread thread = new Thread(new Runnable {
        BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        DataOutputStream outToClient = new DataOutputStream(connection.getOutputStream());
        Scanner scanner = new Scanner(inFromClient);
        while (scanner.hasNextLine()) {
            outToClient.writeBytes(scanner.nextLine());     
        }
        inFromClient.close();
        outToClient.close();
        connection.close();     
    });
    thread.start();
}

If you want to popup a parameter window, you can try either to writer the GUI in your systray program, write the result into a parameter file, and send a special keyword to your service to reload the parameters files... or either to write the GUI directly into your service, send a special keyword, and then do a jframe.setVisible(true) and hope it will be display on the current session... :)如果你想弹出一个参数窗口,你可以尝试在你的系统托盘程序中编写 GUI,将结果写入参数文件,然后向你的服务发送一个特殊的关键字以重新加载参数文件......或者要么编写GUI 直接进入您的服务,发送一个特殊关键字,然后执行 jframe.setVisible(true) 并希望它会显示在当前会话中... :)

Microsoft changed how interactive services work back in 2006. What worked in Windows NT, XP and Server 2003 no longer work in Vista, 7 or Server 2008 thanks to "Session 0 Isolation".微软在 2006 年改变了交互式服务的工作方式。由于“会话 0 隔离”,在 Windows NT、XP 和 Server 2003 中工作的内容不再在 Vista、7 或 Server 2008 中工作。

Read more here:在此处阅读更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM