In this course we have encouraged you to develop Java programs with Together because of its built-in support for UML. But it is perfectly possible to write, compile, and run Java programs with any text editor and the command-line tools that come with Sun's Java implementation. This document is an overview of doing so.
You can write Java programs with any text editor (program that lets you edit standard ASCII text files). Among Unix enthusiasts, whose editor is better is a subject of perennial debate. Two popular and powerful (if not exactly novice-friendly) groups of editors are
Sun's Java compiler is installed on the department Linux machines as javac. So you could compile, for example, program Hello.java by typing the following command.
javac Hello.javaThis assumes that all the classes needed by Hello.java are either part of the standard Java libraries or in the current directory. If some of the classes referenced in Hello.java are in a JAR file, say MyLib.jar, you would need to type
javac -classpath .:MyLib.jar Hello.javaBe advised that the javac installed on the Linux machines is release 1.4, while the compiler included as part of Together is release 1.3. To be absolutely sure of compability with code written using Together, you can run its version of the compiler:
/users/TogetherSoft/Together6.0/jdk/bin/javac -classpath .:MyLib.jar Hello.javaThat being pretty tedious, you can define an alias (see the man page for your shell, probably bash), or you might write a simple script such as the following, which is much like the one I use to compile the game(s) I'm writing for this course. (To use this, cut and paste the code into a file, say mycompile, and then make it executable with the command chmod u+x mycompile. Run it by typing mycompile to compile all .java files or mycompile Hello.java to compile just Hello.java.)
#!/bin/sh # # Compile game. # # Arguments are files to compile; if no arguments, compiles all .java files. # if [ ".$1" = "." ] then files="*.java" else files=$* fi echo compiling $files /users/TogetherSoft/Together6.0/jdk/bin/javac -classpath .:PAD2Assn3.jar $files
For more information on the javac command, see its man page (type man javac) or Sun's Web documentation.
Unlike programs written in C++ and many other languages, Java programs do not compile to code that can be executed directly. Instead they compile to ``byte code'' (.class files) meant to be executed by a Java virtual machine. Sun's JVM is installed on the department Linux machines as java. So if you want to run the main method in class Hello, you would type the following command.
java HelloTo pass command-line arguments to main in Hello, type them at the end of the above command separated by spaces. Note that arguments that contain spaces or other characters that mean something to your shell (e.g., the wildcard character ``*'') need to be enclosed in double or single quotes. Again, the above command assumes that all the classes needed by Hello.java are either part of the standard Java libraries or in the current directory. If some of the classes referenced in Hello.java are in a JAR file, say MyLib.jar, you would need to type
java -classpath .:MyLib.jar HelloYou can abbreviate -classpath -cp. Be advised that the java installed on the Linux machines is release 1.4, while the compiler included as part of Together is release 1.3. To be absolutely sure of compability with code written using Together, you can run its version of the compiler:
/users/TogetherSoft/Together6.0/jdk/bin/java -classpath .:MyLib.jar HelloThat being pretty tedious, you can define an alias (see the man page for your shell, probably bash), or you might write a simple script such as the following, which is much like the one I use to run the game(s) I'm writing for this course. (To use this, cut and paste the code into a file, say myrun, and then make it executable with the command chmod u+x myrun. Run it by typing myrun, possibly followed by command-line arguments.)
#!/bin/sh # # Run game. # # Arguments (if any) are command-line arguments to game's main class. # /users/TogetherSoft/Together6.0/jdk/bin/java -cp .:PAD2Assn.jar NoNameGame $*
You could write similar scripts for running Dr. Lewis's screen and image editors; just change the name of the class (NoNameGame to ScreenEditor) and add any command-line arguments you don't want to type in every time, just before the ``$*''.
For more information on the java command, see its man page (type man java) or Sun's Web documentation.
Here are some common mistakes made by beginners and not-beginners alike. Suggestions for additions to this list are welcome; send them to me at bmassing@cs.trinity.edu.