creating a source file
java has roughly two kinds of files: source files - these end in .java and look like regular text files, ie, they are human readable. after they go through a process known as compilation, they take a .class ending and are now machine-readable (and no longer human-readable).
so let's create a simple source file. this file should do nothing but print out:
"I am a Java God! Watch out and tremble!
on command line
so, grab your favorite textpad and copy-paste (or type if you must) in the following:
public class JavaGod {
public static void main(String[] args) {
System.out.println("I am a Java God! Watch out and tremble!");
}
}
save this in a file called "JavaGod.java" in your favorite directory, in my case
c:\source
to verify that is there, run the the command 'dir'. you should see hopefully something as follows:
c:\source>dir
Volume in drive c has no label.
Volume Serial Number is 3028-A828
....
... JavaGod.java
....
it's there. wow! i'm good. but i always knew that.
compiling your first java programon command line, let's invoke the java compile known us - surprisingly - javac as follows:
c:\source>dir
expect no response. if you get some error messages, it means that your source code contains some typos. go back to step
creating a source file
above
running your first java programc:\source>java JavaGod
if you get the following error message - like i did:
c:\source>java JavaGod
Exception in thread "main" java.lang.NoClassDefFoundError: JavaGod
this means that an environment variable called CLASSPATH is not set. repeat the steps we did for path in previous chapter and now append or add the following to your existing CLASSPATH environment variable the following two characters:
;.
why? apparently, the period tells the java to look for java programs in the current directory as well. yes, i know, it should be obivious but we are dealing with a machine here after all.
now, open a new command window - and remember to open a new one - your system does not know about your new environment variable changes otherwise and try again:
c:\source>java JavaGod
c:\source>java JavaGod
I am a Java God! Watch out and tremble!
yes! i'm going to put up my resume. see you. no, wait, my friends who are java gods are telling me that will not take me very far. so sit still and read on