2
I have a program in java
, no graphical interface, just console. The only output commands are System.out.println()
.
I implemented it so the program would have a trayicon. The problem is that I only want the active trayicon when running and is currently running Tray+console.
I tried, but to no avail:
Runnable runner = new Runnable() {
public void run() {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("C:\\logo.png");
final PopupMenu popup = new PopupMenu();
MenuItem aboutItem = new MenuItem("WS");
Menu displayMenu = new Menu("Hora de Inicio");
MenuItem hora = new MenuItem(tempo);
popup.add(aboutItem);
popup.addSeparator();
popup.add(displayMenu);
displayMenu.add(hora);
TrayIcon trayIcon = new TrayIcon(image, "WebService", popup);
trayIcon.setImageAutoSize(true);
try {
tray.add(trayIcon);
this.setVisible(false);
trayIcon.displayMessage("Aviso!", "Programa continua em execução...", TrayIcon.MessageType.INFO);
} catch (AWTException e) {
System.err.println("Não pode adicionar a tray");
}
} else {
System.err.println("Tray indisponível");
}
}
};
EventQueue.invokeLater(runner);
How do I run this application:
After building in Netbeans IDE I simply use the "java -jar ws.jar
" or I use Launch4j, create a exe
and double click. It displays a black window (application standard+windows
What I would like is to hide the above screen and leave only via Tray(do not take the risk of someone closing the console and stopping the application)
Please add [mcve] of your code, so it is possible to reproduce the problem.
– user28595
@diegofm the example of the code used to try to hide the window is in the question.
– Lucas Torres
But it’s not enough to test. The problem may not even be there.
– user28595
Changed @diegofm
– Lucas Torres
Where is such a console called? Honestly I see no problem in the code and I’m not understanding the problem reported.
– user28595
@diegofm console would be the default output with any System.out.println(). For example System.out.println("hello world") will print "hello world" on the windows console if you run a java -jar program.jar
– Lucas Torres
@diegofm the problem is that this.setVisible(false) is not working. The console is still visible.
– Lucas Torres
Try to rotate your class with the
javaw
, after that you can close the console.– Math
If you don’t have a container or swing/awt component, setvisible won’t work.
– user28595
@Math will try and return
– Lucas Torres
@Math what is the difference between the two? Using javaw the expected result has been achieved.
– Lucas Torres
@Lucastorres the difference is that the command
javaw
is not associated with a console window, so after you run the command your java application will run independently of the window, in the case ofjava
your window will be waiting for your application to close so that it can proceed with new commands.– Math