Вы находитесь на странице: 1из 9

1.

Downloading and installing JDK software


In order to write and run a Java program, you need to install a software program called Java SE
Development Kit (or JDK for short, and SE means Standard Edition). Basically, a JDK contains:

o JRE(Java Runtime Environment): is the core of the Java platform that enables running Java
programs on your computer. The JRE includes JVM(Java Virtual Machine) that runs Java programs
by translating from bytecode to platform-dependent code and executes them (Java programs are
compiled into an intermediate form called bytecode), and other core libraries such as collections,
File I/O, networking, etc.
o Tools and libraries that support Java development.

The JDK ships with two powerful tools which every Java developer should be familiar with:

o javac.exe: is Java compiler that translates programs written in Java code into bytecode form.
o java.exe: is the Java Virtual Machine launcher that executes bytecode.

http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

Check the option “Accept License Agreement”, and choose an appropriate version for your computer from
the list.

After downloading the program, run it to install the JDK on your computer (just following the steps, leave
the defaults and click Next, Next…):

1
You would see the JDK is installed in the following directory, for example: C:\Program
Files\Java\jdk1.7.0_21. The following screenshot describes the JDK’s directory structure:

Now let’s test if Java runtime is installed correctly. Open a command prompt window and type:

java –version

You would see the following result:

That shows version of the JRE, e.g. “1.7.0_21” - Congratulations! Your computer is now
capable of running Java programs.

Now try to type the following command:

javac -version

You would see the following error:

That’s because Windows could not find the javac.exe program, so we need to set some environment
variables which tell the location of javac.exe.

2
2. Setting up environment variables

Now we’re going to set environment variables so that the javac.exe program can be accessed anywhere
from command line. On Windows 7, go to My Computer and click System Properties:

Then click Advanced system settings:

The System Properties dialog appears, select Advanced tab and click Environment Variables...:

3
The Environment Variable dialog appears, click on the New… button under the System variables section.

That opens up the New System Variable dialog. Type the following information:

The field Variable name must be JAVA_HOME, and the field Variable value must point to JDK’s installation
directory on your computer. Here it is set to c:\Program Files\Java\jdk1.7.0_21. Click OK to close this
dialog.

Now back to the Environment Variables dialog, look for a variable called Path under the System Variables
list, and click Edit…:

4
In the Edit System Variable dialog, append the following to the end of the field Variable value:

;%JAVA_HOME%\bin

Note that there is a semicolon at the beginning to separate this value from other ones. Click OK three times
to close all the dialogs.

Now we have to quit the current command prompt and open a new one to see the changes takes effect.
Type the following command again in the re-opened command prompt window:

javac -version

You would see the following output:

3. Writing a Java hello world program

Open a simple text editor program such as Notepad and type the following content:

1. public class HelloWorld {


2. public static void main(String[] args) {
3. System.out.println("Hello world!");
4. }
5. }

Save the file as HelloWorld.java (note that the extension is .java) under a directory, let’s say, C:\Java.

Don’t worry if you don’t understand everything in this simple Java code. The following picture explains it nicely:

5
Every Java program starts from the main() method. This program simply prints “Hello world” to screen

4. Compiling it

Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the following command
to change the current directory to the one where the source file is stored:

cd C:\Java

And type the following command:

javac HelloWorld.java

That invokes the Java compiler to compile code in the HelloWorld.java file into bytecode. Note that the file
name ends with .java extension. You would see the following output:

If everything is fine (e.g. no error), the Java compiler quits silently, no fuss. After compiling, it generates the
HelloWorld.class file which is bytecode form of the HelloWorld.java file. Now try to type dir in the command
line, we’ll see the .class file:

So remember a Java program will be compiled into bytecode form (.class file).

5. Running it

It’s now ready to run our first Java program. Type the following command:

java HelloWorld

That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is no .java or
.class extension). You would see the following output:

It just prints out “Hello world!” to the screen and quits. Congratulations! You have successfully run your first
Java program!

6
6. What we have learnt so far

 Throughout this tutorial you have learnt the following things:


 JDK is the Java SE Development Kit that contains tools and libraries for Java development.
 JRE is the Java Runtime Environment that enables running Java programs on your computer.
 JVM is the Java Virtual Machine that actually executes Java programs. With JVM, programs written
in Java can run on multi-platforms (thus Java is called cross-platform language).
 How to install JDK and configure environment variables.
 javac.exe is the Java compiler. It translates Java source code into bytecode.
 java.exe is the JVM launcher which we use to run our program.
 Every Java program starts from the main() method.
 When compiling, the compiler generates a .class file from a .java file.

How to set JAVA_HOME in Windows 10

Why do I need to set JAVA_HOME?

Many Java based programs like Tomcat require JAVA_HOME to be set as environment variable to work
correctly. Please note JAVA_HOME should point to a JDK directory not a JRE one. The point of setting the
environment variable is to let programs know in which directory executables like javac can be found.

1. Open Advanced System Settings

In Windows 10 press Windows key + Pause Key, This will open the System Settings window. Go to
Change settings and select the Advanced tab.

Alternatively:

Open “Windows search” – you will find it next to the Windows logo

1. In the search field type in – advanced system settings

7
2. Click on the match on top of the list

2. Set JAVA_HOME Environment variable

In “System Properties window” click “Environment Variables…”

Under “System variables” click the “New…” button and enter JAVA_HOME as “Variable name” and the
path to your Java JDK directory under “Variable value”

8
3. Update System PATH

1. In “Environment Variables” window under “System variables” select Path

2. Click on “Edit…”

3. In “Edit environment variable” window click “New”

4. Type in %JAVA_HOME%\bin

4. Test your configuration

Open a new command prompt and type in:

echo %JAVA_HOME%

this will print out the directory JAVA_HOME points to or empty line if the environment variable is not set
correctly

Now type in:

javac -version

this will print out the version of the java compiler if the Path variable is set correctly or “javac is not
recognized as an internal or external command…” otherwise

Вам также может понравиться