Java Applications

This is a concise explanation of the command-line environment and other related things. For more information, see Sun's tutorial. Some sections are still under construction, as Java continues to evolve.


1. Java Programs

1.1 Applications and Applets

You can write Java programs in two general ways:

1.2 Main Class

To be an application, one Java class must contain a main method. The main method typically has this header:

      public static void main(String[] args)
Every class in Java can have a main method. The user must decide which class to use for a particular application. The class whose main you use to run your program is called the Main Class.

1.3 Files and Classes

This section describes general rules for where you place your code:

2. Command-Line Environments

2.1 Operating Systems and Window Environments

The advice herein is really just a snippet of Sun's instructions at http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html:

2.1.1 Using Microsoft Windows

Accessing command windows:

Using MSDOS: for a full set of DOS commands, enter help at the DOS prompt. I've summarized common commands, below:

2.1.3 Unix (Linux, Mac OS X, ...)

There are many flavors of Unix. Mac OS X and Linux are two popular ones. If you're using Unix, there is a good chance you don't need any help :-)

Unix commands:

2.2 Editing Java Programs

Inside a command window, you can run Java programs but your system must be set up to know where the programs reside. The primary Java commands that you need (java ,javac) are located in the directory that contains the JDK programs.

2.2.1 Interactive Development Environments

For many programmers, an IDE makes programming much easier. A good free IDE that works on many platforms is Eclipse. You can do almost all program development tasks within Eclipse, from editing to debugging to running your programs. It gives rapid feedback on a variety of simple programming errors, and provides convenient support looking up library functionality. You may also be familiar with DrJava from taking CS 100.

2.2.1 ASCII Text

ASCII text, or plain text (or even just text) is a universal format for characters that you type. When you use a proprietary editor, like Word or OpenOffice, those program convert your characters into a relatively indecipherable format. But when you use a text editor or save your work to text, your file can be viewed by pretty every editor. Plain text abounds: the format used for writing HTML, programs, e-mail, and characters you type at the command line. An IDE like Eclipse actually stores your code as ASCII text. If you want to dig a bit further, you need to know about bits and bytes. For more information, try the following links:

To test a file to determine if it is ASCII, try viewing it with a command like type (Windows) or more (Unix, Mac), loading it into a webpage, using an IDE's built-in editor.

2.2.2 Common Editors

Note that you can use the text editors that are part of most IDEs.

2.2.3 Other Text Editors

Some commonly used text editors are as follows:

2.3 Compiling and Running Java Programs

There are two directory paths (the list of directories from the top to the current directory) that you need to keep track of:

2.4 Path and Classpath Variables

To find your Java files, your operating system needs to "know" which Java files to compile and run. For example, if you do not want to bother setting the Microsoft Windows PATH variable, you might have to enter something like the following command-line to compile a Java program called MyProgram:

To run a compiled program, you issue this command:

Irritating, isn't it? See below for salvation!

2.4.1 Microsoft Windows

I recommend that you permanently set the PATH variable for all command windows:

When you want to run Java commands on specific files, you have the same issue: The operating system needs to know which files you want to act upon. You can either type the path name for each file or use Java's CLASSPATH variable.

To temporarily set the path variables, use this: set classpath=. or set class path=%classpath%;. (include the .)

For more information, see Sun's Installation Guide and Additional Help.

2.4.2 Unix

Some minor help:

See Sun's help for more information.

2.5 Compiling A Java Program From The Command-Line

Assuming you have set you system's paths as described in Section 2.4 above, you have very little work to compile a Java program!

Assuming you have programmed perfectly, your current directory will now contain .class for all classes in the Java files in that directory.

2.6 Executing A Java Program From The Command-Line

Again, assuming you have set you system's paths as described in Section 2.4 above, you have very little work to run a Java program!

3. Command-Line Arguments

3.1 Definition

Command-line arguments are strings that you supply to the main method from the command-line. How does do they work? Since main is a method that you activate at the prompt, you (and the operating system) are effectively another part of a larger program that is calling a particular method. Since all methods can take arguments, why can't main? So, whatever you type after java MyProgram at the command-line is entered in the arguments of main.

3.2 Applications

Command-line arguments have great importatn when you want various applications to "communicate" with each other when developing larger tools. When programming in C and doing Unix development, communicating at the command-line is crucial. Moreover, having the ability to enter data directly into main makes running larger numbers of the same program vastly easier for grading!

3.3 Syntax

Method main expects the user to enter zero to many strings. Here are some rules:

Since command-line arguements are often meant to be values other than strings, you will need to use methods, like Integer.parseInt(String) and Double.parseDouble(String). For example, if the user enters java MyProgram 1 to particular program, inside MyProgram's main method the programmer, could write Integer.parseInt(args[0]) to convert the String "1" to an integer 1.

3.4 Example

Compile the following program:

   public class TestArgs {
      public static void main(String[] args) { // could use any name for args!
         for (int i=0; i < args.length; i++)
            System.out.println(args[i]);
         }
      }
   }

Run the program at the command-line as follows:

  • java TestArgs: no output.
  • java TestArgs a 1 outputs a and 1 on new lines.
  • To Be Continued....

    Javadoc, Jar files, User I/O, File I/O, ....