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.
You can write Java programs in two general ways:
main
method
of a given class. A Java application is typically composed of many classes.An application that you write is usually either a single text file
(see also Section 2.2.1) or collection of text files. You can also use Jar files,
but for now, just focus on code that you write. Each file that you write will contain Java code and, thus, have the extension
import
and package
statements) must be written inside a Java class.public
.
All other classes must not have public
in their headers. (Some IDEs allow you to break this rule, but since we use the JDK command line to
compile your code, you must not break this rule!public
class called Blah
. No other classes in that file should be public
.public class Thing { } |
class C { } |
To be an application, at least one Java class inside a Java file must contain a main
method.
The main
method has this header:
public static void main(String[] args)Which class should contain
main
? Use the public
class from one of the main
method that starts your program as
the Main Class. When you run a program, Java starts with the main
method from the Main Class.
public class Test {} public static void main(String[] args) { Thing t = new Thing(); t.print("Hello,"); t.print(" world!"); t.print("\n"); } } class Thing { public void print(String s) { System.out.print(s); } } |
We explain how to specify a Main Class at the command line in Sections 2.5 and 2.6. If you would like to get a head start,
(and are willing to check if your PATH
and CLASSPATH
are set), try the following commands:
javac Test.java
(compile the program)java Test
(run the program)Why bother with a Main Class? Java allows each class in a program to have its own main
method. So, you can actually
change which class to use to start your program.
What do the public static void
modifiers mean?
public
: allow an outside source/user to call the method.void
: the main
method returns nothing.static
: the outside user/source doesn't create an object of the Main Class.
However, the code inside the class can create an object of that class. For example,
public class MyThing { public static void main(String[] args) { MyThing t = new MyThing(); t.something(); } private void something() { System.out.println("Hi!"); } }
The advice herein is really just a snippet of Sun's instructions at http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html:
Accessing command windows:
cmd
.Using MSDOS: for a full set of DOS commands, enter help
at the DOS prompt. I've summarized common commands, below:
C:
: change to a particular drive.dir
: list files in a directory. Try dir | more
to page through long listings.tree
: show directory tree for current directory.cd
: change directories.~1
. For example, to access Program Files
on the C
drive,
C:\cd progra~1
.cd ..
.cd C:\docume~1\dis\desktop
.type
: view contents of a file. For example, type Thing.java
.edit
: create/edit/save a text file.copy
: copy a file. For example, copy Thing.java ThingCopy.java
.ren
: rename a file. For example, ren ThingOld.java ThingNew.java
.del
: delete a file. For example, del Thing.java
.We need more info here. For now, see Sun's tutorial.
Unix windows:
Unix commands:
Inside a command window, you can enter all of Java command along with operating systems commands,
but your system must be set up to know where the commands reside. The primary Java commands that you need
(java
,javac
) are located in the directory that contains the JDK programs.
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. 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.
edit
. If you do not get line
breaks or you a lot of mysterious "boxes." try another editor. A nifty trick is loading the document into a web browser and then
saving back to ASCII.emacs
. Enough said. Some people prefer vi
.Note that you can use the text editors that are part of most IDEs.
Some commonly used text editors are as follows:
There are two directory paths (the list of directories from the top to the current directory) that you need to keep track of:
javac
and java
:
javac
: This program compiles your Java programs (.class
extensions) that contain Java bytecodes. In Microsoft Windows, this program is called java
: This program runs your compiled Java classes. In Microsoft Windows, this program is called 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
:
C:\Program Files\Java\jdk1.5.0_01\bin\javac
C:\docume~1\dis\desktop\MyProgram.java
To run a compiled program, you issue this command:
C:\Program Files\Java\jdk1.5.0_01\bin\java
C:\docume~1\dis\desktop\MyProgram
Irritating, isn't it? See below for salvation!
I recommend that you permanently set the PATH variable for all command windows:
;
and then the path to your Java software, followed by the word bin
.
For example, on my system I have entered the following:
;C:\Program Files\Java\jdk1.5.0_01\bin\Note the semicolon in front of this path to separate it from the previous individual path in the list.
path
at the prompt in a command window.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.
Some minor help:
javac
is /usr/j2se/bin/javac
.java
is /usr/j2se/bin/java
.See Sun's help for more information.
need someone to fill this in....
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!
javac
MyProgram.java
.javac *.java
.Assuming you have programmed perfectly, your current directory will now contain .class
for
all classes in the Java files in that directory.
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!
java
MyProgram, where MyProgram is the name of Main Class:main
that you want to run.java
MyProgram.java
or
java
MyProgram.class
.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
.
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!
Method main
expects the user to enter zero to many strings. Here are some rules:
args
could be any legal Java name.java
and Main Class MyProgram are not
included as command-line arguments unlike some other languages.args
is an array of length zero.args
from left to right,
starting at index 0 in the args
array.”
) must be quoted with a backslash (\
).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
.
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.Javadoc, Jar files, User I/O, File I/O, Mac, ....