Compiling How to compile an mxj class

To develop Java classes you will create and edit text files of Java code, and then compile them into bytecode .class files that the Java Virtual Machine (JVM) needs. The traditional way to compile Java code is to use javac, a compiler that can be executed from the command line - in OS X this means using the terminal, and in Windows XP this means going to the DOS command prompt, or a unix-emulating layer such as Cygwin. For instance, if the working directory contains a text file called MyClass.java, the command

javac MyClass.java

compiles the source code in the text file and creates a bytecode file called MyClass.class in the working directory. If the code has a complicated structure it is possible for several different .class files to be generated. If the code contains errors the compiler outputs descriptions of the errors. IBM's jikes is another command-line Java compiler that can be used in the same way.

If you are reading this for the first time and you try the above command on any of the classes that live in java/classes, the compiler probably will return an error related to the fact that the Java compiler does not know where to look to find max.jar. To remedy this we have to add max.jar to our classpath. It's possible to do this in a few different ways, but perhaps the most straightforward way is with a command line argument to the compiler. On Mac we'd use

javac -classpath "/Applications/Max*/Cycling '74/java/lib/max.jar" MyClass.java

And on Windows,

javac -classpath "\Program Files\Common Files\Cycling '74\java\lib\max.jar" MyClass.java

The *.java source code files for the tutorial examples can be found in the java/doc/tutorial/src directory, so the following command could be used to compile the HelloWorld1 class on Mac:

javac -classpath "/Applications/Max*/Cycling '74/java/lib/max.jar" HelloWorld1.java

If you're using a unix terminal, to learn more about the javac compiler you can call up its manual pages:

% man javac

If the command line isn't your style, don't despair - it's actually possible to do all your Java development within the Max environment. After receiving a viewsource message an mxj object will launch an editing environment. We'll discuss this environment next when we talk about the quickie class.

Finally, it's also possible to compile your Java classes using one of many available IDE (Integrated Development Environment) packages. In the java/doc/ide directory we've provided example projects for a couple of great free options, Apple's Xcode and IBM's Eclipse. Please note that there is a bug in Xcode that's triggered by a path name with a single quote mark, so don't place any Xcode projects in your Cycling '74 directory.