Trayicon with hidden console

Asked

Viewed 132 times

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

inserir a descrição da imagem aqui

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.

  • @diegofm the example of the code used to try to hide the window is in the question.

  • But it’s not enough to test. The problem may not even be there.

  • Changed @diegofm

  • Where is such a console called? Honestly I see no problem in the code and I’m not understanding the problem reported.

  • @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

  • @diegofm the problem is that this.setVisible(false) is not working. The console is still visible.

  • Try to rotate your class with the javaw, after that you can close the console.

  • 1

    If you don’t have a container or swing/awt component, setvisible won’t work.

  • @Math will try and return

  • @Math what is the difference between the two? Using javaw the expected result has been achieved.

  • 1

    @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 of java your window will be waiting for your application to close so that it can proceed with new commands.

Show 7 more comments

1 answer

5


From what I’ve seen you’re no longer having problems with Trayicon, you just want a windows console window not to wait for the end of running your program. To solve this obstacle you can use the command javaw to call your class instead of the command java.

Of documentation:

The javaw command is identical to java, except that with javaw there is no Associated console window. Use javaw when you do not want a command prompt window to appear. The javaw Launcher will, However, display a dialog box with error information if a Launch fails.

In free translation:

The javaw command is identical to the java command, except that with javaw there is no associated console window. Use javaw when you don’t want a command window to appear. Launcher javaw will however display a dialog window with error information if the call fails.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.