CS 281
Advanced Programming


Stonehill College             CS 281

barmove.gif (33725 bytes)

The Java compiler

Your Java programs will usually consist of several different files. (Unike C++, each public class must be stored in a different file.)  You might build a directory structure to organize your files as follows:

1. Make a parent folder, javaprograms, for all your programs.  Of course you can name this anything you like.

2. Put each different program into a separate folder.  So you will have subfolders prog1a, prog1b, prog2a....

 

 

How to compile and run a java program

If you use Notepad:

  1. Save your program with a .java extension.  For example junk.java
    Remember the name of the file must be the same as the class which you are saving. Also, Java is case sensitive.
    Also, to prevent Notepad from adding a txt extension place the name of your file in quotes:


  2. Open a DOS window and navigate to the folder where you have saved your program.  For example if you have saved your program in c:\javaprograms\prog1 you may have to issue  commands like:
                      >  c:
                      > cd \
                      >  cd  \javaprograms\prog1
  3. To compile the program you must run the java compiler.  Enter the command javac programname.java.
     
    For example:
                    javac junk.java
  4. If you get no error messages you can run or  execute your program (bytecode) with the command java programname (Do not include an extension)
                       java junk

If you use Textpad :

You will issue the same commands  (javac and java) but within the Texpad environment.  You will not open a DOS window.

  1. Save your program.  If Textpad has been set up correctly the .java extension will be the default and you do not need to include the extension in the name.

  2. To compile the program select Run from the tools menu.  Fill in the dialog boxes as shown below.  Here the file saved is junk.java.


  3. If your program compiles without errors you can run it.
    To run a program, again choose tools-->Run.  Notice, however, that the appropriate command is java NOT javac and the file is simply junk NOT junk.java.


  4. The "Compile Java" or "Run Java Application" commands also available and are simpler for simple programs.  For more complex programs spread across many files and/or directories, it is often easier to use the Run command.