+ All Categories
Home > Documents > JDK Installation for Microsoft Windows

JDK Installation for Microsoft Windows

Date post: 27-Nov-2015
Category:
Upload: walaba06
View: 93 times
Download: 7 times
Share this document with a friend
Description:
jdk
23
JDK Installation for Microsoft Windows See JDK 7 and JRE 7 Installation Guide for general information about installing JDK 7 and JRE 7. The following topics are covered: System Requirements Installation Instructions Notation Installation Instructions o Downloading the Installer o Running the JDK Installer Installing the JDK Silently o Updating the PATH Environment Variable (Optional) o Starting to Use the JDK Uninstalling the JDK Installed Directory Tree Installation Troubleshooting o System File Not Suitable for Running MS-DOS and Microsoft Windows Applications o Corrupt Cabinet file o System Error During Decompression o Program Cannot Be Run in DOS Mode o Private Versus Public JRE o Creating Source Files in Notepad o Characters That Are Not Part of the System Code Page System Requirements See Oracle JRE 7 and JDK 7 Certified System Configurations for information about supported platforms, operating systems, and browsers. See Windows System Requirements for JDK and JRE for minimum processor, disk space, and memory requirements. Note: The JDK and JRE have a version string that enables you to determine the version number. See Java Platform Standard Edition 7, Names and Versions Version Number for information about Java SE 7 version numbers. The JDK has the option of installing the public JRE. For more information about JRE installation, see JRE Installation for Microsoft Windows . If you have any difficulties, see the Troubleshooting section at the end of this document or submit a bug report for your installation problem. Installation Instructions Notation
Transcript
Page 1: JDK Installation for Microsoft Windows

JDK Installation for Microsoft WindowsSee JDK 7 and JRE 7 Installation Guide for general information about installing JDK 7 and JRE 7.

The following topics are covered:

System Requirements Installation Instructions Notation Installation Instructions

o Downloading the Installer o Running the JDK Installer

Installing the JDK Silently o Updating the PATH Environment Variable (Optional) o Starting to Use the JDK

Uninstalling the JDK Installed Directory Tree Installation Troubleshooting

o System File Not Suitable for Running MS-DOS and Microsoft Windows Applications o Corrupt Cabinet file o System Error During Decompression o Program Cannot Be Run in DOS Mode o Private Versus Public JRE o Creating Source Files in Notepad o Characters That Are Not Part of the System Code Page

System RequirementsSee Oracle JRE 7 and JDK 7 Certified System Configurations for information about supported platforms, operating systems, and browsers.

See Windows System Requirements for JDK and JRE for minimum processor, disk space, and memory requirements.

Note:

The JDK and JRE have a version string that enables you to determine the version number. See Java Platform Standard Edition 7, Names and Versions Version Number for information about Java SE 7 version numbers.

The JDK has the option of installing the public JRE. For more information about JRE installation, see JRE Installation for Microsoft Windows.

If you have any difficulties, see the Troubleshooting section at the end of this document or submit a bug report for your installation problem.

Installation Instructions NotationFor any text in this document that contains the following notation, you must substitute the appropriate update version number:

<version>

For example, if you were downloading the JDK installer for 32-bit systems for update 1.7.0_01, the file name: jdk-7<version>-windows-i586.exe would become jdk-7u1-windows-i586.exe.

Similarly, if you were downloading the JDK installer for 64-bit systems for update 1.7.0_01, the file name jdk-7<version>-windows-x64.exe would become jdk-7u1-windows-x64.exe.

Page 2: JDK Installation for Microsoft Windows

Installation InstructionsIn this procedure, you will run the self-installing executable file to unpack and install the JDK. As part of the JDK, this installation includes an option to include the public Java Runtime Environment. (The JDK also contains a private JRE for use only by its tools; see Private Versus Public JRE for more information.)

Install the JDK by doing the following:

Downloading the Installer Running the JDK Installer

o Installing the JDK Silently Updating the PATH Variable (Optional) Starting to Use the JDK

Downloading the InstallerIf you save the self-installing executable file to disk without running it from the download page at the web site, note that its byte size provided on the download page. After the download has completed, verify that you have downloaded the full, uncorrupted software file.

Running the JDK InstallerYou must have administrative permissions in order to install the JDK on Microsoft Windows.

The file jdk-7<version>-windows-i586-i.exe is the JDK installer for 32-bit systems. The file jdk-7<version>-windows-x64.exe is the JDK installer for 64-bit systems. If you downloaded either file instead of running it directly from the web site, double-click the installer's icon. Then, follow the instructions the installer provides. The installer may ask you to reboot your computer. When finished with the installation, you can delete the downloaded file to recover disk space.

Note: Installers for JDK 7u6 and later install the JavaFX SDK and integrate it into the JDK installation directory. Installers for JDK 7u2 to 7u5 install the JDK first, then start the JavaFX SDK installer, which installs JavaFX SDK in the default directory C:\Program Files\Oracle\JavaFX 2.0 SDK or C:\Program Files (x86)\Oracle\JavaFX 2.0 SDK on 64-bit operating systems. If you want to install the JavaFX SDK (version 2.0.2) with JDK 7u1 or earlier, see Installing JavaFX for more information.

Installing the JDK SilentlyYou can perform a silent, non-interactive, JDK installation by using the command-line arguments. The following table lists example installation scenarios and the commands required to perform them:

Installation Scenario Command

Install the public JRE in silent mode

jdk.exe /s

Install development tools and source code in silent mode but not the public JRE

jdk.exe /s ADDLOCAL="ToolsFeature,SourceFeature"

Install development tools, source code, and the public JRE in silent mode

jdk.exe /s ADDLOCAL="ToolsFeature,SourceFeature,PublicjreFeature"

Page 3: JDK Installation for Microsoft Windows

Install the public JRE in the specified directoryC:\test\ in silent mode

jdk.exe /s /INSTALLDIRPUBJRE=C:\test\

Updating the PATH Environment Variable (Optional)You can run the JDK without setting the PATH environment variable, or you can optionally set it so that you can conveniently run the JDK executable files (javac.exe, java.exe,javadoc.exe, and so forth) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable file every time you run it, such as:

C:\> "C:\Program Files\Java\jdk1.7.0\bin\javac" MyClass.java

It is useful to set the PATH variable permanently so it will persist after rebooting.

To set the PATH variable permanently, add the full path of the jdk1.7.0\bin directory to the PATH variable. Typically, this full path looks something like C:\Program Files\Java\jdk1.7.0\bin. Set the PATH variable as follows on Microsoft Windows:

1. Click Start, then Control Panel, then System.

2. Click Advanced, then Environment Variables.

3. Add the location of the bin folder of the JDK installation for the PATH variable in System Variables. The following is a typical value for the PATH variable:

C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.7.0\bin

Note:

The PATH environment variable is a series of directories separated by semicolons (;) and is not case-sensitive. 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 a time. Those following the first instance are ignored.

If you are not sure where to add the path, add it to the right of the value of the PATH variable. The new path takes effect in each new command window you open after setting the PATH variable.

Starting to Use the JDKIf you are new to developing and running programs in the Java programming language, see The Java Tutorial online for some guidance. Note especially the tutorial trails under the heading Trails Covering the Basics.

You can also download the JDK documentation from the Java SE Downloads page.

Uninstalling the JDKIf you should ever want to uninstall the JDK, use the "Add/Remove Programs" utility in the Microsoft Windows Control Panel.

Installed Directory Tree

Page 4: JDK Installation for Microsoft Windows

See JDK and JRE File Structure for a description of the directory structure of the JDK. (Note that the file structure of the JRE is identical to that of the JDK's jre directory.)

Installation TroubleshootingBelow are some tips for working around problems that are sometimes seen during or following an installation. For more troubleshooting information, see Troubleshooting Java SE:

Corrupt Cabinet file System Error During Decompression Program Cannot Be Run in DOS Mode Private Versus Public JRE Creating Source Files in Notepad Characters That Are Not Part of the System Code Page

Corrupt Cabinet FileIf you see the error message "corrupt cabinet file," then the file you have downloaded is corrupted. Check the file size against the expected file size listed in these instructions. If sizes do not match, try downloading the bundle again. (A cabinet file contains compressed application, data, resource, and DLL files.)

System Error During DecompressionIf you see the error message "system error during decompression," then you might not have enough space on the disk that contains your TEMP directory.

Program Cannot Be Run in DOS ModeIf you see the error message "This program cannot be run in DOS mode," then do the following:

1. Open the MS-DOS shell or Command Prompt window.2. Right-click the title bar.3. Select Properties.4. Choose the Program tab.5. Click the Advanced button.6. Ensure that the item "Prevent MS-DOS-based programs from detecting Windows" is not selected.7. Select OK.8. Select OK again.9. Exit the MS-DOS shell.10. Restart your computer.

Private Versus Public JREInstalling the JDK also installs a private JRE and optionally a public copy. The private JRE is required to run the tools included with the JDK. It has no registry settings and is contained entirely in a jre directory (typically at C:\Program Files\jdk1.7.0\jre) whose location is known only to the JDK. On the other hand, the public JRE can be used by other Java applications, is contained outside the JDK (typically at C:\Program Files\Java\jre1.7.0), is registered with the Windows registry (at HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft), can be removed using Add/Remove Programs, might be registered with browsers, and might have the java.exe file copied to the Windows system directory (which would make it the default system Java platform).

Source Files in NotepadIn Microsoft Windows, when you create a new file in Microsoft Notepad and then save it for the first time, Notepad usually adds the .txt extension to the file name. Therefore, a file you name Test.java is

Page 5: JDK Installation for Microsoft Windows

saved as Test.java.txt. It is important to note that you cannot see the .txt extension unless you turn on the viewing of file extensions (in Microsoft Windows Explorer, unselect "Hide file extensions for known file types" under Folder Options). To prevent the .txt extension, enclose the file name in quotation marks, such as "Test.java", when typing it into the Save As dialog box.

On the other hand, Microsoft WordPad does not add the .txt extension if you specify another extension. However, you must save the file as "Text Document".

Characters That Are Not Part of the System Code PageOn Windows XP, it is possible to name directories using characters that are not part of the system locale's code page. If such a directory is part of the installation path, then generic error 1722 occurs, and installation is not completed. Error 1722 is a Windows Installer error code. It indicates that the installation process has failed. The exact reason for this error is not known at this time.

To prevent this problem, ensure that the user and system locales are identical, and that the installation path contains only characters that are part of the system locale's code page. User and system locales can be set in the Regional Options or Regional Settings control panel.

The associated bug number is 4895647.

**

yet another insignificant programming notes...   |   HOMETABLE OF CONTENTS (HIDE)1.     How To Install JDK on Windows 1.1     Using TextPad for Java Programming (on Windows) 1.2     Using NotePad++ for Java Programming (on Windows) 2.     How to Install JDK on Mac 3.     How to Install JDK on Ubuntu 4.     First Java Program with Eclipse 5.     First Java Program with NetBeans 6.     (Advanced) External JAR Files and Native Libraries

JDK 7How to Install andGet Started with Java Programming (on Windows, Mac & Ubuntu)

Page 6: JDK Installation for Microsoft Windows

Java Development Kit (JDK) 1.7 (officially named Java SE 7), which is freely

available from Sun Microsystems (now part of Oracle), is needed for writing Java

programs. JDK can be downloaded from the Java mother site

@ http://java.sun.com (or http://www.oracle.com/technetwork/java/index.html).JDK or JRE?

JRE (Java Runtime) is needed for running Java programs. JDK (Java Development

Kit), which includes JRE plus the development tools (such as compiler and

debugger), is need for writing as well as running Java programs. Since you are

supposed to write Java Programs, you should install JDK, which includes JRE.JDK Versions

The various JDK versions are:

1. JDK Alpha and Beta (1995): Sun announced Java in September 23,

1995.

2. JDK 1.0 (January 23, 1996): Originally called Oak (named after the oak

tree outside James Gosling's office). Renamed to Java 1 in JDK 1.0.2.

3. JDK 1.1 (February 19, 1997): Introduced AWT event model, inner class,

JavaBean, JDBC, and RMI.

4. J2SE 1.2 (codename Playground) (December 8, 1998): Re-branded as

"Java 2" and renamed JDK to J2SE (Java 2 Standard Edition). Also released

J2EE (Java 2 Enterprise Edition) and J2ME (Java 2 Micro Edition). Included

JFC (Java Foundation Classes - Swing, Accessibility API, Java 2D, Pluggable

Look and Feel and Drag and Drop). Introduced Collection Framework and

JIT compiler.

5. J2SE 1.3 (codename Kestrel) (May 8, 2000): Introduced Hotspot JVM.

6. J2SE 1.4 (codename Merlin) (February 6, 2002): Introduced assert, non-

blocking IO (nio), logging API, image IO, Java webstart, regular expression

support.

7. J2SE 5.0 (codename Tiger) (September 30, 2004): Officially called 5.0

instead of 1.5. Introduced generics, autoboxing/unboxing, annotation,

enum, varargs, for-each loop, static import.

8. Java SE 6 (codename Mustang) (December 11, 2006): Renamed J2SE to

Java SE (Java Standard Edition).

9. Java SE 7 (codename Dolphin) (July 28, 2011): First version after Oracle

purchased Sun (called Oracle JDK).

10. Java SE 8: expected in summer 2013?!

Page 7: JDK Installation for Microsoft Windows

1.  How To Install JDK on WindowsStep 0(a): Un-Install Older Version(s) of JDK/JRE

I recommend that you install the latest JDK, but it can be messy if you have

multiple versions of JDK/JRE.

If you have previously installed older version(s) of JDK/JRE, un-install ALL of

them. Run "Control Panel" ⇒ Program and Features ⇒ Un-install programs begin

with "Java", such as "Java SE Development Kit" and "Java SE Runtime". If you are

not sure whether you have older version(s) of JDK, check!

Step 0(b): Understand Windows' CMD Shell

Programmers need to know how to use CMD shell to issue commands. If you are

completely new to CMD, read "Programmer's Survival Guide for Windows".Step 1: Download JDK

1. Goto Java SE download site

@ http://www.oracle.com/technetwork/java/javase/downloads/index.html.

2. Click the "Download" button under "JDK" of "Java SE 7".

3. Check "Accept License Agreement".

4. Choose your operating platform, e.g., Windows x86 for 32-bit Windows OS

or Windows x64 for 64-bit Windows OS. You can check whether your

Windows OS is 32-bit or 64-bit via "Control Panel" ⇒ System ⇒ Under the

"System Type".

Step 2: Install JDK and JRE

Run the downloaded installer (e.g., "jdk-7uxx-windows-i586.exe"), which installs

both the JDK (Java Development Kit) and JRE (Java Runtime). By default, the JDK

will be installed in directory "C:\Program Files\Java\jdk1.7.0_xx",

where xx denotes the latest upgrade number; and JRE in "C:\Program Files\Java\

jre7".

For novices, accept the defaults. Simply click "next"..."next"... to install JDK in

"C:\Program Files\Java\jdk1.7.0_xx" and JRE in "C:\Program Files\Java\jre7".

Take note of your JDK installed directory (which you will need to use in the next

step). Check the JDK installed directory by inspecting these folders using

Windows' Explorer.

Page 8: JDK Installation for Microsoft Windows

I shall refer to the JDK installed directory as <JAVA_HOME>, hereafter, in this article.Step 3: Include JDK's "bin" Directory in the PATH

Windows OS searches the current directory and the directories listed in

the PATH environment variable for executable programs. JDK's programs (such as

Java compiler javac.exe and Java runtime java.exe) reside in directory

"<JAVA_HOME>\bin" (where <JAVA_HOME> denotes the JDK installed directory,

e.g., C:\Program Files\Java\jdk1.7.0_xx). You need to include the "<JAVA_HOME>\

bin" directory in the PATH.

To edit the PATH environment variable in Windows 2000/XP/Vista/7/8:

1. Click "Start" button ⇒ "Control Panel" ⇒ "System" ⇒ (Vista/7/8 only)

"Advanced system settings".

2. Switch to "Advanced" tab ⇒ "Environment Variables..."

3. In "System Variables" box, scroll down to select "PATH" ⇒ "Edit..."

4. (CAUTION: Read this paragraph 3 times before doing this step!

There is no UNDO) In "Variable value" field, INSERT "c:\Program Files\

Java\jdk1.7.0_xx\bin" (Replace xx with the upgrade number and VERIFY

that this is your JDK's binary directory!!!) IN FRONT of all the existing

directories, followed by a semi-colon ( ; )  which separates the JDK's binary

directory from the rest of the existing directories. DO NOT DELETEany

existing entries; otherwise, some existing applications may not run.

5. Variable name : PATH

Variable value : c:\Program Files\Java\jdk1.7.0_xx\bin;[exiting entries]

(For Advanced Users Only)I suggested that you place the JDK bin directory in front of "c:\windows\system32"

and "c:\windows". This is because some Windows systems may have an out-

Page 9: JDK Installation for Microsoft Windows

dated copy of JDK/JRE in these directories. Do a search for "java.exe", and you

will be amazed by the findings.

You could read "Java Applications and Environment Variable" for more

discussions about PATH environment variable.

I also recommend that you define an environment variable called JAVA_HOME,

which contains the JDK installed directory, and include the JDK bindirectory in

the PATH via JAVA_HOME, i.e., PATH=%JAVA_HOME%\bin;.....Step 4: Verify the JDK Installation

Launch a CMD shell (Click "Start" button ⇒ run... ⇒ enter "cmd"; or "Start" button ⇒ All Programs ⇒ Accessories ⇒ Command Prompt).

1. Issue a "path" command to list the contents of the PATH environment

variable. Check the output and make sure that <JAVA_HOME>\bin is listed in

the PATH.

2. prompt> path

PATH=c:\Program Files\Java\jdk1.7.0_xx\bin;[other entries]

3. Issue the following commands to verify that JDK/JRE are properly installed

and display their version:

4. prompt> java -version5. java version "1.7.0_xx"6. Java(TM) SE Runtime Environment (build 1.7.0_xx-b11)7. Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)8. 9. prompt> javac -version

javac 1.7.0_xx

Step 5: Write a Hello-World Java Program

1. Create a directory to keep all your works, e.g., d:\myproject, or any

directory of your choice. Do NOT save your works in "Desktop" or

"Documents" as they are hard to locate. The directory name shall not

contain blank or special characters. Use meaningful but short name as it is

easier to type.

2. Launch a programming text editor (such as TextPad or NotePad++). Begin

with a new file and enter the following source code. Save the file as

"Hello.java", under your work directory (e.g., d:\myproject).

3. /*4. * First Java program to say Hello

Page 10: JDK Installation for Microsoft Windows

5. */6. public class Hello { // Save as "Hello.java" under "d:\

myproject"7. public static void main(String[] args) {8. System.out.println("Hello, world!");9. }

}

Page 11: JDK Installation for Microsoft Windows

Step 6: Compile and Run the Hello-World Java Program

1. To compile the source code "Hello.java":

a. Start a CMD Shell (Click "Start" button ⇒ Select "run..." ⇒ Enter "cmd";

or "Start" button ⇒ All Programs ⇒ Accessories ⇒ Command Prompt).

Page 12: JDK Installation for Microsoft Windows

b. Set the Current Drive to the drive where you saved your source file

"Hello.java". For example, suppose that your source file is saved in

drive "d", enter "d:" as follow:

c. prompt> d:

D:\xxx>

d. Set the Current Working Directory to the directory that you saved

your source file via the cd (Change Directory) command. For example,

suppose that your source file is saved in directory "d:\myproject".

e. D:\xxx> cd \myproject

D:\myproject>

f. Issue a dir (List Directory) command to confirm that your source file

is present in the current directory.

g. D:\myproject> dirh. ......i. 08-May-XX 06:25 PM 277 Hello.java

......

j. Invoke the JDK compiler "javac" to compile the source code

"Hello.java".

D:\myproject> javac Hello.java

The compilation is successful if the command prompt returns.

Otherwise, error messages would be shown. Correct the errors in your

source file and re-compile. Check "Common JDK Installation Errors", if

you encounter problem compiling your program.

k. The output of the compilation is a Java class called "Hello.class".

Issue a dir (List Directory) command again to check for the output.

l. D:\myproject> dirm. ......n. xx-xxx-xx 01:53 PM 416 Hello.classo. xx-xxx-xx 06:25 PM 277 Hello.java

......

To run the program, invoke the Java Runtime "java":

D:\myproject> java Hello

Page 13: JDK Installation for Microsoft Windows

Hello, world!

Everything that can possibly go wrong will go wrong : Read

"JDK Installation Common Errors".Step 7: (Optional) Download JDK API Documentation, Samples and Demos

The JDK download does not include the documentation, which needs to be

downloaded separately. In the past, I always insist that my students should have

a local copy of JDK API Documentation. But, today, you can easily access the

online copy by googling "JDK 7 Documentation".

To install JDK API documentation:

1. From the Java SE download page

(@ http://www.oracle.com/technetwork/java/javase/downloads/index.html),

look for "Java SE 7 Documentation" (under "Additional Resources") ⇒ Download the zip-file (e.g., "jdk-7....zip" - about 57MB).

2. Unzip into the <JAVA_HOME> (JDK installed directory). The documentation will

be unzipped into "<$JAVA_HOME>\docs". Browse the JDK documentation by

opening "<JAVA_HOME>\docs\index.html".

You should also download the "JDK Samples and Demos" from the Java SE

download site.

Step 8: (For Advanced Users Only) JDK's Source Code

Source code for JDK is provided and kept in "<JAVA_HOME>\src.zip". I strongly

recommend that you to go through some of the source files such as

"String.java", "Math.java", and "Integer.java", under "java\lang".1.1  Using TextPad for Java Programming (on Windows)

Page 14: JDK Installation for Microsoft Windows

TextPad (@ www.textpad.com) is an excellent programming text editor for

writing toy Java programs. It can be configured to couple with the JDK, hence,

bypassing the CMD shell. From the TextPad editor, you can invoke the JDK

compiler/runtime directly via "Tools" menu ⇒ External Tools ⇒ "Compile Java" or

"Run Java Application". Take note of the keyboard shortcuts - Ctrl+1 for compile

and Ctrl+2 for run.

If you cannot find these commands in the "Tools" menu, goto "Configure" ⇒ Preferences... ⇒ Tools ⇒ Add ⇒ JDK Commands. You can also configure the

properties of "compile" and "run" there, such as prompting for command-line

arguments.

TextPad Tips and Configuration

Check HERE!1.2  Using NotePad++ for Java Programming (on Windows)

Notepad++ (@ http://notepad-plus-plus.org) is a free and open-source

programming editor.

You can use NotePad++ to input Java source code, and compile and run the Java

program under CMD shell.

Page 15: JDK Installation for Microsoft Windows

NotePad++ Customization and Tips

You can customize Notepad++ to compile and run Java programs with hot-keys.

Read "NotePad++".

2.  How to Install JDK on MacStep 0: Understand the "Terminal"

Programmers need to know how to issue commands through the "Terminal". If

you are new to Terminal, read "Programmer's Survival Guide for Mac and

Ubuntu".Step 1: Check if JDK has been Pre-Installed

In some Mac systems (earlier than Mac OS X 10.7 (Lion)), JDK has been pre-

installed. To check if JDK has been installed, open a "Terminal" (Go ⇒ Utilities ⇒ Terminal) and issue these commands:

$ javac -version

If a JDK version number is returned (e.g., JDK {1.x.x}), then JDK has already

been installed. Proceed to "Step 3: Write a Hello-world Java program".

If message "command not found" appears, JDK is NOT installed. Proceed to

the "Step 2: Install JDK".

If message "To open javac, you need a Java runtime" appears, select

"Install" and follow the instructions to install JDK. Then, proceed to "Step 3:

Write a Hello-world Java program".

Step 2: Download and Install JDK

1. Goto http://connect.apple.com.

2. Login with your AppleID.

3. Download "Java for Mac OS X {10.x} Update {u} Developer Package

(DMG)". Choose {10.x} according to your Mac OS X version and the

latest{u}.

4. Double-click to install the downloaded Disk Image (DMG) file.

5. Eject the DMG file.

6. To verify your installation, open a "Terminal" and issue these commands:

7. // Check the version of "javac" (Java Compiler) and "java" (Java Runtime)

8. $ javac -version9. javac {1.x.x_xx}

Page 16: JDK Installation for Microsoft Windows

10. 11. $ java -version12. java version "{1.x.x_xx}"13. Java(TM) SE Runtime Environment (build {1.x.x_xx-xxx})14. Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)15. 16. // Find the location of "javac" (Java Compiler) and "java" (Java

Runtime)17. $ which javac18. /usr/bin/javac19. 20. $ which java

/usr/bin/java

Step 3: Write a Hello-World Java Program

1. Create a directory called "myproject" under your home directory (Finder ⇒ Go ⇒ Home; File ⇒ New Folder ⇒ "myproject").

In Mac under the Terminal, the home directory of the current login user is

denoted as "~". Hence, this new directory is represented as "~/myproject".

2. Use a programming text editor (such as jEdit, gedit) to input the following

source code and save as "Hello.java" under the directory "~/myproject"

created earlier.

If you use "TextEdit" (NOT encouraged, as it is a plain text editor, NOT

a programming text editor), you need to open a new file ⇒ choose

"Format" ⇒ "Make Plain Text" ⇒ Enter the source code ⇒ Save as

"Hello.java" (without the ".txt").

3. /*4. * My First Java program to say Hello5. */6. public class Hello { // Save as "Hello.java" under "~/myproject"7. public static void main(String[] args) {8. System.out.println("Hello, world from Mac!");9. }

}

Page 17: JDK Installation for Microsoft Windows

Step 4: Compile and Run the Hello-World Java Program

1. To compile the source code "Hello.java", open a new "Terminal" (Go ⇒ Utilities ⇒ Terminal) and issue these commands (as illustrated):

2. // Change Directory (cd) to where "Hello.java" resides3. // i.e., sub-directory "myproject" under home (~)4. $ cd ~/myproject5. 6. // Check if "Hello.java" exists using list (ls) command7. $ ls8. Hello.java ......9. 10. // Compile "Hello.java" using JDK compiler "javac"11. $ javac Hello.java12. // If error message appears, debug your source code13. 14. // Check for the output "Hello.class"15. $ ls

Hello.class Hello.java ......

16. To run the Hello-world, invoke the Java Runtime "java" as follows:

17. // Run "Hello.class"18. $ java Hello

Hello, world from Mac!

3.  How to Install JDK on UbuntuRead "How to Install JDK on Ubuntu".

4.  First Java Program with Eclipse1. You need to first install Eclipse. Read "How to Install Eclipse".

2. You can then proceed to write your first Java program. Read "Writing your

first Java Program with Eclipse".

3. Read "Debugging program in Eclipse".

Page 18: JDK Installation for Microsoft Windows

5.  First Java Program with NetBeans1. You need to first install NetBeans. Read "How to Install NetBeans".

2. You can then proceed to write your first Java program. Read "Writing your

first Java program with NetBeans".

3. Read "Debugging program in NetBeans".

6.  (Advanced) External JAR Files and Native LibrariesExternal Java packages (such as Servlet, MySQL Connector/J, JOGL, JUnit) are

often distributed in JAR files (Java Archive - a single-file package of many Java

classes), with possibly Native Libraries (".lib" and ".dll" in Windows, or ".a"

and ".so" in Linux/Mac).External JAR Files (".jar")

If external JAR files are not properly included:

During the compilation, you will receive compilation error "cannot find

symbol" on classes belonging to the external packages.

During execution, you will get a runtime error "Could not find or load main

class xxx" or "NoClassDefFoundError".

To include external JAR files, you can either:

1. Copy all the JAR files of the external packages to the Java's Extension

Directories.

o For Windows, the JDK extension directory is located at "<JAVA_HOME>\

jre\lib\ext" (e.g., "c:\Program Files\Java\jdk1.7.0_xx\jre\lib\ext").

o For Mac, the JDK extension directories are "/Library/Java/Extensions"

and "/System/Library/Java/Extensions".

o For Ubuntu, the JDK extension directories are "<JAVA_HOME>/jre/lib/ext"

(e.g., "/usr/user/java/jdk1.7.0_xx/jre/lib/ext") and

"/usr/java/packages/lib/ext".

The location of JDK's extension directories is kept in Java's System Property

"java.ext.dirs". You can print its contents

viaSystem.out.println(System.getProperty("java.ext.dirs")).

2. You can also include all the JAR files in the CLASSPATH environment variable.

The CLASSPATH may contain directories (containing many Java classes) or

JAR files (single-file archive of Java classes). If you set the CLASSPATH, you

must also include the current directory (denoted as ".").

Page 19: JDK Installation for Microsoft Windows

o For Windows, set the CLASSPATH in Control Panel ⇒ System ⇒ Advanced

system settings ⇒ Advanced ⇒ Environment Variables ⇒ System

Variables ⇒ New ⇒ In "Variable name", enter "CLASSPATH" ⇒ In

"Variable value", enter ".;path1\xxx.jar;path2\yyy.jar", where the

entries are separated by a semi-colon (;).

o For Linux and Mac OS:

Edit ~/.profile or ~/.bash_profile (or /etc/profile for system-wide

setting) to include the following line at the end of the file:

export CLASSPATH=.:path1/xxx.jar:path2/yyy.jar

The entries are separated by colon (:).

3. You can also set the CLASSPATH in the javac/java's command-line via the

option -cp <paths> (or -classpath <paths>), for example,

4. // Compile Java source code5. > javac -cp .:path1/xxx.jar:path2/yyy.jar ClassName.java6. // Run Java class

> java -cp .:path1/xxx.jar:path2/yyy.jar ClassName

External Native Libraries (".lib", ".dll", ".a", ".so")

Some external package may provide static or shared native libraries in the form

of ".lib" (Windows' static library), ".dll" (Windows' dynamically link library),

".a" (Unix's static library), or ".so" (Unix's shared library).

Native Libraries are to be kept in a directory accessible via JRE's Property

"java.library.path", which normally but not necessarily includes all the

directories in the PATH environment variable.

Native libraries are not involved in the compilation. But if they are not properly

included during runtime time, you will get a runtime error

"java.lang.UnsatisfiedLinkError: no xxx in java.library.path".

To include external native libraries:

1. Copy the native libraries into a system library directory, e.g., c:\windows\

system32 (Windows), /usr/lib or /usr/local/lib (Linux or Mac OS). You can

verify that the directory is included in Java's System Property

"java.library.path",

viaSystem.out.println(System.getProperty("java.library.path")).

2. You can also set the native library path via the java's command-line

option -Djava.library.path=xxx, for example,

Page 20: JDK Installation for Microsoft Windows

> java -Djava.library.path=xxx ClassName

Eclipse/NetBeans

Using an IDE can greatly simplifies inclusion of external packages. Read "Eclipse

How-To" or "NetBeans How-To".

Link to References & Resources

Latest version tested: JDK 1.7.0_45

Last modified: January, 2014Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan

([email protected])   |   HOMEPath

Windows 71. Select Computer from the Start menu

2. Choose System Properties from the context menu

3. Click Advanced system settings > Advanced tab

4. Click on Environment Variables, under System Variables, find PATH, and click on it.

5. In the Edit windows, modify PATH by adding the location of the class to the value

for PATH. If you do not have the item PATH, you may select to add a new variable

and add PATH as the name and the location of the class as the value.

6. Reopen Command prompt window, and run your java code.

Windows Vista1. Right click My Computer icon

2. Choose Properties from the context menu

3. Click Advanced tab (Advanced system settings link in Vista)

4. In the Edit windows, modify PATH by adding the location of the class to the value

for PATH. If you do not have the item PATH, you may select to add a new variable

and add PATH as the name and the location of the class as the value.

5. Reopen Command prompt window, and run your java code.


Recommended