This website explains how to compile and run Java 2 SE 5.0 (Java 1.5) programs on the Mac OS X. It is intended as an introductory guide for first- and second-semester Java students, but can be used by anyone. I've also recently added an additional section with information on how to make your Java programs more Mac-friendly.
Installation Instructions For Tiger (10.4)
If you are using Tiger, make sure you have upgraded to the most recent release of the operating system (10.4.7 at present). You can check what version of the OS you're running by going to "About This Mac" in the drop-down Apple menu. If your version is an older version, please run "Software Update" (also from the drop-down Apple menu). IMPORTANT: Mac OS X now automatically comes bundled with the latest version of Java. See the Using Java on your Mac section to continue from here.
If you ever want to check what version of Java you are running:
- Open up the Terminal by going to Applications > Utilities > Terminal.app.
- Type: ls -l /System/Library/Frameworks/JavaVM.framework/Versions/
- You should see "Current JDK" listed, with an arrow pointing to some version numbers. If you have the most current version installed, it should say "Current JDK -> 1.5.0". That number is the current version of Java you are using.
Instructions For Panther (10.3)
If you are using Panther, you cannot install Java 1.5. The latest version of Java that Panther supports is 1.4. You can still write basic programs with Java 1.4, however, as long as you add support for the Scanner class (this will satisfy the requirements for CS 101 and most other first-semester programming courses). You can go here for good and simple instructions on how to install the pseudo-Scanner class.
Using Java on your Mac
There are three main options for writing Java programs on your Mac: XCode, Eclipse, or the Terminal and a simple text editor.
- Eclipse: Eclipse is an open source project that is available for free on both Windows and Macintosh. I highly recommend Eclipse for students who are new to Java, because it is a standard program that will be familiar to professors and TAs. Eclipse is also the program that UVA's CS201 course uses.
- XCode: XCode is a Macintosh-specific application that comes with every copy of Tiger. If you have Tiger, XCode is already on your computer! You can begin using XCode by going to Applications > Installers > XCode Tools > XcodeTools.mpkg. Once XCode is installed, you will find it in [name of your computer] > Developer > Applications. To start a new Java project, go to File > New Project > Java > Ant-based Application Jar. When you compile your program, you will find a Jar automatically created for you in the "dist" folder and your source code in the "build" folder.
- The Terminal: Mac OS X provides you with a nifty Terminal application, from which you can run and compile code. By using the Terminal to run and compile code, you save memory, save screen space, and look like a super cool hacker. However, the usefulness of a command line and text editor to write code are limited. You may want to familiarize yourself with the Terminal whether or not you choose to use it frequently.
The following instructions teach you how to use the Terminal and a text editor to write, compile, and run programs. If you click on the small screenshot below, you can see what your commands will look like.
- Write your program in a text editor. I highly recommend SubEthaEdit (free for non-commercial use) to write programs. For example, I save a file FileName.java in adrienne > Documents > CompSci.
- Then, to compile and run the program, I open up the Terminal (Applications > Utilities).
- It opens up to my home directory (adrienne). I have to change to the directory that my Java programs are saved in, so I type: cd ./Documents/CompSci . For an overview of Terminal commands, go to here. To return up to your home directory, type ".." (two periods).
- To compile the program, I type: javac FileName.java
- It displays some errors, which I fix and then re-compile.
- To run the program, I type: java FileName
At some point in time you may also want to use java -jar Foo.jar [args], which executes a .jar file. If you want to learn more of the intricacies of the Terminal, visit this tutorial.
Some Tips for Writing Mac-Friendly Programs
- Customizing the menu bar. Most Mac programs display their menu bar at the very top of the screen, in line with the apple icon. By default, however, Java programs put their menu bar inside of the window that the JFrame is associated with. This arrangement looks strange to a Mac user, and luckily there is a quick fix.
Before you initialize the JFrame, set the system property apple.laf.useScreenMenuBar to true. For example:
System.setProperty("apple.laf.useScreenMenuBar", "true");
JFrame frame = new JFrame("Camera Control Interface");
etc. etc. etc.
frame.setVisible(true);

Please note that older versions of Java used com.apple.macos.useScreenMenuBar, which has been deprecated since Java 1.4.1.
- Moving the re-size square/bar. If the re-size square is intruding on a button or otherwise bothering you, set com.apple.mrj.application.growbox.intrudes to false. For example:
System.setProperty("com.apple.mrj.application.growbox.intrudes","false");
System.setProperty("apple.laf.useScreenMenuBar", "true");
JFrame frame = new JFrame("Camera Control Interface");
etc. etc. etc.
frame.setVisible(true);
- I have not been able to locate an up-to-date and comprehensive list of Mac "comfort" tips, so I will continue to update this as I come across issues on my own. If you know of a site, please send it to me and I will link to it from here.
Tutorial written by Adrienne Felt. Last updated 10 September 2006.