oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Shell Scripts, Command Lines, and Classpaths
Pages: 1, 2

From the command line

Open up a terminal window and navigate to the jaxp directory. This is where the three .jar files are located, so create an environment variable called "JAXP" that points to this directory as follows.

setenv JAXP /Users/dhs/Applications/jaxp

Again, you will use your own user login in place of "dhs". Next, you will set an environment variable to hold the additional classpath information using the following command:

setenv CP $JAXP/jaxp.jar:$JAXP/crimson.jar:$JAXP/xalan.jar:.

The $JAXP is replaced by the value of the JAXP variable that you previously set. If you come from a Windows background, note that you need to use colons -- and not semicolons -- to separate the entries. Finally, you include "." so the current directory will be in the classpath. Now navigate to the jaxp/examples/SAXTagCount directory and compile the example like this.

javac -classpath $CP

The compiler will quietly churn and return a prompt. Now you are ready to run the example program. You need to pass it an XML DTD for which it will count the various tags. In the directory jaxp/examples/samples is an XML version of Shakespeare's Richard III called rich_iii.xml and the accompanying Document Type Definition, play.dtd. You can have SAXTagCount count the tags in Richard III with the following command.

java -cp $CP SAXTagCount "../samples/rich_iii.xml"

Your terminal window should look something like this.


You can use option (2) for storing the values of the environment variables in a .tcshrc or .login file. See James' article on finding his $JAVA_HOME for perspective.

One note for newbies -- you won't see the .login file if you are using your Finder. If you open up a terminal and type ls you still won't see these hidden files. You need to type ls -a to see them. There are a couple of strategies for creating and maintaining them. You can create them with TextEdit and then use the command cp login.txt .login to copy the file login.txt to one named .login.

The advantage here is that login.txt is not hidden and you can open it up again with TextEdit, change it, and then do the same trick to save it as .login. Of course, you can also use emacs and vi to edit these hidden files. My favorite solution, however, is to use BBEdit. BBEdit has an option in the File menu to "Open Hidden". When you select it, you can see the hidden files, open any one of them, edit it, and save it.

XML editors

You can edit an XML document with a simple text editor. In a way, that's the point of XML. On the other hand, XML files are hierarchical and it is nice to be able to see this structure when creating a file. For example, if you look at the play.dtd file it specifies the various components of a <PLAY>. You can see what a <PLAY> contains and in what order. A <SPEECH> can be contained in a <SCENE> that is in turn contained in an <ACT>. A <SPEECH> consists of a <SPEAKER> followed by one or more elements chosen from <LINE>, <STAGEDIR>, and <SUBHEAD>.

The idea for this article actually came when I started to play with the ElfData XML Editor available for download on the Apple web site. Here's a look at Richard III using this application.


On the one hand, this is a nice hierarchical view of the play. But if I alter Shakespeare by adding a <SPEECH> after this one, then right after I add the new <SPEECH> I should somehow be prompted to add the <SPEAKER> element as every <SPEECH> must begin with a <SPEAKER>.

As I looked around at other XML editors, I saw a familiar story. For the most part, the applications were Windows-only and none of them listed Mac OS X as a platform. And then I remembered Xeena. It is available for free and is a Java application. It has to work on Mac OS X -- right? Download the distribution and expand it and save it in the /Users/dhs/Applications directory (where again you replace "dhs" with your user name).

Shell scripts and running Xeena

Now that you've got Xeena downloaded, rename the directory xeena. Inside you'll find a lib directory that contains all the .jar files needed by this application. You'll also see .bat and .sh files used to run Xeena and various examples on Windows and Unix platforms. Take a look at the file. It is way too complicated. One of the advantages of running a Java application on Mac OS X is that you don't have to wonder about which virtual machine is being used and where it is. You can dramatically simplify the shell script so that consists of the following:




java -cp $CP $*

Note: The third item in the listing above should be all on one line, with no spaces between the ":" and the following "$" -- the line has been broken here for the sake of the display.

This should look familiar from your earlier experience. You are setting the location of the Xeena directory (yes, you should replace "dhs" from /Users/dhs/Applications/xeena with your own name). JAVA_HOME points to the home directory for the Java installation. You are then using these variables to set the classpath to point to the four jar files required by the application as well as the Java classes. The final step is to run the application from the jar file xmleditor.jar. You can now run Xeena by running the shell script. You could type this.


You don't get much for your money. You need to pass in information such as the name of the DTD, the root element, and the XML file that you're editing. To see an example, run one of the examples. For instance, type the following:


This brings up the following address book example.

Screen shot.

The application helps you insert the correct elements according to the specified DTD. More importantly, you now can create, edit, and run shell scripts.

Daniel H. Steinberg is the editor for the new series of Mac Developer titles for the Pragmatic Programmers. He writes feature articles for Apple's ADC web site and is a regular contributor to Mac Devcenter. He has presented at Apple's Worldwide Developer Conference, MacWorld, MacHack and other Mac developer conferences.

Read more Java Programming on the Mac columns.

Return to the Mac DevCenter.