Setting Up Eclipse With Java 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Setting up Eclipse with Java 1.

6 on Windows

http://www.ugrad.cs.ubc.ca/~cs211/tutorials/Eclipse/Eclipse_Java_Wi...

Setting Up Eclipse with Java 1.6 on Windows


Step
Step
Step
Step
Step
Step
Step

1
2
3
4
5
6
7

Download Java
Install Java
Set the PATH
Set the CLASSPATH
Test the Java installation
Download and install Eclipse
Test the Eclipse installation

Step 1

Download Java 1.6 package for Windows.


Click the Download button alongside the label JDK 6 Update 2 (or similar). On that page, accept the license
agreement near the top of the page and download either the online or offline Windows Platform installation file. The
offline option is a big file (c. 66MB) which includes all of the Java language and can be used to install even when an
internet connection is not available. The online option is a small file (c. 0.4MB) that when run downloads Java over an
internet connection as installation proceeds. It is recommended that you save the installer file to the Desktop so that
it can be found easily.

Step 2 Install the Java 1.6 package.


a. Double-click the file that you just downloaded. The name should be something similar to jdk-6u2-windowsi586-p.exe or, if you chose the online installation option, jdk-6u2-windows-i586-p-iftw.exe
b. Accept the License Agreement.
c. Accept all the default choices. In other words, just keep clicking "Next".
d. Wait for the installation to complete.

Step 3 Update the PATH environment variable.


(The following is adapted from Sun's Installation Notes for JDK 1.6 Microsoft Windows)
You can run the JDK without setting the PATH variable, or you can optionally set it as a convenience. To save yourself
a lot of careful typing, it is highly recommended that you set the path variable.
Set the PATH variable if you want to be able to conveniently run the JDK executables (javac.exe, java.exe,
javadoc.exe, etc.) from any directory without having to type the full path of the command. If you don't set the PATH
variable, you need to specify the full path to the executable every time you run it, such as:
C:> "\Program Files\Java\jdk1.6.0_<version>\bin\javac" MyClass.java

It's useful to set the PATH permanently so it will persist after rebooting.
To set the PATH permanently, add the full path of the jdk1.6.0_<version>\bin directory to the PATH variable.
Typically this full path looks something like C:\Program Files\Java\jdk1.6.0_<version>\bin, where
<version> is a two-digit number. Set the PATH as follows, according to whether you are on Microsoft Windows NT,
XP, 98, 2000, or ME.
Microsoft Windows NT, 2000, and XP - To set the PATH permanently:
a. Choose Start, Settings, Control Panel, and double-click System. On Microsoft Windows NT, select the
Environment tab; on Microsoft Windows XP and 2000 select the Advanced tab and then Environment Variables.
Look for "Path" in the User Variables and System Variables. If you're not sure where to add the path, add it to
the right end of the "Path" in the User Variables. A typical value for PATH is:
C:\Program Files\Java\jdk1.6.0_<version>\bin
Where <version> is the latest version number. For example, if you downloaded jdk1.6.0_02, then the value
to add to the path is:
C:\Program Files\Java\jdk1.6.0_02\bin
Capitalization doesn't matter. Click "Set", "OK"
or "Apply".
The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows looks for programs in
the PATH directories in order, from left to right. You should only have one bin directory for a JDK in the path at

1 of 6

1/30/2015 12:46 AM

Setting up Eclipse with Java 1.6 on Windows

http://www.ugrad.cs.ubc.ca/~cs211/tutorials/Eclipse/Eclipse_Java_Wi...

a time (those following the first are ignored), so if one is already present, you can update it to
jdk1.6.0_<version>\bin.
b. The new path takes effect in each new Command Prompt window you open after setting the PATH variable.
Microsoft Windows 98 - To set the PATH permanently, open the AUTOEXEC.BAT file and add or change the PATH
statement as follows:
a. Start the system editor. Choose "Start", "Run" and enter sysedit, then click OK. The system editor starts up
with several windows showing. Go to the window that is displaying AUTOEXEC.BAT
b. Look for the PATH statement. (If you don't have one, add one.) If you're not sure where to add the path, add it
to the right end of the PATH. For example, in the following PATH statement, we have added the bin directory at
the right end:
PATH C:\WINDOWS;C:\WINDOWS\COMMAND;"C:\PROGRAM FILES\JAVA\JDK1.6.0_<version>\BIN"
Capitalization doesn't matter. The PATH can be a series of directories separated by semi-colons (;). Microsoft
Windows searches for programs in the PATH directories in order, from left to right. You should only have one
bin directory for a JDK in the path at a time (those following the first are ignored), so if one is already present,
you can update it to jdk1.6.0_<version>.
c. To make the path take effect in the current Command Prompt window, execute the following:
C:> c:\autoexec.bat

To find out the current value of your PATH, to see if it took effect, at the command prompt, type:
C:> path

Microsoft Windows ME - To set the PATH permanently:


From the start menu, choose programs, accessories, system tools, and system information. This brings
up a window titled "Microsoft Help and Support". From here, choose the tools menu, then select the
system configuration utility. Click the environment tab, select PATH and press the edit button. Now add
the JDK to your path as described in step b above. After you've added the location of the JDK to your
PATH, save the changes and reboot your machine when prompted.

Step 4 Set the CLASSPATH environment variable.


The CLASSPATH variable tells Java where to look for *.class bytecode files. Bytecode is created when you compile
your *.java files. Set the CLASSPATH as follows, according to whether you are on Microsoft Windows NT, XP, 98,
2000, or ME.
Microsoft Windows NT, 2000, and XP - To set the CLASSPATH:
a. Choose Start, Settings, Control Panel, and double-click System. On Microsoft Windows NT, select the
Environment tab; on Microsoft Windows 2000 or XP select the Advanced tab and then Environment Variables.
Look for "classpath" in the User Variables and System Variables, then click Edit. If no classpath variable exists,
create it by clicking New and adding "classpath" to the User Variables. A typical value for CLASSPATH is:
.;..
The '.' represents your current directory, allowing you to compile and run code in your current directory (as
shown in the command line of your command window). The '..' represents the folder above the current
directory. Together, the '.' and '..' cover most situations where you need to compile and run programs. The
semi-colon ';' separates these entries. The CLASSPATH can contain multiple directories separated by
semi-colons.
Click "Set", "OK" or "Apply".

b. The new classpath takes effect in each new Command Prompt window you open after setting the CLASSPATH
variable.

Microsoft Windows 98 - To set the CLASSPATH permanently, open the AUTOEXEC.BAT file and add or change
the CLASSPATH statement as follows:

a. Start the system editor. Choose "Start", "Run" and enter sysedit, then click OK. The system editor starts up

2 of 6

1/30/2015 12:46 AM

Setting up Eclipse with Java 1.6 on Windows

http://www.ugrad.cs.ubc.ca/~cs211/tutorials/Eclipse/Eclipse_Java_Wi...

with several windows showing. Go to the window that is displaying AUTOEXEC.BAT


b. Look for the CLASSPATH statement. (If you don't have one, add one.) The classpath should contain a '.' and a
'..'. If you're not sure where to add them, put them at the right end of the CLASSPATH. For example, in the
following CLASSPATH statement, we have added the '.' and '..' directories at the right end:
CLASSPATH C:\someFolder\someClasses.jar;..;.
The '.' represents your current directory, allowing you to compile and run code in your current directory (as
shown in the command line of your command window). The '..' represents the folder above the current
directory. Together, the '.' and '..' cover most situations where you need to compile and run programs. The
semi-colon ';' separates these entries. The CLASSPATH can contain multiple directories separated by
semi-colons.

c. To make the classpath take effect in the current Command Prompt window, execute the following:
C:> c:\autoexec.bat
To find out the current value of your CLASSPATH, to see if it took effect, at the command prompt, type:
C:> classpath

Microsoft Windows ME - To set the CLASSPATH permanently:


From the start menu, choose programs, accessories, system tools, and system information. This brings
up a window titled "Microsoft Help and Support". From here, choose the tools menu, then select the
system configuration utility. Click the environment tab, select CLASSPATH and press the edit button.
Now add '.' and '..' to your path as described in step b above. Then save the changes and reboot your
machine when prompted.

Step 5

Test your Java installation.

a. Open a command window and create a test directory.


1.
2.
3.
4.
5.

Go to Start >> Run... then type cmd and click OK.


Type cd \ and press Enter.
The bottom line should read C:\>
Type mkdir test and press Enter to create a new directory for your testing.
Type cd testand press Enter to enter the directory. The bottom line of the command window should read
C:\test>

b. Open a text editor and create a small Java program.


Open WordPad or NotePad (in XP these can be found at Start >> All Programs >> Accessories) and copy in the
following simple program:
public class MyTestApplication {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

c. Save the program in the test directory.


In the text editor menu, choose File >> Save. Then in the File Name field near the bottom of the dialog box, type
the following exactly C:\test\MyTestApplication.java
and click Save.

d. Confirm the save.


In your command window, type dir. You should see something similar to C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 3C4D-5998

3 of 6

1/30/2015 12:46 AM

Setting up Eclipse with Java 1.6 on Windows

http://www.ugrad.cs.ubc.ca/~cs211/tutorials/Eclipse/Eclipse_Java_Wi...

Directory of C:\test
29/01/2006
29/01/2006
29/01/2006

08:20 PM
<DIR>
.
08:20 PM
<DIR>
..
08:20 PM
129 MyTestApplication.java
1 File(s)
129 bytes
2 Dir(s) 59,076,812,800 bytes free

C:\test>

e. Compile the program.


Type javac MyTestApplication.java and press Enter. You should see C:\test>javac MyTestApplication.java
C:\test>
There are two problems that you may encounter at this step.
Problem 1
If instead you get the following message C:\test>javac MyTestApplication.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
C:\test>
then Java has not been correctly installed. Perform the following tests :
1) Check that Java has been correctly downloaded and installed. In particular, look for the directory
C:\Program Files\Java\jdk1.6.0_<version>\bin. If you cannot find a Java folder in the Program Files
folder, then go back to Step 1 now.
2) Check that the PATH variable has been set correctly. Type echo %path%. You should see something
similar to C:\test>echo %path%
C:\Perl\bin\;c:\programfiles\imagemagick;C:\texmf\miktex\bin;C:\WINDOWS\system32
;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Contr
ol Panel;C:\Program Files\SecureCRT\;C:\Program Files\Java\jdk1.6.0_02\bin
C:\test>
If you do not see an entry anywhere in your PATH similar to the one above in bold, then go back to Step 3
now.

Problem 2
If instead you get the following message C:\test>javac MyTestApplication.java
error: cannot read: MyTestApplication.java
1 error
C:\test>
then either the CLASSPATH variable is not set or your file is not in the test folder. Perform the following tests:
1) Check that the CLASSPATH variable has been set correctly. Type echo %classpath%. You should see
something similar to C:\test>echo %classpath%
.;..;C:\someFolder\someClasses.jar
C:\test>
Note that the classpath may have been automatically updated to contain the location of various class files, and
therefore may be longer than the example above. Just make sure that it contains a '.' with a ';' to separate it
from other entries. Go back to Step 4 above to edit the classpath.
2) Make sure you are in the test folder. If you are not, then use the cd command to navigate to the test
folder. Once you are in the test folder, type dir at the command line to examine its contents. If you do not
see an entry for MyTestApplication.java then Go back to Step 5b above.

4 of 6

1/30/2015 12:46 AM

Setting up Eclipse with Java 1.6 on Windows

http://www.ugrad.cs.ubc.ca/~cs211/tutorials/Eclipse/Eclipse_Java_Wi...

f. Confirm the compilation.


Type dir. You should see something similar to C:\test>dir
Volume in drive C has no label.
Volume Serial Number is 3C4D-5998
Directory of C:\test
29/01/2006
29/01/2006
29/01/2006
29/01/2006

08:28 PM
<DIR>
.
08:28 PM
<DIR>
..
08:28 PM
440 MyTestApplication.class
08:20 PM
129 MyTestApplication.java
2 File(s)
569 bytes
2 Dir(s) 59,076,796,416 bytes free

C:\test>
If compilation was successful, a *.class file should be present as shown.

g. Run the program.


Type java MyTestApplication to run the program. You should see C:\test>java MyTestApplication
Hello World!
C:\test>
There is one common problem at this step.
Problem
If you get the following message C:\>java MyTestApplication
Exception in thread "main" java.lang.NoClassDefFoundError: MyTestApplication
C:\>
then either your class file is not in the test folder (type dir at the command line to make sure), or you are in
the wrong directory as in this example, where the java command has been typed in C: rather than C:\test.
Go back to Step 5b above and make sure that you have performed all steps correctly.

Step 6

Download and install Eclipse.

Download Eclipse from the Eclipse downloads page.


Click the link next to "Download now".
If possible, choose a mirror close to your location (e.g. Portland, if you are in Vancouver).
Wait for your download to finish. This may take some time: the installer is a large file (>100MB).
Unzip the downloaded file to C:\Program Files. Again, this may take some time.
In C:\Program Files\eclipse, right-click on eclipse.exe and select Create Shortcut. Rename the shortcut
to eclipse and drag it to your Desktop. Then close the eclipse folder.
7. On the Desktop, double-click on the eclipse shortcut to start the program.
8. A Workspace Launcher dialog will appear. Select "Use this as the default and do not ask again" and click OK.
9. Eclipse will start up. Either explore the Overview, Tutorials, etc. or close the Welcome page by clicking on the
white X to reveal the Java perspective, where you will do your programming.

1.
2.
3.
4.
5.
6.

Now set up Eclipse to use the latest version of Java for compiling and running your Java programs.
(Note: you need to configure Eclipse to use Java 1.6 or it will use an older Java version by default)
1. Set the default installed JRE to Java 1.6 (this tells Eclipse which libraries to use):
Open the Eclipse Preferences Pane (Window -> Preferences) and go to the Java >> Installed JREs
subpane.
Check the JRE 1.6.0_<version> as the default (where <version> is a two digit number representing the
version number that you downloaded and installed)
Click OK to save the settings.
2. Set the Java compiler to Java 1.6 (this tells Eclipse which compiler to use):
In Eclipse Preferences Pane go to Java >> Compiler >> Compliance Level
Select "6.0".
Click OK to save the settings.
Eclipse should then be set up properly to compile and run Java 1.6 programs.

Step 7

5 of 6

Test the Eclipse installation.

1/30/2015 12:46 AM

Setting up Eclipse with Java 1.6 on Windows

http://www.ugrad.cs.ubc.ca/~cs211/tutorials/Eclipse/Eclipse_Java_Wi...

Create a new Java project with a class and run it.


1.
2.
3.
4.
5.
6.

In the Java Perspective, right-click in Eclipse's Package Explorer window and select New >> Project....
Select Java Project and Next.
Name the project whatever you like (e.g. 'test'). For now the other options do not matter; just click on Finish.
In the Package Explorer window, right-click on the test package and select New >> Class.
Name the class MyTestApplication. For now the other options do not matter; just click on Finish.
Delete everything in the editor that appears. Copy the following into the editor:
public class MyTestApplication {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

7. In the Eclipse menu bar, select File >> Save. If everything is working properly this program should compile
automatically without errors.
8 Run the program by right-clicking on the MyTextApplication class in the package manager, then select Run As
>> Java Application.

"Hello World!" should appear in the console output.

Please report any errors found on this page to mike@cs.ubc.ca

University of British Columbia

6 of 6

Last updated: August 31, 2007 17:21

1/30/2015 12:46 AM

You might also like