Create and Compile Your First Java Program
Using a Text Editor and DOS

(Free Web Tutorials & Resources)

by Michael Thomas

Java First Steps Home Page

If you have trouble with any of the steps below see the table below called "Help Notes".

Steps
(Tested with Java SE 8u111)

Step: Using a Text Editor

Pick a text editor to use:

Step 1:  DOS: Create some batch files to test JDK install

Step 2:  DOS: Create and compile your first Java Application

Step 3:  Create and compile your first Java Applet

Step 4:  Pat Your Self on the Back.  Your finished!



Help Notes
(Sorry, could be outdated! I did not want to remove.)

 

Topic Notes
Environment Variables:
  • Path
  • Classpath

 

DOS Environments:

View the DOS Environment Variables.

  • c:\> set | more -  This DOS command will show you all of the DOS environment variables and page break if neccessary.

Changing the DOS Environment Variables.

  • set CLASSPATH=.    - This sets the class path to the current directory.  Make sure you don't add extra spaces to this command!
  • set CLASSPATH=     -  This will remove the CLASSPATH variable.

DOS Variables: path & CLASSPATH

  • path - shows the director order in which DOS will look for DOS Commands.  I like to have the JDK be the first in the list if your OS allows it.  (ex:  c:\jdk1.3\bin; c:\winnt; etc...)
  • CLASSPATH - a variable that tells java which directories to look for classes.  You should have ".;" be the first in the list.  If you need to customize the CLASSPATH for a program use the -classpath compile option when compiling (ex: javac HelloWorld.java -classpath . ).

General Notes - Environment Variables:

  • User Variables Section
    Note:  Because some software programs create errors when installing, I always change the setttings for "temp" and "tmp".
    From: %USERPROFILE%\Local Settings\Temp
    To: c:\temp (make sure the directory exits)

Win XP

  • Start, Settings, Control Panel, System
  • Click the tab "Advanced", then click "Environment Variables"

WinNT (NT4.0 & NT2000)

  • Start, Settings, Control Panel, System, Environment
  • Next Click on the "System Variable" called "Path".
  • Next modify the "value" field by putting the path at the end of the current value.  (Separate the paths with a ";").
    Ex:  ....; c:\jdk1.3\bin

Win 95/98

  • Steps
    • Press Start, Run
    • type:  msconfig
    • Then hit the "Enter" key.
    • Click on the Autoexec.bat tab
    • Click on "set Path" line.  Then click "Edit"
    • Right now you are viewing the Autoexec.bat file.  Be careful while you are in this file.
    • See if you have a command that begins with
      "SET PATH =".  If you do, add the correct path as the first entry after the "SET PATH =".  Choose from the following:
      • For JDK 1.2 add:  c:\jdk1.2\bin;
      • For SDK 1.3 add:  c:\jdk1.3\bin;
      Note: If you don't have a "SET PATH" statement, add the following:
      • set path = c:\jdk1.3\bin;
    • Next, you must reboot.
    • After you reboot go to a DOS prompt and type:
      path
    • If  c:\jdk1.3\bin appears first your are completed.  If c:\windows and/or c:\windows\command is first, you maybe calling java.exe from those directories.

Win ME

  • Steps
    • Press Start, Run
    • type:  msconfig
    • Then hit the "Enter" key.
    • Click on the Environment tab
    • Click on "Path" line.  Then click "Edit"
    • Be careful and add the correct path.  Note - add the path as the first entry.
      For JDK 1.2 add:  c:\jdk1.2\bin;
      For SDK 1.3 add:  c:\jdk1.3\bin;
      Ex: c:\jdk1.3\bin; c:\windows; c:\windows\command;
    • Click on "Apply"
    • Now click on the "General" tab.  Make sure that the Normal option is selected.  If not you may get a message about "selective startup" after you reboot.
    • Click on "OK"
    • Next, you must reboot.
    • After you reboot go to a DOS prompt and type:
      path
    • If  c:\jdk1.3\bin appears first your are completed.  If c:\windows and/or c:\windows\command is first, you maybe calling java.exe from those directories.
CLASSPATH variable CLASSPATH - 
Goto a DOS prompt Ways to get to a DOS prompt:
  • First way:  Start, Programs, Command Prompt
  • Second way: Start, Run, command.com

 

DOS Commands

Common DOS Commands

Command Example Description
Steps to Create a directory for this project. DOS Command.  Type a DOS prompt.

c:
md \class
md \class\java
md \class\java\j1
cd \class\java\j1

dir *.* This shows a list of the files in a directory.
C: This takes you to the C: drive.
cd class This changes your default directory to a subdirectory called class
cd \class\java This changes your default directory to a directory called "java" which is in a folder called "class" which is in the root "\" directory of the C: drive.
md \class Makes a directory called class under the root "\" directory.
javac MyFirstApplication.java Compiles your program.
java MyFirstApplication Runs a Java Application
appletviewer MyFirstApplet.html Runs Sun's appletviewer that will display your applet in a window.  No html code is displayed.
   
   
   
  
www.easydos.com
- Helpful info on DOS commands.
Dos Filenames DOS (some OS versions)
  • dir /x  - shows 8char short file names.  Some versions of DOS require short filenames when you CD or give pathnames.  This command is handy in those cases.  May not work for all OS's.
 
Errors Common Errors & Solutions
  • Error opening registry key 'Software\JavaSoft\Java Runtime Environment' when calling:  java <program name>
    • Quick fix - give the full path for java:  ex: c:\jdk1.3\bin\java
    • Reinstall the Java Runtime Environment (JRE)