A Cup of Java for the Mac
Posted by Sebastien Arbogast on 10/1/07 in Articles, Development, Featured
As a passionate software developer, I am what people sometimes call a computer geek. And yet there is something very special about my geekiness: I hate coffee. I have to confess that I have never drunk a single cup of coffee and yet - I love Java!
Ok, right, that’s a bad word trick that has been used thousands and thousands of times. Of course the Java I’m talking about is not something to drink, it’s something to write: I’m talking about the programming language.
One day, I was interviewed for a job and my potential employer asked me a very tricky question: how could you explain Java programming to your grandma? Well, that’s not what I’m going to do here, I am not going to take you through Java programming from scratch. I’ll just assume that you already are a Java programmer on Windows or Unix, and you are afraid to switch to the Mac because of Java. Welcome to the wonderful world of Java development on the Mac. Please fasten your seatbelt…
Environment variables
One of the first issues you usually encounter is how to setup that damn JAVA_HOME environment variable. On Windows it’s relatively simple: click, click, click and click. On Mac OS X, it can be a little trickier. Let’s try to see what Apple has to say about setting environment variables. In a few words, the solution is to create or edit the file /Users/
For that, either you install developer tools from your Mac O SX installation disc and more specifically the Property List Editor, or you just use your favourite text editor. For the sake of convenience, here is my environment.plist file with a few useful environment variables for various Java-related tools:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FLEX_HOME</key>
<string>/Applications/flex_sdk_2_hf1</string>
<key>JAVA_HOME</key>
<string>/System/Library/Frameworks/JavaVM.framework/Home</string>
<key>JBOSS_HOME</key>
<string>/Applications/jboss-4.0.5.GA</string>
<key>M2_HOME</key>
<string>/Applications/maven-2.0.7</string>
<key>M2_REPO</key>
<string>/Users/sarbogast/.m2/repository</string>
<key>MAVEN_OPTS</key>
<string>-XX:MaxPermSize=128m -Xmx512m</string>
</dict>
</plist>
Save this file, log out and back in and that should be it.
Now in my experience, this only works when you want to add an environment variable. But what about extending existing ones, like $PATH for example. Some say it depends on your shell. Well that’s true: if you have modified your default shell to something like tcsh or something else, you will probably have to dive into your shell configuration file (.tcshrc for example). But if you’re just using the default shell, all you have to do is to edit /Users/
test -r /sw/bin/init.sh && . /sw/bin/init.sh
*Ed: Had to break the line below to fit in the page.
PATH=”/Applications/flex_sdk_2_hf1/bin:/Applications/maven-2.0.7/bin:
/usr/local/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}”
export PATH
As you can see, thanks to the “:${PATH}” at the end of the line, we’ve extended the default PATH variable and I’ve added useful things like the Maven bin directory. Don’t forget to log out and back in, and run commands like “echo $PATH” to check that your environment variable is correctly set.
Getting API and sources
Assuming that you have downloaded and installed the Mac OS X Java 5 distribution here, something might upset you a little bit: where are the JDK sources? Where is Javadoc? Both of these are very important when you’re developing using advanced IDE’s like Eclipse or so. Of course you often need to see the Javadoc of the class or method you’re trying to use.
But even more importantly is when you automatically generate some implementation of a standard interface from the JDK; something like TableCellRenderer for example. Well, your IDE names all the method parameters based on their name in the source. And if it can’t find the source, then they are named based on their type. That’s ugly, huh?
Well, at least I’ve been struggling with this for a while until I found a very interesting article in my RSS feeds. Basically, here is the procedure:
- Create yourself an Apple Developer Connection account if you don’t have one
- Log in to ADC website
- Then click the “Downloads” link
- Then click “Java” on the right menu
- And finally download “J2SE 5.0 Release 5 Developer Documentation”
- Once you’re done with setup, everything can be found and linked to in /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home
Isn’t it amazing?
Java editors
OK, now let’s talk about your development environment. Of course, as usual there are several schools here and I will just give you a few leads.
First things first: forget about XCode. Before getting my Mac, I thought “wow that’s cool, Mac OS X is actually packed up with a full IDE like Eclipse, and if it is as neat as their other apps, it might be interesting…” Bad guess! XCode might be really adapted for Cocoa and Objective-C development, but for Java, it’s really weird. That’s why I looked for other options.
The first thing that can be very useful is a simple text editor with syntax coloring and that kind of stuff. Very interesting when you just want to view an isolated file or for very small projects. Of course you could buy something like Textmate, which is very popular. But before getting $39 out of your pocket, you might want to consider downloading Smultron for free. And if you’re not satisfied with Smultron default icons, there’s even a neater set here.
For those Unix fans who can’t work outside of Emacs (and I’ve been a great Emacs fan myself when I was a student), there is this special distribution just for you. Now just to avoid a World War III between Emacs and VIM fans, I’ll just point to this very interesting article about Configuring VI for Java Development.
Of course, I won’t go into more details about Eclipse, which is of course available for the Mac. But since you have switched to a platform that provides so much more usability and ergonomy, maybe now would be the right time for you to try something better as a Java IDE. Something like… IntelliJ Idea. OK, it’s not free, it’s something like 250 bucks for a personal license, but believe me, if you work on big projects, it’s really worth it. As Jetbrains guys told us last year at JavaPolis, “it’s the only Java IDE that is actually worth paying for!”. And it works great on the Mac, so…
Last but not least, I want to talk to you about something else. It’s not a text editor or an IDE, but a companion that can be very precious in you Java tasks: iTerm. Of course, Mac OS X comes with its very own Terminal application, but iTerm offers a whole lot of additional features like tabs, bookmarks and so on.
Special keys
When I wrote my first Java classes on my brand new Mac (inside IntelliJ Idea of course), I first had big trouble writing something like:
if(foo == null || foo.bar() == 0)
//do that
}
Why? Because of the keyboard! OK, I don’t know what kind of keyboard layout you have, maybe it’s simpler on yours, but on my French Belgian keyboard, I couldn’t find characters like “|”. So I had to do a little bit of research and just in case you don’t find them either, here are a few tips:
| - alt+shift+L
{ - alt+5
} - alt+)
[ - alt+shift+5
] - alt+shift+)
What about native apps?
A few weeks ago, I was looking for a bandwidth counter to help me monitor my download/upload quota with my DSL provider. And I finally found a menu bar widget to do just that. But unfortunately it was not supported anymore and it didn’t work with the new infrastructure of my provider. So I contacted the author, just in case I could get the source code and try to upgrade it in some way. He kindly sent me the source code and I was very surprised to see that it was Java.
That’s when I discovered that there was a Java API on Cocoa libraries. Well, at least there used to be one! Apparently, Apple has made a choice: they won’t support these API’s anymore, and if you want to develop native apps for the Mac, you will have to learn a whole new language: Objective-C. I don’t know about you, but I’m not sure that Objective-C is gonna be profitable in the IT industry anytime soon, so I’m gonna stick with Java for a while, at least until I have some time left to learn a whole new language.
Fortunately, there is an alternative.
Look And Feel
There is something very special about Java on the Mac. Sun provides Java implementations for Windows, Linux and Solaris, but not for the Mac. It means that it’s Apple responsibility to provide its own implementation of the JDK. It’s very disturbing because it means that the Mac OS X JDK is always going to be behind Windows in terms of schedule. For example, there is no final implementation of JDK 6 for the Mac yet. But on the other hand, it means that Apple developers can really focus on adapting Java, and more specifically Java UI libraries to the Mac OS X look and feel.
For example, I was working on an MDI application and I was very surprised to see that when I minimized my internal frames, they actually minimized into some sort of dock at the bottom of the JDesktopPane. That’s something very specific in the Mac implementation. And then I found this article which details even more ways to customize your Java application for the Mac and make it look almost like a native application. Ain’t that cool?
Java 6 is coming through!
Now what about JDK 6? The truth is there is a “hidden” developer preview implementation of JDK 6 on the Mac but I can’t find it anymore. It seems that Apple is preparing a final release bundled with Mac OS X 10.5 Leopard due in October 2007. And this new release could feature a Swing look-and-feel adapted for the rumored new “Illuminous” style. Just a few weeks and we’ll see…
Anything else?
If you have other tips and tricks about developing Java applications on the Mac, feel free to leave comments and let’s make this post a reference for all Java developers on the Mac.
Guest
Article Author
Contributor
+0
Subscribe to MacApper's Full Page Feeds
Subscribe to MacApper's Podcast
13 Comment(s)