Mixing Java and Titanium: Part 1
Pages: 1, 2
Mac OS X: Developer Appeal
What is it about Mac OS X that is so attractive? What is the substance underneath the sleek hardware and cool window effects?
First off, OS X is Unix underneath. It isn't just a "Unix-like" OS, nor is it an OS that exposes only some POSIX-like functionality to C programmers. At any time, you can pop open a Terminal window to examine processes, grep through files, or execute a quick Perl script. Or, better yet, pipe the results of "grepping" through a set of files to a Perl script.
Thankfully, Apple isn't ashamed of the Unix foundation of Mac OS X and hasn't tried to hide it from the power user. In many respects, I feel like I am running a FreeBSD operating system that lost X Windows along the way in favor of something much nicer -- a platform that can run the latest versions of Office and Dreamweaver without requiring me to dual-boot.
The latest and greatest Java Development Kit is already installed by default. For the first time, there's no need to surf over to java.sun.com to pick up the newest bits of code. Apple exceeded expectations with its implementation of the JDK. Not only are Java threads mapped to Mach kernel threads (which was expected), but each virtual machine instance cooperates with other running virtual machines to increase performance. It does this by caching loaded class files as well as HotSpot-compiled code in shared memory. If a class file has already been loaded and optimized by one virtual machine, another virtual machine can just use that code without having to perform the work again.
Finally, Mac OS X ships with a strong suite of development tools. Apple is the first vendor to ship a consumer operating system with a fully functional set of development tools in the same box -- for no extra charge. And not only are Project Builder and Interface Builder (Apple's excellent IDE tools) included, so are emacs, vi, and other Unix text editors. This is perfect for those who are comfortable using traditional tools, or who need to use something familiar when the pressure is on.
In essence, even though Mac OS X is strikingly new, it's also surprisingly comfortable for any developer -- Java or otherwise -- that has a Unix background. No matter how pretty the interface is or how cool it is that movies still play as their windows are being minimized, you can get right down to work and get things accomplished. If this is what "Think Different" means, I'm all for it.
Mac OS X and Java: Where's my '$JAVA_HOME'?
The first thing I needed to do when starting to work with Mac OS X's Java implementation was set my JAVA_HOME environment variable. Several Java programs use this environment variable to help them locate various JAR files that are not on the default classpath. Because the default Terminal shell on OS X is tcsh, it is easy enough to set your JAVA_HOME to the necessary string as long as you know what that string should be. However, finding the installation directory of Java on Mac OS X was an adventure of its own that led to some interesting questions.
To find the right value for JAVA_HOME, I first executed a `which java` in the nearest Terminal. this told me that /usr/bin/java was the executable that launched the virtual machine. A quick ls -l /usr/bin/java told me that this wasn't the symbolic link to the JDK installation that I was hoping for. Instead, it is a short shell script that executes:
exec /System/Library/Frameworks/JavaVM.framework/Versions/1.3/Commands/java "$@"
From this bit of information I was able to poke around and find that I could set JAVA_HOME to:
/System/Library/Frameworks/JavaVM.framework/Versions/1.3/Home
This was all I needed to keep everything running. But the Versions/1.3 string intrigued me a little bit, so I dug a bit deeper into the directory structure of the framework and actually found that I could use a shorter string:
/System/Library/Frameworks/JavaVM.framework/Home
To solve this problem forevermore, I dropped the following line into my ~/.login file:
setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home
Now that I had a solution to the problem at hand, I really wanted to know more about how frameworks were implemented under OS X. After all, everything from AppKit to Cocoa and Carbon to OpenGL is contained in the /System/Library/Frameworks folder. I found the answer in Apple's Inside Mac OS X: System Overview. In short, a framework is a type of bundle that packages shared libraries along with resources. In addition, frameworks are versioned which should help developers address compatibility problems as Mac OS X evolves.
After wrapping my head around frameworks for a while, I dug into the other kinds of bundles that exist in Mac OS X, including application bundles. It turns out that items that look like single file applications in the Finder are actually hierarchically structured directories containing the program and its resources. I'll talk more about how you can package up Java applications into these bundles in the upcoming articles.
James Duncan Davidson is a freelance author, software developer, and consultant focusing on Mac OS X, Java, XML, and open source technologies. He currently resides in San Francisco, California.
Return to the Mac DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 9 of 9.
-
Easier solution to setenv JAVA_HOME
2001-12-01 12:28:49 basilbourque [Reply | View]
-
Easier solution to setenv JAVA_HOME
2001-12-04 13:56:59 James Duncan Davidson |
[Reply | View]
Good tips.
I've also found that most applications that want JAVA_HOME can be spoofed by setting JAVA_HOME to /usr of all things.This falls in the category of stupid pet tricks, but since almost all apps that want JAVA_HOME set are trying to find the javac compiler classes, and those classes are already on the classpath in the Apple VM, this does work.
-
Java on OS X is, typically, a curate's egg...
2001-10-14 13:50:48 jthwaites [Reply | View]
...good in parts. But it's the completion that matters - in my case, I need Java 3D.
Blast, back to Windoze again...
-
Java/Apple and the Web
2001-09-24 04:43:53 garrett948 [Reply | View]
Greetings,
I think the article is quite good and appreciate the enthusiasm. I am left with one question, however. Why does my applet work on every type of computer everywhere in the world except Apple above OS8.6/IE4.5?
This bit of java is important to people who need: schools, low-cost housing, disaster repairs, and railroad stations in rural underdeveloped countries. Few have Apple computers, but I do, and I am at the crossroad of either buying an old Apple with the old systems, or, drats, a PC.
See http://ferrocement.com/DrawAndSend/DrawFreehand_26_4_01.html and the error:
Please wait until data transmission messages appear below this line.
<HTML><HEAD><TITLE>Fatal Error</TITLE></HEAD>
<BODY>
<H1>Error:</H1>
The soupermail CGI died due to the following error:
<BLOCKQUOTE>
Unable to determine where the config file is.
</BLOCKQUOTE>
<HR>
Check your soupermail configuration or contact:
garrett@ferrocement.com
informing them of the error, and how and where it occured.
<HR>
Soupermail Release Version 1.0.7.1
</BODY></HTML>
-
Java and Mac OS X are a great combo!
2001-09-05 10:49:47 cgervais [Reply | View]
I've been drooling at the opportunity to have a full Java2 stack on my TiBook and Mac OS X hasn't disappointed. Probably the greatest benefit is the range of options that a developer has when writing Java code on Mac OS X.
First, you can write 100% pure Java code which uses AWT or Swing and you get a great-looking UI. I've had hardly any issues downloading software that was impossible to run with Mac OS 9's MRJ 2.2.5, which runs great on Mac OS X.
Second, you can optionally add Mac OS-specific technologies to your apps by using Apple's JDirect Carbon-Java bindings. This allows you to really create Mac-style apps with Java.
Thirdly, since Java is my favorite programming language, I can use the Cocoa APIs and have an ultra-rapid development environment. I wrote a quick front-end tool for Ant in Cocoa-Java that's very full-featured and works and acts just like a first-class Macintosh application. It's ridiculously cool.
Of course the biggest draw is that you can run it on hardware that's not only tuned for the environment, but that supports features that you just can't have under Windows or Linux. When was the last time you felt really good about running Linux on a laptop. I know folks do it, but Apple's really been able to tune OS X for their machines and the consequence is speed and stability.
Mac OS X is a great place to develop Java!
Chris






They have placed an alias (similar to a symbolic link) at:
/Library/Java/Home
This will take you to the current directory holding the equivalent of Sun's "jdk" directory. From there you can get to the /bin directory.
So, to set an environment variable such as JAVA_HOME or JDK_HOME, use:
setenv JAVA_HOME /Library/Java/Home
Also, for convenience you can set up these env vars in the invisible file ".tcshrc" found in your home directory. This file is run each time you open a Terminal window.
BBEdit is a popular Macintosh tool for editing files, now updated for Mac OS X. It includes an option for exposing hidden files so you can open them. It comes in both free and commercial version. http://www.bbedit.com/
More tips:
You do not need to install Apple's "Developer" CD-ROM which comes with Mac OS X, unless you want to use Apple-specific tools and non-Java stuff (gnu compilers, etc.). In the default Mac OS X installation, Apple already includes the full suite of java tools similar to Sun's JDK, such as java, javac, javadoc, jar, rmic, etc. (Yeh!)
The NetBeans ide (http://www.netbeans.org/) runs well on Mac OS X, if you use the latest versions.
--Basil