General Steps to Creating, Compiling and Running Java Programs
Applets and Applications in DOS
General Steps to Creating, Compiling and Running Java Programs
Applets and Applications in DOS
I. Applets
- Start a MS-DOS command window.
- For PCs running Windows NT (as in the Weber Lab),
choose Start/Programs/Command Prompt from the Windows NT desktop. - For PCs running Windows 95,
choose Start/Shut Down/Restart in MS-DOS Mode from the Windows 95 desktop.
- For PCs running Windows NT (as in the Weber Lab),
- Now, change to the directory of your choice from within the command window.
For example:>cd \Temp
- Create some Java source code with Notepad from within the command window.
For example, suppose we want a class named "A",
then we would start Notepad with the name of the Java file:>notepad A.java
- The source code, A.java, might look like this:
Don't forget to save the file!// This program draws a tall rectangle via the A.html applet tag. // mlc // 9/1999 import java.applet.*; import java.awt.*; public class A extends Applet { private int w, h; public void init( ) { w = 45; h = 50; } public void paint(Graphics g) { g.drawRect(w, h, 20, 80); } }
- Double check the name of the Java program file you just saved
by listing out the files in the current directory:>dir /p
- If the name of the Java program file listed does not end in .java,
you need to rename it so that it does
For instance, if the file appears as A.java.txt, type Now if you list the files in the directory, you should see it named correctly>ren A.java.txt A.java
- Now, since this is an applet, we'll need to make an HTML file.
We could call it A.html and create it from the command line using Notepad again:>notepad A.html
- The A.html file might look like this:
This file launches the 'A' applet: A.class!
- Again, be sure to save the file
Also be sure that the name of the file ends in .html (not .txt) - Assuming we saved the Java source code (as in step 4) from within Notepad,
we can now go back to the command prompt and compile it.
This is done with the javac compiler command:>javac A.java
- If everything compiles fine
(we know this by the fact we had no error messages
and the "A.class" file exists),
we are ready to run it.
Otherwise, we have to go back to Notepad and edit the A.java
until there are no more mistakes.
Then we repeat step 7 after saving the changes. - At this point (assuming we saved the HTML file in step 5),
we can run either a Java-enabled browser or
simply use the appletviewer program from the command line:>appletviewer A.html
- The above command should bring up a window using the HTML file and
then launch (or run) the Java applet from within the viewer. You should see a tall rectangle. Good work!This file launches the 'A' applet: A.class!
II. Applications
- Start a MS-DOS command window.
- For PCs running Windows NT (as in the Weber Lab),
choose Start/Programs/Command Prompt from the Windows NT desktop. - For PCs running Windows 95,
choose Start/Shut Down/Restart in MS-DOS Mode from the Windows 95 desktop.
- For PCs running Windows NT (as in the Weber Lab),
- Now, change to the directory of your choice from within the command window.
For example:>cd \Temp
- Create some Java source code with Notepad from within the command window.
For example, suppose we want a class named "Test",
then we would start Notepad with the name of the Java file:>notepad Test.java
- The source code, Test.java, might look like this:
Don't forget to save the file!public class Test { public static void main(String args[]) { System.out.println("We work!"); } }
- Double check the name of the Java program file you just saved
by listing out the files in the current directory:>dir /p
- If the name of the Java program file listed does not end in .java,
you need to rename it so that it does
For instance, if the file appears as A.java.txt, type Now if you list the files in the directory, you should see it named correctly>ren A.java.txt A.java
- Assuming we saved the Java source code (as in step 4) from within Notepad,
we can now go back to the command prompt and compile it.
This is done with the javac compiler command:>javac Test.java
- If it doesn't compile, fix the errors with Notepad.
Otherwise if it compiles fine
(we know this by the fact we had no error messages
and the "Test.class" file exists),
we can run the application (found in Test.class) using the java command
followed by the name of the class (without the extension):>java Test
- At this point you should see:
We work!
A Possible Java J2SE Problem -- No Path to Java J2SE
If the system can't find javac, java or appletviewer,you should check your path by typing "PATH" at the command prompt:
>PATH |
PATH=C:\WINNT\system32;C:\WINNT;C:\JDK1.3.1\BIN |
You should check with your systems person.
They will consult the JDK or J2SE readme.txt file
and the location of where the Java J2SE was installed
to assure the correct path.
-mlc
Opening (or Browsing for) an Existing File in Notepad
Whenever you are trying to open (or browsing for) a file that already exists in your directories,be sure that you highlight the All Files option
on the Files of type: form at the bottom of the Open/Browse window
This should permit you to see all the .java, .html, and .txt files in your directory
Don't limit your search to just Text Documents (*.txt) or HTML Files,
or you'll never find files with names ending in .java or .class
No comments:
Post a Comment