Wednesday, May 19, 2010

Changing the JRE being used in Eclipse

To change the JRE you are currently working on
  • Window > Preferences > Java >Installed JREs
  • Add the new installed JRE. Check the one you want to use.
  • Now go to Window > Preferences > Java > Compiler
  • change the "Compiler compliance level" to the one you are using
To fix the JRE not compatible with workspace .class file compatibility: 1.6 error
  • Window > Preferences > Java > Compiler and change the "Compiler compliance level" to 5.0

Adding External Library (.jar) in Eclipse

Adding external library (.jar) to the java classpath
  • From the menu select File -> Import -> File system. Select your jar and select the folder lib as target.
  • You can either create complete folder structure or selected folder structure only.Select the selected folder structure only option.
  • Select your project, right mouse click and select properties. Under libraries select "Add JARs".
  • If you find some errors in the import then find the jar in eclipse/plugin folder for the missing jar files.

Difference Between JRE and JDK

JRE and JDK
JRE
(Java Runtime environment)
JDK
(Java Development Toolkit)
It is an implementation of the Java Virtual Machine* which actually executes Java programs.It is a bundle of software that you can use to develop Java based applications.
Java Run Time Environment is a plug-in needed for running java programs.Java Development Kit is needed for developing java applications.
JRE is smaller than JDK so it needs less Disk space.JDK needs more Disk space as it contains JRE along with various development tools.
JRE can be downloaded/supported freely from
java.com
JDK can be downloaded/supported freely from
java.sun.com
It includes JVM , Core libraries and other additional components to run applications and applets written in Java.It includes JRE, set of API classes, Java compiler, Webstart and additional files needed to write Java applets and applications.
Reference :

Opening a Existing Project in Eclispe

Opening a Existing Project in Eclipse
  • New> Java Project
  • Project name should be the same as the folder name of the existing project
  • Content == Create project from the existing source
  • Projects located in the workspace folder must be direct sub folders of the workspace folder
  • Select the working set as the folder name as the name of the project folder.

Useful Shorts for Eclipse

Shortcuts for Eclipse
To comment Multiple lines :
  • Select all the lines to be commented
  • Press Ctrl + /
  • To uncomment do the same thing
To see the Declaration or corresponding code for a function :
  • Select the function you want to see the code for.
  • Press F3 or right click and do Open Deceleration
To Automatically indent the code use the following keys
  • Select the Code to be indented properly
  • Press Ctrl + i
To Search a function and declaration within a code
  • Press Ctrl + o


Tuesday, May 11, 2010

JUnit to test Java Programs

JUnit is a framework for developing unit tests for Java classes. JUnit provides a base class called TestCase that can be extended to create a series of tests for the class you are creating, an assertion library used for evaluating the results of individual tests, and several applications that run the tests you create.

You need to create a new separate class XText if our original programs class name is X.

There are three things you must do as you set up this class in order to run your tests:

  1. Import junit.framework.*.
  2. Extend TestCase.
  3. Provide a constructor with a single String parameter. This constructor should call the TestCase constructor with the parameter.

Here is a minimal class that demonstrates the structure of classes derived from TestCase:

import junit.framework.*;  
public class XTest extends TestCase {  
 public XTest(String name)  { 
    super(name); 
  }    
 public void Y()  {   .........  } 
} 

Monday, May 10, 2010

Eclipse Basic Help

To add a package to the project in eclipse

Create a new Java project
  • File>New>Java Project
After the Java project is created
  • Right click on the new created folder by the Java
  • New>Package
  • Now add the new Java Package name.
  • You can use dots (.) to specify multiple folders.
  • After the package is added you will see a Tree Hierarchy of the folders which you can see when you go to the folder or when you add the class file inside it.
To add the class file
  • Right click on the last folder and click NEW> Class
  • This will create the new class for the project.
Running you Java program outside Eclipse(create a jar File)
  • To run your Java program outside of Eclipse you need to export it as a jar file. Select your project, right click on it and select "Export".
  • Select JAR file, select next. Select your project and maintain the export destination and a name for the jar file. I named it "myprogram.jar".
Adding external library (.jar) to the java classpath
  • From the menu select File > Import > File system. Select your jar and select the folder lib as target.
  • Select your project, right mouse click and select properties. Under libraries select "Add JARs".
  • If you find some errors in the import then find the jar in eclipse/plugin folder for the missing jar files.
To change the JRE you are currently working on
  • Window > Preferences > Java >Installed JREs
  • Add the new installed JRE. Check the one you want to use.
  • Now go to
  • Window > Preferences > Java > Compiler
  • change the "Compiler compliance level" to the one you are using
To fix the JRE not compatible with workspace .class file compatibility: 1.6 error
  • Window > Preferences > Java > Compiler and change the "Compiler compliance level" to 5.0
REFERENCE: