I'm trying to make a java application on Mac that puts an icon in the status bar, but I don't want the jar icon on the Mac dock (the one that has the coffee cup on the paper). So I tried to use the System.setProperty(java.awt.headless, true) technique, but then I can't put anything in the SystemTray menubar because I get an HeadlessException. If anybody knows a way to get around this, help would be appreciated.
-
Ideally, you'd want your app to be a "UI element". For a Cocoa app, that would mean having the `LSUIElement` key in the Info.plist, set to 1. Don't know about for a Java app. – Ken Thomases May 19 '12 at 08:44
-
By "status bar" you mean menu bar? You can obviously change the dock icon but I don't know of any way to programmatically remove the dock icon. That requires native Cocoa code and admin privileges. – Gray May 19 '12 at 13:18
-
1Because of the "headlessexception", I'm wondering if this is something that ***cannot be worked around*** because of Apple security concerns (i.e. the potential of silent & malicious Java apps being spawned by infected web sites). – Michael Dautermann May 24 '12 at 10:01
-
I just figured that if other apps can do it and you can remove both, you can remove just one.I feel like that would make sense considering if it was a security reason, they wouldn't let you do both. – Shawn Shroyer May 26 '12 at 14:00
2 Answers
it's easy... if you know how :)
first wrap your jar file in a mac application bundle
- either use the "jar bundler" app that comes with the mac developer tools
- or use the -unrelated- jarbundler ant task http://jarbundler.sourceforge.net/
then go into the contents of your generated package and open the info.plist. there just add the LSUIElement property and set it to 1. this removes the application from the dock when started. also see the apple docs here: http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html
for completeness: there's also another way to do this, but it's much more painful. there is a cocoa command that allows you to show/hide the dock icon dynamically: SetSystemUIMode (https://developer.apple.com/library/mac/#documentation/Carbon/reference/Dock_Manager/Reference/reference.html) you could either try to call this command using rococoa or write your own jni lib. alternatively i would have an xcode project which does something very similar -hide the menu bar- in my github account: https://github.com/kritzikratzi/jAppleMenuBar/ you'd only have to change some parameters in the src/native/jAppleMenuBar.m file.

- 19,662
- 1
- 29
- 40
-
-
Pretty cool, but now as an unintended side-effect, when I try to launch the app when it's already running, it doesn't launch. Is there a way to know in the originally running app when someone tries to launch it again? I want to open the UI if they double click the app when it's already running. – phreakhead Jun 01 '14 at 00:02
-
either dig through eawt [1] and/or lwawt [2]. those are classes for osx that come bundled with the jdk for mac (disable forbidden references in your IDE). or see if you can implement NSApplicationDelegate [3] (eg using Rococoa) LINKS: [1] http://hg.openjdk.java.net/macosx-port/macosx-port/jdk/file/65910faed942/src/macosx/classes/com/apple/eawt [2] http://hg.openjdk.java.net/macosx-port/macosx-port/jdk/file/65910faed942/src/macosx/classes/sun/lwawt [3] https://developer.apple.com/library/mac/documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html – kritzikratzi Jun 02 '14 at 20:30
This avoids anything in the dock:
System.setProperty("apple.awt.UIElement", "true");
And this adds the tray icon, as shown in https://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html
//Check the SystemTray is supported
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon =
new TrayIcon(createImage("images/bulb.gif", "tray icon"));
final SystemTray tray = SystemTray.getSystemTray();
// Create a pop-up menu components
MenuItem aboutItem = new MenuItem("About");
CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
Menu displayMenu = new Menu("Display");
MenuItem errorItem = new MenuItem("Error");
MenuItem warningItem = new MenuItem("Warning");
MenuItem infoItem = new MenuItem("Info");
MenuItem noneItem = new MenuItem("None");
MenuItem exitItem = new MenuItem("Exit");
//Add components to pop-up menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(cb1);
popup.add(cb2);
popup.addSeparator();
popup.add(displayMenu);
displayMenu.add(errorItem);
displayMenu.add(warningItem);
displayMenu.add(infoItem);
displayMenu.add(noneItem);
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
}

- 28,273
- 12
- 60
- 94