I have installed my Java application as windows service on Windows 7(32 bit) OS. My application is expected to show java.awt.TrayIcon on notification area when service starts. But it is no showing the Icon. The same application is working fine on Windows XP. Anybody has any idea about that?
-
Have you set the "Allow service to interact with desktop" flag in the "Log On" panel of the Service properties? – Paul Cager Jan 24 '12 at 11:44
-
Yes i have set the flag "Allow service to interact with desktop". – imtiaz haider Jan 24 '12 at 11:58
2 Answers
A service on windows is not graphical. 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. (It's in that way I've done my own java service on my computers).
Ok, I've worked under Windows server 2003. Maybe service can't have GUI since windows 2003?
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):
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... :)

- 766
- 1
- 11
- 19
-
Thanks for your reply. Same application running on Windows XP without any problem. Is this a Windows 7 specific issue? Can you give more information on your solution? if possible with some code. – imtiaz haider Jan 24 '12 at 12:04
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".
Read more here:

- 2,345
- 2
- 17
- 24