Java on a Windows Machine
- You can download the JDKs (Java 2) from Sun and use either Notepad or a Java Editor to edit and compile your
code.
- Download the following two platforms:
- Install both platforms - You can use the default directories or you can create a container directory "C:\Java" and then
install the Standard Java in "C:\Java\j2sdk1.4.0_01" and the Enterprise Java
in "C:\Java\j2sdkee1.3.1"
- It is important to place the JDK in a location with no spaces in the
pathname (the JDK is a DOS bases system and does not recognize spaces as
valid characters for file naming)
Using Java Editors
- There are a number of editors available on the WWW that can be used
with the standard Sun JDK.
- The advantages of using these programs is that they:
- color code the different code elements & comments
- allow you to compile & run directly from their program window
without opening a DOS shell
- provided some management tools to alloy you to organize multiple
files and classes
- One free JDK editor is called JCreator LE and is available at
http://www.jcreator.com (Note:
download the free version not the one that costs money).
- Once you download and install JCreator you need to set a few parameters in
the Configure | Options menu selection to allow it to automatically
compile and run you code.
- Click JDK Profiles in the text area on the left of the Options window.
- Click the New... button to add a new JDK profile (to tell it what
version of Java you are using and where to find the built-in class files).
- Browse to the directory where you installed the Sun JDK (e.g.
C:\Java\j2sdk1.4.0_01) and select it.
- It will generate a list of .jar files that contain the default Sun
classes. These should not need to be edited. Click OK.
- To use the classes from the enterprise edition (later in the semester)
you will need to add them to the profile. Highlight the default
profile and click "Edit".
- Choose "Add" and browse to the directory where you installed the
Enterprise Edition (e.g. C:\Java\j2sdkee1.3.1) and browse to
the lib folder. Highlight all the .jar files
in the folder and click "OK".
- Click JDK Tools in the text area on the left of the Options window.
- Select "Compiler" from the "Select Tool Type" drop down list.
- Click the New... button to add new a JDK compiler (to tell it what
programs to use to compile your code).
- Give the compiler a name (javac). Then click on the [...]
to the right of the Command text box to browse to the directory where your
compiler is found (e.g. C:\Java\j2sdk1.4.0_01\bin) and select the file
javac.exe.
- The "Tool Configuration: Compiler" window has a number of check boxes
that can be checked. I usually check: "Save all documents
first" and "Capture Output". Click OK.
- Select "Run Application" from the "Select Tool Type" drop down list.
- Click the New... button to add new a JDK Tool that runs Java
applications (to tell it what programs to use to run your code).
- Give the compiler a name (java). Then click on the [...]
to the right of the Command text box to browse to the directory where your
compiler is found (e.g. C:\Java\j2sdk1.4.0_01\bin) and select the file
java.exe.
- The "Tool Configuration: Run Application" window has a number of check
boxes that can be checked. You do not need to check any of them.
Click OK.
- Click Apply then OK to close the Options window.
- Once you have set-up JCreator to compile & run Java programs you can use
it to create Java applications.
- For this class I recommend you either
- create individual files and do not create a Workspace or a Project. (the
files would be listed as External files)
- create a single workspace and add projects to that workspace. The
project should be created as "Empty Projects" so JCreator does not generate
code for them. Add or create new .java files within a given project.
- Creating a program:
- Choose New from the File menu and type in a short program (e.g. one that
outputs a single line to the screen)
public class Test
{ public static void main(String argv[])
{ System.out.println("This is a test.");
}
}
- Save the program in a file with the same name as the class (Test.java).
Remember to put it in a directory without any spaces in the path (e.g. C:\Java\mycode)
- To compile Test.java using JCreator, chose
"Compile File" from the Build menu or click "Compile File" on the toolbar.
- To run Test.class using JCreator, make sure you are looking at the code
for Test.class in the JCreator editor pane and chose "Execute File" from the
Build menu or click "Execute File" on the toolbar.
Using Notepad and DOS Compilation
- You can choose not to download a Java Editor but then you need to compile
and run you code at the command line.
- Creating a program:
- Start a text editor by choosing "Start", "Programs",
"Accessories", and choosing WordPad or NotePad.
- Choose New from the File menu and type in a short program (e.g. one that
outputs a single line to the screen)
public class Test
{ public static void main(String argv[])
{ System.out.println("This is a test.");
}
}
- Save the program in a file with the same name as the class (Test.java).
Remember to put it in a directory without any spaces in the path (e.g. C:\Java\mycode)
- Compiling the code: None of the Java tools are Windows programs with GUI interfaces -- they are all run
from the DOS command line.
- go to a DOS shell by selecting Run from the Start Menu and typing cmd
- go to the directory containing your Test.java (cd jdk1.3.1\mycode)
- compile the code by typing:
C:\Java\j2sdk1.4.0_01\bin\javac Test.java
Where C:\Java\j2sdk1.4.0_01 is the directory you downloaded the Sun
JDK to,
bin is the directory the Java programs are located in, and
javac.exe is the Java compiler provided by Sun.
- Running the code: When the code compiles a Test.class file is
created. It is run using the following command:
C:\Java\j2sdk1.4.0_01\bin\java Test
- Where C:\Java\j2sdk1.4.0_01 is the directory you downloaded the Sun JDK
to,
bin is the directory the Java programs are located in, and
java.exe is the Java application interpreter provided by Sun.
-
- When running the javac and java programs in the DOS shell it is cumbersome
to always have to type the path before the command (C:\Java\j2sdk1.4.0_01\bin\javac).
- You can avoid typing the path if you set the Path environment variable to
point to the bin directory in the jdk folder.
- On a Windows 95/98/Me System you set Path by:
- Start a text editor by choosing "Start", "Programs",
"Accessories", and choosing WordPad or NotePad.
- Choose Open from the File menu and type "c:\autoexec.bat" for the filename
This will open the file for editing.
- Look for the PATH statement. Notice that the PATH statement is a series of
directories separated by semi-colons (;). (add C:\Java\j2sdk1.4.0_01\BIN
to the path:
- If there is no path or the path is blank:
PATH ="C:\Java\j2sdk1.4.0_01\BIN"
- If there are already values for the path add C:\Java\j2sdk1.4.0_01\bin to the end of the path
line (after a ;):
PATH ="C:\WINDOWS;C:\;C:\DOS;C:\Java\j2sdk1.4.0_01\BIN"
- To make the path take effect, execute the autoexec.bat (or reboot)
- On a Windows 2000 or NT System you set Path by:
- Edit the System Environment variables by choosing "Start",
"Settings", "Control Panel".
- Choose "System" in the "Control Panel" window.
- Select the "Advanced" tab
in the "System" window and Click the "Environment Variables..." button.
-
Click the New button to create a new environment variable.
- Below the System Variables Text area you will:
- If there is no path, click New...:
Variable:
PATH
Variable Value: C:\Java\j2sdk1.4.0_01\BIN
- If there are already values for the path, highlight the path line and
click Edit...
add
c:\jdk1.3.1\bin to the end of the
line in the Variable Value text box (after a ;):
C:\WINDOWS;C:\;C:\DOS;C:\Java\j2sdk1.4.0_01\BIN
- The path takes effect immediately
- To run the JDK tools after setting the Path just type:
javac Test.java
and
java Test
Updated June 27, 2002
by Dawn Gregg,