Tuesday, December 25, 2012

Installation and Environment Setup for Java

Follow the below steps to set up the environment of Java.

     Java SE is free software to download from the following URL http://www.oracle.com/technetwork/java/index.html. You can choose a version based on your operating system.

     Follow the instructions to run the .exe to install Java on your machine. Once you installed Java on your machine, you should set environment variables to point to correct installation directories.

Setting up the path for windows 2000/XP/7:

Assuming you have installed Java in c:\Program Files\Java\jdk directory:

  • Right-click on 'My Computer' and select 'Properties'.
  • Click on 'Environment Variables' button under the 'Advanced' tab.
  • Now alter the 'Path' variable so that it also contains the path to the Java executable.
    Example: if the path is set to some other path then append the below path to existed path.         ";C:\Program Files\java\jdk\bin".

Setting up the path for Linux, UNIX, Solaris, FreeBSD:

Environment variable PATH should be set to point to where the java binaries have been installed. Refer to your shell documentation if you have trouble doing this.

Example, if you use bash as your shell, then you would add the following line to the end of your '.bashrc:export PATH=/path/to/java:$PATH'

Most Popular Java Editors:

1. Notepad
2. Netbeans
3. Eclips

Creating First Application:

Assume your first application MyApp, will display "Hello World".

Source Code:

public class MyApp {

public static void main(String args[]) {
System.out.println("Hello World"); //prints Hello World

Note: While Compiling and Executing any Java Programs program names are case-sensitive.

Executing the Java Program:

Create a source file(creating .java file): Source file contains the required code, written in Java programming language. You can use any text editor to create and edit source files.

Compile the source file(it will create a .class file): Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.

Run the program: The Java application launcher tool (java) uses the Java virtual machine to run your application.

How to Write, Save, Write, Compile and Run a Java Program:

1. Open Text Editor and write code and save the file as MyApp.java.
2. Open command prompt window and go to the directory where you saved the class. 
Example C:\
3. Type 'javac MyApp.java' and press enter to compile your code. If there are no errors in your code the command prompt will take you to the next line.(Assumption: The path variable is set).
4. Now type 'java MyApp' to run your program.
5. You will be able to see 'Hello World' printed on the window.

Now you can write and execute any number of programs.... Enjoy writing.

No comments:

Post a Comment