CSCI 1321 (Principles of Algorithm Design II), Spring 2003:
Java Without an IDE


Contents

Overview

In this course we encourage you to use the Together IDE for at least some of your Java program development 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.

Writing Java programs

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

See Some Useful Links (Mostly for CS 1320/1321) (also linked from the course ``Useful links'' page) for links to more documentation about editors. If you plan to do more than a little program development in a Unix/Linux environment, it's probably worthwhile to try learning one of these editors; they're widely available and fast to use once you get past the learning curve.

Compiling Java programs

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.java
This 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.java
Be 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 compatibility with code written using Together, you can run its version of the compiler:
/users/TogetherSoft/Together6.0/jdk/bin/javac -classpath .:MyLib.jar Hello.java
That 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.

Running Java programs

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 Hello
To 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 Hello
You 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 Hello
That 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.

Common mistakes

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.



Berna Massingill
2003-01-16