title: How do I do OS-specific notifications in Java?
body: <p>I am not a programmer by trade (any Java knowledge I have comes from the School of Hard Knocks).  Please forgive me for the stupid question I'm about to ask, and answer appropriately.</p>
  
  <p>A Java app I'm working on uses very buggy platform-agnostic notifications (such as when a file has been successfully downloaded).  I want to use platform-aware notifications.  The code to raise a notification on Linux is quite simple:</p>
  
  <pre><code>import org.gnome.gtk.Gtk;
  import org.gnome.notify.Notify;
  import org.gnome.notify.Notification;
  
  public class HelloWorld
  {
      public static void main(String[] args) {
          Gtk.init(args);
          Notify.init("Hello world");
          Notification Hello = new Notification("Hello world!", "This is an example notification.", "dialog-information");
          Hello.show();
      }
  }
  </code></pre>
  
  <p>On Mac it's a bit more complicated but still doable:</p>
  
  <pre><code>interface NsUserNotificationsBridge extends Library {
      NsUserNotificationsBridge instance = (NsUserNotificationsBridge)
              Native.loadLibrary("/usr/local/lib/NsUserNotificationsBridge.dylib", NsUserNotificationsBridge.class);
  
      public int sendNotification(String title, String subtitle, String text, int timeoffset);
  }
  </code></pre>
  
  <p>It requires a dylib obtainable from this github repository: <a href="https://github.com/petesh/OSxNotificationCenter" rel="nofollow noreferrer">https://github.com/petesh/OSxNotificationCenter</a></p>
  
  <p>Windows way is like this:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.TrayIcon.MessageType;
  
  public class TrayIconDemo {
  
      public static void main(String[] args) throws AWTException {
          if (SystemTray.isSupported()) {
              TrayIconDemo td = new TrayIconDemo();
              td.displayTray();
          } else {
              System.err.println("System tray not supported!");
          }
      }
  
      public void displayTray() throws AWTException {
          //Obtain only one instance of the SystemTray object
          SystemTray tray = SystemTray.getSystemTray();
  
          //If the icon is a file
          Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
          //Alternative (if the icon is on the classpath):
          //Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("icon.png"));
  
          TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
          //Let the system resize the image if needed
          trayIcon.setImageAutoSize(true);
          //Set tooltip text for the tray icon
          trayIcon.setToolTip("System tray icon demo");
          tray.add(trayIcon);
  
          trayIcon.displayMessage("Hello, World", "notification demo", MessageType.INFO);
      }
  }
  </code></pre>
  
  <p>The point is, I want these snippets to execute <em>only</em> on the appropriate platform; I don't want Java to compile, say, the GTK method on Windows, because the dependency for it doesn't exist.</p>
  
  <p>How do I make it so that Java recognises it, like "Hey, I'm compiling for a Mac system, so I'm using the Mac version of the code."</p>