A Cup of Java for the Mac

Java!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//.MacOSX/environment.plist which is actually an XML file.

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//.profile. Here is what is in mine:

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.

Comments

14 Responses to “A Cup of Java for the Mac”

  1. Magnus Nordlander on October 1st, 2007 11:20 am

    Or you could, like, download Xcode, and skip half of the steps, because Xcode installs everything you need for you. Even if you don’t plan to use it as your IDE, it’s still the easiest way to get everything you need.

  2. Sebastien on October 1st, 2007 11:25 am

    Are you sure about that? Because I’ve installed XCode and developer tools on my MacBook Pro and nothing was set up. No JAVA_HOME, no javadoc or java source. Or maybe there’s a package with all of that on the web somewhere. Would you have a link?

  3. Magnus Nordlander on October 1st, 2007 12:44 pm

    At least I have javadoc in /System/Library/Frameworks/JavaVM.framework/Commands/ and I haven’t knowingly installed anything Java related.

    As for developer documentation, isn’t that installed when you select it in the Xcode installation (I didn’t do that, because it takes forever, and very very rarely use Java).

    As for learning Objective-C, that’s pretty easy, if you’re used to object oriented programming, it takes a couple of days. However learning Cocoa is an entirely different story.

    Also, AFAIK, the reason the Java-Cocoa-bridge was deprecated is that very few people used it. Cocoa simply goes better with dynamic languages, like Objective-C. I read somewhere that WebObjects simply wasn’t as good after the move to Java because of the lack of categories.

  4. Jon Kantro on October 1st, 2007 1:09 pm

    Have you taken a look at MochaCode. http://syntori.com/mochacode/ It’s a complete java IDE with a mac look and feel.

  5. Sebastien on October 2nd, 2007 4:26 am

    That is very interesting. Still in early stage of development and not quite mature enough for day-to-day work but definitely a lot of potential. Thanks a lot for the link.

  6. nerg on October 2nd, 2007 7:54 am

    A very good free text editor id text wrangler from bare bones software

    http://www.barebones.com/products/textwrangler/

  7. Henrik Hartz on October 2nd, 2007 1:34 pm

    Hi! Great article! I love Java (i.e. the type that contains caffeine) as well ;-) I work for a company called Trolltech who create a cross-platform programming Framework called Qt. It just happens that we provide this API for Java as well, aptly named Qt Jambi. I think you’ll find it is very intuitive, and you might want to check it out if you’re looking to investigate other options available for Java development.

  8. Sebastien on October 2nd, 2007 2:05 pm

    I’ve already tried it, I even rediscovered the joy of C++ with Qt a few years ago. Qt is definitely a very interesting alternative to Swing or SWT. I don’t know how Qt’s look and feel can merge into native MacOSX style, but Qt Jambi is definitely very interesting, especially with mechanisms such as signals and slots. It makes me think of a possible future review… ;o)

  9. Henrik Hartz on October 2nd, 2007 2:26 pm

    Well, Qt is actually using native HIView’s and HITheme under the hood, so it should merge quite well ;-) Good luck!

  10. Sebastien on October 2nd, 2007 6:10 pm

    I just found another series of tips and tricks to adapt Java applications to Apple’s user interface guidelines and make them feel more native: http://simplericity.com/2007/10/02/1191336060000.html

    Some tips are already explained in DevDaily’s article, others are new. Quite interesting!

  11. Tonio Loewald on October 5th, 2007 11:48 am

    If you want to quickly develop solid applications for Mac, Windows, and/or Linux, I’d suggest RealBasic as a strong alternative to Java. By the time you’ve set your environment variables up for Java, you can have developed a working text editor in RealBasic. And by the time you’ve configured an IDE, well, you’re probably working on your third app.

    And RB apps effortlessly conform to Apple HI guidelines on Macs, Microsoft guidelines on Windows, and are as good as anything else under Linux.

    I think Java has its place (on servers, mainly) but just looking at that last link — With Java you jump through hoops to get a third rate interface.

  12. Sebastien on October 5th, 2007 1:55 pm

    Let me complete your statement for the sake of precision.
    With Java, at least you have a real-world-class application with clean, readable and maintainable code using open and vendor-unlocked technologies. And guess what. It’s portable too, so you don’t need to compile it for different systems.
    But let’s kill this troll in the egg!
    My post was about Java, not about RealBasic, Objective-C or any other programming language war of any sort.

  13. Sebastien Arbogast on November 7th, 2007 6:43 pm

    Some very interesting information concerning Java on Leopard: http://developer.apple.com/technotes/tn2007/tn2196.html

  14. idrive on February 9th, 2011 5:18 pm

    Hi Guys, I just found a pretty nice site for unlock / jailbreak/ iphone 3g 3gs and iphone4 jailbreak.

    http://www.anysim.net

    Thank u Anysim Team for fast Support. :)

    Did everybody now when the update ios 4.3 for iphone4 comes?

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!