Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a java program run in the background of the computer

Tags:

I've looked on some other posts about this, but didn't really understand much from it.

I've made a program that works like a server while capturing different pictures of the screen. Now, i'd like the program to just be active in the background - like the programs that appear under hidden icons. Programs that are not directly shown at the bottom taskbar. Do i need to add some specific code inside my java program when i execute it to a jar file? Or do i need to create the project some other way?

enter image description here

enter image description here

I hope this was enough explanation - Thanks in advance

like image 647
Anders Lassen Avatar asked Aug 17 '16 16:08

Anders Lassen


People also ask

How do I make a Java program run automatically?

Put you program in this directory: %appdata%\Microsoft\Windows\Start Menu\Programs\Startup Everything in there will be executed at startup.

How does computer run Java program?

To run a Java program, whether an applet or an application, the JVM is then used to interpret and execute the bytecode. The Java SE comes in two parts, a runtime program, called the Java Runtime Environment (JRE) and a development package, called the Software Development Kit (SDK).

Can Java run on any machine what is required to run Java on a computer?

To sum it up, Java, when compiled, creates a bytecode (. class file), which can be run in any machine which supports JVM. So once compiled it doesn't require re-compilation at every machine it runs, JVM converts the bytecode to be understood by the underlying hardware.


2 Answers

Something super simple that I got from Here. All I did was add an exit on click.

Code

public static void main (String [] args) {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;
    }
    Image image = Toolkit.getDefaultToolkit().getImage("MY/PATH/TO_IMAGE");

    final PopupMenu popup = new PopupMenu();
    final TrayIcon trayIcon = new TrayIcon(image, "MY PROGRAM NAME", popup);
    final SystemTray tray = SystemTray.getSystemTray();

    MenuItem exitItem = new MenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(1);
        }
    });
    popup.add(exitItem);

    trayIcon.setPopupMenu(popup);

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.out.println("TrayIcon could not be added.");
    }
}

Just get any image and add it to your resources or wherever you keep your images and make a path to it.

like image 135
cunniemm Avatar answered Sep 26 '22 16:09

cunniemm


You can achieve this by using the java.awt.SystemTray API in combination with Java Swing API.

Refer this documentation from Oracle:

Oracle Java documentation for System Tray API

like image 27
anacron Avatar answered Sep 22 '22 16:09

anacron