How to compile and run Java at DOS

  1. Compiling the java file
  2. Running Java Application
  3. Running Java Applet

Compiling the java file


1) You can  use any wordprocessor (text editor), as long as you can save files as TEXT format. Here I use UltraEdit. (Notepad works too, but it is not as pretty.)

Note: When you save the source file above, save it as "Example.java". When you save it, make sure you save it as text. Do not save it as a WordPad document or any other format. All text editors will allow you to save your file as text only.

Also make sure that the file you create does not have a ".txt" extension. For example, NotePad may have saved the file as "Example.java.txt". You need to make sure it is correctly called "Example.java". To avoid the ".txt" extension, simply surround the name of the file in quotation marks when you save the file.

2) To compile the file, type "javac Example.java" in this case. You must save your file as  the class name and with .java extension. After this, the Example.java file is compiled and generate a new file called Example.class.


 


Running Java Application


1) To run the program, type "java Example" (Note: not java Example.class).


Running Java Applet

1)  The procedure for compiling applets is same as applications.

2) To run the applet, you need to create a HTML file which contains the applet tag.
Example:

<html>
<body>
<applet code="HelloApplet.class" width=200 height=200></applet>
</body>
</html>

Save the HTML file.

3) Now type "appletviewer Example.html" to load  your applet



Any problems, please email to Angela.
July 2001