How to determine if your Java/Scala application is running on Mac OS X

If you're developing a Swing/Java application to run on multiple platforms, at some point you'll probably have to make some tweaks for each operating system. Yes, there are differences in Swing behavior between Mac OS X, Linux, and Windows, and you'll want to account for those.

Probably the best way to determine which system you're running on is to look at a system property named os.name. All you have to do is get this system property, which is a String, and then parse it, looking for different operating system names. For instance, if I just want to run a test to see if my Java application is currently running on Mac OS X, I would write code that looks like this:

String osName = System.getProperty("os.name").toLowerCase();
boolean isMacOs = osName.startsWith("mac os x");
if (isMacOs) 
{
  // do mac-specific things here
}

A Mac-specific test

If you really just want to see if your application is running on Mac OS X, you can also just look for the system property mrj.version (though I don't really recommend this). If you're running on Mac OS X, this property will not be null, but if you're running on another operating system, it will be null.

Here's the source code showing what that test looks like:

String mrjVersion = System.getProperty("mrj.version");
boolean isMacOs = mrjVersion != null;
if (isMacOs)
{
  // do mac-specific things here
}

Although I showed this method, I still prefer the os.name approach, as it works across all platforms.

Bonus: How to print all system properties

While I'm in the neighborhood of system properties, I thought I'd share a little Java code to demonstrate how to print all the system properties. This simple Java program prints all of the system properties:

import java.util.Properties;

public class PrintAllSystemProperties
{
  public static void main(String[] args)
  {
    Properties props = System.getProperties();
    props.list(System.out);
  }
}

As you can see, the program just gets a Properties object from the System class, and then prints out all the properties.

Here's the output from that program on my Mac OS X system, a MacBook Pro currently running Mac OS X 10.5.7. I sorted the output slightly to show the Mac-specific properties at the top of the list:

-- listing properties --
os.name=Mac OS X
mrj.version=1050.1.5.0_16-284
java.vm.vendor=Apple Inc.
java.vendor.url=http://www.apple.com/

java.awt.graphicsenv=apple.awt.CGraphicsEnvironment
awt.toolkit=apple.awt.CToolkit
java.vendor=Apple Inc.
java.vendor.url.bug=http://bugreport.apple.com/

java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=/System/Library/Frameworks/JavaVM.fra...
java.vm.version=1.5.0_16-133
awt.nativeDoubleBuffering=true
gopherProxySet=false
path.separator=:
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
user.home=/Users/al
user.dir=/Users/al/stuff
user.name=al
java.class.path=/Users/al/stuff...
java.runtime.version=1.5.0_16-b06-284
java.endorsed.dirs=/System/Library/Frameworks/JavaVM.fra...
os.arch=i386
java.io.tmpdir=/tmp
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
sun.jnu.encoding=MacRoman
java.library.path=.:/Library/Java/Extensions:/System/Li...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Client Compiler
os.version=10.5.7
user.timezone=
java.awt.printerjob=apple.awt.CPrinterJob
file.encoding=MacRoman
java.specification.version=1.5
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=/System/Library/Frameworks/JavaVM.fra...
java.specification.vendor=Sun Microsystems Inc.
user.language=en
java.vm.info=mixed mode, sharing
java.version=1.5.0_16
java.ext.dirs=/Library/Java/Extensions:/System/Libr...
sun.boot.class.path=/System/Library/Frameworks/JavaVM.fra...
file.separator=/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.cpu.isalist=