Tuesday, February 18, 2014

GETTING STARTED WITH JAVA PROGRAMMING

0 comments

Environment Used

  • Windows 7
  • Java 6
  • Notepad
  • Eclipse Helios

Requirements

Installing JDK

JDK should be installed with proper environment set up. 

Installing Eclipse IDE

We use Eclipse IDE through out this tutorial. If you need to install Eclipse, you can read this page.

Your First Java Program Using Notepad

  • Open Notepad and type the following code.
    1
    2
    3
    4
    5
    6
    public class Hello {
        /* My first Java program */
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
  • Save the file as “Hello.java” in some folder. In my case the location is “C:\Users\iByteCode\java-pgms\HelloWorld”.

Compile and Run

In Windows open Command Prompt (Windows button + R and type cmd) or Terminal in Linux and go to the folder (use cd command) where your java code exists.
  • To compile a Java program, type the command:
    • javac [filename]
    • So in this case, type: javac Hello.java
  • During compilation, javac adds a file to the disk called [filename].class, or in this case, Hello.class, which is the actual bytecode.
  • To run your Java program, type in the command:
    • java [.class file without the extension]
    • In the case of our example, type: java Hello
  • You can see on the screen after running the program: “Hello world”

Using Eclipse IDE

Integrated Development Environment (IDE) allows developers to quickly develop Java projects by all the needed features like, GUI builder, text or code editor, compiler and/or interpreter and a debugger in a single application.
In this section we will create the same Java project using Eclipse IDE.
  • Open Eclipse IDE.
  • Make sure you are in “Java Perspective”. If not go to Window -> Open Perspective -> Other, select Java and click on OK. Or Use the icon present near top right corner to change perspective.

Creating Java Project

New Project can be created in many ways,
  1. Inside Eclipse select the menu item File -> New -> Project…. to open the New Project wizard.
    • Select Java Project then click Next to start the New Java Project wizard:
    • Type “HelloWorld” in the Project name field, and make sure Java SE 1.6 is selected as execution environment
  2. Right click on Package Explorer -> New -> Java Project
  3. File -> New -> Other and Select Java Project.

Creating Java Class

  • After creating Java Project, right click on ‘src’ folder in package explorer -> New -> Class or go to File -> Class or click on the “New Java Class” icon on the toolbar.
  • Type the class name as “Hello” and make sure to check the “public static void main(String[] args)” check box and click on Finish
  • Type the following code in the editor and save your class (Ctrl + s).
    1
    2
    3
    4
    5
    6
    public class Hello {
        /* My first Java program */
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
  • Compiling and running java code in Eclipse IDE is easy. Eclipse automatically compiles the code and displays error messages in editor. You can also compile manually by going to Project -> Build Project. By default “Build Automatically” option is enabled in Eclipse.
  • The code can be run in many ways.
    1. Press Ctrl + F11 and the output will be displayed in the “Console” tab.
    2. (or) Click on the “Run” icon in toolbar.
    3. (or) Right click on editor and select “Run As” option.
    4. You should run it as Java application in all the options.

Code Explanation

  1. public class Hello
    • Indicates the name of the class which is Hello
    • In Java, all code should be placed inside a class declaration
    • The class uses an access specifier public, which indicates that our class in accessible to other classes from other packages (packages are a collection of classes).
  2. {
    • curly brace { indicates the start of a block.
  3. /* My first Java program */
    • Indicates a Java comment which is used to document a part of a code.
    • It is not part of the program itself, but used for documentation purposes.
    • It is good programming practice to add comments to your code.
    • Comments are ignored by the compiler but are useful to other programmers
  4. public static void main( String[] args ){
    • Method declaration
    • The main method is the starting point of a Java program.
    • All programs except Applets written in Java start with the main method.
    • Make sure to follow the exact signature.
  5. System.out.println(“Hello world”);
    • The command System.out.println(), prints the text enclosed by quotation on the screen.
  6. }
  7. }
    • The last two lines which contain the two curly braces are used to close the main method and class blocks respectively.

Coding Guidelines

  • Java programs should always end with the .java extension.
  • Filenames should match the name of your public class. So for example, if the name of your public class is Hello, you should save it in a file called Hello.java.
  • Write some comments in your code explaining what a certain class does, or what a certain method does.

Sunday, February 16, 2014

INSTALLING JDK ON UBUNTU LINUX

0 comments

Environment Used

  • Java SE 6 Update 30
  • Ubuntu Linux version 11.10 32-bit

Downloading Java Development Kit (JDK):

You can download latest version of JDK from this location
http://www.oracle.com/technetwork/java/javase/downloads/index.html
We have used Java SE 6 Update 30. Click on the download link and it will take you to the download page.
Accept the License Agreement and choose x86 (32-bit) or x64 (64-bit) version depending on your OS.
Download the JDK suitable for your system requirement to a safe location on your machine. We saved it on our home folder (/home/ibytecode).

Installing Java Development Kit (JDK) – Single User

Step 1:

Open Terminal by clicking on Ubuntu icon on desktop sidebar and type terminal in search bar. Click on the terminal icon.

Step 2:

Go to the folder where you saved the downloaded jdk file. In this case it is folder ‘/home/ibytecode”. So type,
cd /home/<username>

Step 3:

Check whether the downloaded jdk bin file has executable permissions by using the command,
ls –l

Step 4:

As you can see, the file does not have ‘x (executable) permission set. So change the file permissions to make it executable by issuing the following command.
chmod 755 <filename>
(i.e.)
chmod 755 jdk-6u30-linux-i586.bin

Step 5:

Check the permissions again by typing “ls –l” to make sure it is set properly. Now run the bin file by typing the following command.
./ jdk-6u30-linux-i586.bin

Step 6:

Press Enter to finish the installation. The jdk is installed in our current directory (i.e.) /home/ibytecode. Type “ls –l” to see the jdk folder, in our case it is jdk1.6.0_30.

Step 7:

Go into that folder by typing this command,
cd jdk1.6.0_30
and type “ls –l” to see the various folders like ‘bin’, ‘jre’, ‘lib’ etc.

Installing JDK – System wide install

You need to have administrator privileges for this section. If you do not have admin privileges please refer to single user install mentioned above.
Steps 1 – 4 are the same as in Single user install.

Step 1

Before we run the executable file, go to folder where you would like to install jdk, like /opt or /usr/share etc. We use /opt folder.
cd /opt

Step 2

Now run the file in “sudo” (admin) mode by issuing the following command.
sudo ~/jdk-6u30-linux-i586.bin
“~” refers home directory (/home/ibytecode)

Step 3

Press Enter to finish the installation. The jdk installed directory is /opt/jdk1.6.0_30.

Setting Required Environment Variables for Java

  • The installed JDK must be configured to the environment so that the Java compiler (javac) and runtime (java) becomes available for compiling and running the Java application from any location.
  • In Linux environments, there are many places where we can set our environment variables.
    According to Ubuntu documentation on Environment Variables
According to Ubuntu documentation on Environment Variables (https://help.ubuntu.com/community/EnvironmentVariables) the preferred file and location for setting environment variables which should affect FOR A SINGLE USER is .pam_environment file in your home directory.
Whereas FOR SYSTEM-WIDE ENVIRONMENT variables the preferred file is /etc/environment.
SINGLE USER: ~/.pam_environment
SYSTEM WIDE: /etc/environment
You can use any text editor which you are comfortable with to modify the files. We will be using “vi” editor; if you want a GUI based editor you can use “gedit”. To use that, wherever we type “vi” just replace with “gedit”.

Single User Java Environment Variables

  1. We will set the environment variable JAVA_HOME (used by many applications which require java) to point to the jdk folder location and add the “bin” folder inside the jdk folder to the PATH environment variable.
  2. Now go to your home folder by typing
    cd ~
  3. Create a file by using vi
    vi .pam_environment
  4. Press ‘i’ for insert mode and type the following lines.
    JAVA_HOME=~/jdk1.6.0_30
    PATH DEFAULT=${PATH}:${JAVA_HOME}/bin
  5. To save the file in “vi” editor press “Esc” and then type “:wq”.
  6. Now logout and then login again. Go to terminal and type the following commands.
    javac –version
    java –verison
  7. You should get proper outputs with the installed java version numbers displayed. If you get any command not found errors, then the path is not set properly.

System Wide Java Environment Variables

  1. We will set the environment variable JAVA_HOME (used by many applications which require java) to point to the jdk folder location and add the “bin” folder inside the jdk folder to the PATHenvironment variable.
  2. Open terminal and open the file “/etc/environment” by using “vi” in “sudo” mode.
    sudo vi /etc/environment
  3. Press ‘i’ for insert mode and JAVA_HOME.
    JAVA_HOME=/opt/jdk1.6.0_30
  4. Append /opt/jdk1.6.0_30/bin to the PATH variable preceded by “:” as shown below.
  5. To save the file in “vi” editor press “Esc” and then type “:wq”.
  6. Now logout and then login again. Go to terminal and type the following commands.
    javac –version
    java –verison
  7. You should get proper outputs with the installed java version numbers displayed. If you get any command not found errors, then the path is not set properly as shown in 5.1 Step 7.
  8. javac is the primary Java compiler, included in the Java Development Kit (JDK) and
  9. java is the Java launcher/runtime engine.
 

Copyright 2014 All Rights Reserved