CSCI 1321 (Principles of Algorithm Design II), Fall 2005:
Java Without an IDE


Contents

Overview

In this course we encourage students to develop Java programs with Eclipse because it has many built-in features that make it easier to work with large programs. 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
If that seems 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 have used in previous semesters to compile game-project programs 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
    
    javac -classpath .:PAD2Assn8.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. If that seems 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 have used in previous semesters to run game-project programs 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.

Generating HTML documentation from Java code

The javadoc command generates documentation in HTML format from ``javadoc'' comments (/** .... */) in Java source code. To generate documentation from all the Java source files in the current directory and put the result in your Local/HTML-Documents/MyDocs directory, you would type

javadoc *.java -d ~/Local/HTML-Documents/MyDocs
For more information on the javadoc command, see its man page (type man javadoc) or Sun's Web documentation.

Java packages versus directories/folders

Java normally expects source code for package x.y.z to be stored in a directory (folder) x/y/z. Eclipse automatically creates an appropriate directory structure when you create a package; if you're working from the command line, you should do it yourself. If you need to create many nested directories (e.g., x containing y containing z), you can do this with mkdir -p (e.g., mkdir -p x/y/z).

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
2005-09-16