Java Environment Setup
Before we proceed further it is important that we set up the java environment correctly. Once you installed Java on your machine, you would need to set environment variables to point to correct installation directories.
Path setup for windows 2000/XP:
Assuming you have installed Java in c:\Program Files\java\jdk directory:
· Right-click on 'My Computer' and select 'Properties'.
· Click on the '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 currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
Path setup for windows 95/98/ME:
Assuming you have installed Java in c:\Program Files\java\jdk directory:
· Edit the 'C:\autoexec.bat' file and add the following line at the end:
'SET PATH=%PATH%;C:\Program Files\java\jdk\bin'.
Path setup 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'.
Java Editors:
To write your java programs you will need a text editor.
Ø Notepad : On Windows machine you can use any simple text editor like Notepad .
Ø Netbeans :is a Java IDE that is open source and free .
Ø Eclipse : is also a java IDE developed by the eclipse open source community .
Sample Java Program:
Let us look at a simple code that would print the words Hello World.
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
How to save the file, compile and run the program. Please follow the steps given below:
· Open notepad and add the code as above.
· Save the file as : Sample JavaProgram.java.
· Open a command prompt window and go o the directory where you saved the class. Assume its C:\.
· Type ' javac Sample JavaProgram.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).
· Now type ' java Sample JavaProgram ' to run your program.
· You will be able to see ' Hello World ' printed on the window.
C : > javac Sample JavaProgram.java
C : > java Sample JavaProgram
Hello World
Basic Syntax for Java Program:
About Java programs, it is very important to keep in mind the following points.
Ø Case Sensitivity - Java is case sensitive which means identifier Hello and hello would have different meaning in Java.
Ø Class Names - For all class names the first letter should be in Upper Case.
Ø Method Names - All method names should start with a Lower Case letter.
Ø Program File Name - Name of the program file should exactly match the class name.
Ø public static void main(String args[]) - java program processing starts from the main() method which is a mandatory part of every java program.
Before we proceed further it is important that we set up the java environment correctly. Once you installed Java on your machine, you would need to set environment variables to point to correct installation directories.
Path setup for windows 2000/XP:
Assuming you have installed Java in c:\Program Files\java\jdk directory:
· Right-click on 'My Computer' and select 'Properties'.
· Click on the '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 currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
Path setup for windows 95/98/ME:
Assuming you have installed Java in c:\Program Files\java\jdk directory:
· Edit the 'C:\autoexec.bat' file and add the following line at the end:
'SET PATH=%PATH%;C:\Program Files\java\jdk\bin'.
Path setup 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'.
Java Editors:
To write your java programs you will need a text editor.
Ø Notepad : On Windows machine you can use any simple text editor like Notepad .
Ø Netbeans :is a Java IDE that is open source and free .
Ø Eclipse : is also a java IDE developed by the eclipse open source community .
Sample Java Program:
Let us look at a simple code that would print the words Hello World.
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
How to save the file, compile and run the program. Please follow the steps given below:
· Open notepad and add the code as above.
· Save the file as : Sample JavaProgram.java.
· Open a command prompt window and go o the directory where you saved the class. Assume its C:\.
· Type ' javac Sample JavaProgram.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).
· Now type ' java Sample JavaProgram ' to run your program.
· You will be able to see ' Hello World ' printed on the window.
C : > javac Sample JavaProgram.java
C : > java Sample JavaProgram
Hello World
Basic Syntax for Java Program:
About Java programs, it is very important to keep in mind the following points.
Ø Case Sensitivity - Java is case sensitive which means identifier Hello and hello would have different meaning in Java.
Ø Class Names - For all class names the first letter should be in Upper Case.
Ø Method Names - All method names should start with a Lower Case letter.
Ø Program File Name - Name of the program file should exactly match the class name.
Ø public static void main(String args[]) - java program processing starts from the main() method which is a mandatory part of every java program.