Setup Eclipse IDE (Integrated Development Environment)

Edited by Lim Siong Boon, last dated 22-Sep-2010.

email:    contact->email_siongboon  

website: http://www.siongboon.com



Eclipse classpath setup

Most of the time when my code couldn't compile, the problem is most probably on the class path. Attached is a sample the classpath file from one of my typical java project. This classpath is named as .classpath and is found under the root of the Eclipse project folder. This file is specially for Eclipse IDE to allow the runtime to locate the files and resources required by the project.

Eclipse java classpath example (.classpath, another classpath example)
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_18">
        <attributes>
            <attribute name="owner.project.facets" value="jst.java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v6.0">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry combineaccessrules="false" kind="src" path="/Wake&amp;Sleep"/>
    <classpathentry kind="lib" path="E:/java projects/wakeShutWebservice/lib/axiom-api-1.2.8.jar"/>
    <classpathentry kind="lib" path="E:/java projects/wakeShutWebservice/lib/axiom-dom-1.2.8.jar"/>
    <classpathentry kind="lib" path="E:/java projects/wakeShutWebservice/lib/axiom-impl-1.2.8.jar"/>
    <classpathentry kind="lib" path="E:/java projects/wakeShutWebservice/lib/commons-logging-1.1.jar"/>
    <classpathentry kind="lib" path="E:/java projects/wakeShutWebservice/lib/log4j-1.2.14.jar"/>
    <classpathentry kind="lib" path="resources"/>
    <classpathentry kind="output" path="build/classes"/>
</classpath>


If you are not using Eclipse to run your java project, and is using the command line prompt, the classpath will have to be indicated in your javac command. The following example is a window batch (*.bat) file that indicate the classpath and compile the java code through the command prompt. The shell script (*.sh) file is similar to the batch file, which is used in the Linux OS. The period "." is refering with reference to the executing or current directory."./bin" indicates that the subdirectory to be included in the classpath.

The last parameters "MyMain" indicates the initial class object to invoke. The Java will attempt to execute the main(String[] args) method that is found in this class. If the class object is under a directory call "myfolder", the last parameter will be "myfolder/MyMain"

MyMain is the class name for the program entry point.

More classpath reference,
http://mindprod.com/jgloss/classpath.html

Command prompt java classpath (run.bat for Windows OS, run.sh for Linux OS)
java -classpath .;bin;lib/log4j-1.2.14.jar;lib/commons-logging-1.1.jar;lib/PingICMP.jar;
lib/axiom-api-1.2.8.jar;lib/axiom-dom-1.2.8.jar;lib/axiom-impl-1.2.8.jar;
resource MyMain

run2.bat batch file with Ant example.

 

Command prompt java classpath (run.sh for Linux OS)
#!/bin/sh
#comment for writing a shell script file for linux
#1) save this file as *.sh (linux shell script file)
#2) run command "chmod 777 *.sh" to change the file permission for executable
#3) type "./samplescript.sh" to run this script in linux
echo “LSB shell script file for Linux”
java -cp ./bin myMain


Shortcut file for Microsoft Windows user to open command prompt which starts at the local directory.

Usually when you open a command prompt program, open up under the default directory, example "c"\users\abc\". You can delete away the "Start in" directory found under the cmd prompt shortcut *.lnk 's properties.

If this *.lnk file is place under d:\mydir\myhome\. When you click on this lnk, it will open up a command prompt with the current directory which it is in, which is "d:\mydir\myhome\".

 

 

cmd prompt open current dir.lnk
Eclipse Java compiler setup

Another common problem I have is to set my eclipse to the latest compiler version after installing the eclipse IDE (integrated development environment). Although I think that the installation process is quite simple, but I couldn't managed to get it configured without help from my senior teacher. Perhaps I am still unclear about the java environment.

I wanted to get it cleared once and for all. Therefore I put in this effort to write down the steps here for the future. I know that I will probably forget once more.

To change the Java compiler version 6 for the project in eclipse
  1. Click on the project package.
  2. Go to menu Project>Properties
  3. Select "Java Build Path", click on the tab "Libraries".
  4. Select the JRE (Java runtime environment), and click edit.
  5. Choose Alternate JRE: jdk1.6.0_14
  1. if jdk1.6.0_14 option is not available, download and install the new JRE.
  2. Click on the button "Installed JREs..."
  3. Click "Add..."
  4. Go to JRE home directory:, click Browse...
  5. Go to the Java directory to find the installed JRE.
  6. Click ok all the way.
  1. Go to menu Project>Properties
  2. Select "Java Compiler", tick on "Enable project specific settings"
  3. Change the "Compiler compliance level" to 6.0.
Import existing Java project into Eclipse
- File>Import...>General>Existing Projects into Workspace
- Select the project folder, then click Finish
- click the file where your main() is, then click on run (Ctrl+F11)

The project can be previously compiled without any error and might still encounter error after importing the project to the new PC.
You might encounter the error for example:
"Implicit super constructor Object() is undefined".

Most issue is basically classpath set incorrectly.

- On the left project navigation panel, right click on the project's Properties (Alt+Enter)
- Select the option Java Build Path (left panel), and Libraries (right panel)
You should see a list of classpath as from your previous project.
There should be some icon with the red cross. Click on them and click Edit.
Resolving the error will solve the issue.
Very often the JRE version install on the new PC is different from the old.
Sometimes the path of the libraries are wrong after the import (due to absolute path in the classpath).


Importing *.jar and *.dll files to Eclipse Java project

 

1) copy the *.jar, *.dll to the folder "/project/lib"

2) right click the java project folder, and click on the properties

3) go to "Java Build Path>Libraries"

4) add the *.jar to the libraries

5) click on the *.jar package and link the required *.dll for the jar packages.

6) under Native Library location, select the directory path for the dll.

 

reference:

http://www.eclipse-blog.org/eclipse-ide/exportingimporting-jar-files.html


Installing plugin for Eclipse
1) Run Eclipse program.
2) Go to Help>Install New Software...>
     Work with: --All Available Sites--
     Key in the name of the plugin in the filter text field.
3) Select and install the plug-in.



Installing plugin for Eclipse
1) Copy the plugin to eclipse/plugins
2) Run Eclipse program with an option -clean, "D:\eclipse\eclipse.exe -clean"
3) The plugin feature should be installed when the Eclipse is running.
4) To check if plugin is installed, go to Help>About Eclipse>Installation Details>Plug-ins

Managing plugins using links
1) Create a folder "eclipse/links" in the eclipse installed directory.
2) Create a file *.link
3) File will contains the path to the plugin outside the eclipse installation directory.
    path=D:/thirdparty/wtp
4) The path should point to a directory which has a “eclipse” folder which in turn has “features” and “plugins” as sub-folders
references:
http://www.venukb.com/2006/08/20/install-eclipse-plugins-the-easy-way/

Setup environment path for WindowXP, Vista
1) Start>Settings>Control Panel>System>System Properties>Advanced>Click "Environment Variables"
2) Under "System Variables", select variable "Path"
3) Edit the path to include your desire path of your program.



Reference for Windows operating system environment variables. eg. %APPDATA%, %PROGRAMFILES%, %SYSTEMDRIVE%, %SYSTEMROOT%, %TEMP%, %USERPROFILE%
-Environment Variables in WinXP.pdf
Setup Eclipse src folder tree, package presentation to Flat or Hierarchical.
For easy viewing of the package in a folder view.
At the Package Explorer on the left panel, press Ctrl+F10. Select "Package Presentation", then choose "Hierarchical".
Useful Shortcuts for Eclipse

Must know

Ctrl+F11 -Run console
Crtl+Space -auto complete
Crtl+i -indent the code nicely
Ctrl+Shift+F -format code style
Ctrl+/ -comment/uncomment the selection of codes
Ctrl+Shift+O -Organise imports statement

Good to know

Ctrl+ -expand a method
Ctrl- -collapse a method
Ctrl+* -expand all the codes
Ctrl+Shift+* -collapse all the codes
Ctrl+Shift+(up/down) -navigate through the members of the class
Alt+(left/right) -scroll through places of visits

Ctrl+Shift+T search for Java class
Ctrl+Shift+R search for any files in the workspace

Alt+Shift+(up) -select chunck of codes within the brackets

Ctrl+Q go to the previous location of edit.
Ctrl+T pull down a list of related superclass and interface class

F3, go to declaration.

Unable to use, not so useful

Ctrl+F2 -Stop console
Crtl+1 -assist

Ctrl+click -go to declaration
Crtl+Shift+Space -assist
Ctrl+M -max coding space
Alt+(up/down) -shift code selection up/down
Ctrl+Shift+G -find name


 

 

more amazing shortcut key,

http://theshyam.com/2009/07/eclipse-productivity-shortcuts/
Formatter for your Java codes
Eclipse comes with it's standard coding style. I am not used to the way the brackets appears on the code. Eclipse allow customization for the coding style. And I have attached it for my reference.

- 2009-07-07 LSB style eclipse.xml

To customised coding format
Go to Project>>Properties>>Java Code Style>>Formatter>>Enable project specific settings>>New

 

//I don't like the following format
public void myFunction{
}

//My preferred format
public void myFunction
{
}


Editor Templates

2011-01-18 Eclipse template. What I have just learned today. They are the shortcut keyword to auto generate codes in Eclipse. After programming Java in Eclipse for so long, I started to become lazier and lazier, looking for shortcut. As short as possible. You may get boring typing the same while loop, delay function, data iterator. In fact, most of the time, I copy the codes from my old programs and do a bit of modification to fit the new program. If you are starting to get bore with typing all those codes again and again, I am sure you are going to like what I have learned today; Eclipse template.

Let's say you need to code your main method that we always have as our program entry. Type in the word "main" and then press Ctrl+Space. You will see a menu pop up indicating a text icon with the text "main - main method". Double click on this option and pop, your main method is completely generate in an instant.

type "main" & press Ctrl+Space

The main method is generated immediately .

That's cool. There are many code generating keyword which are already built in as default. Keyword for example, while, if, etc... The loops variables are auto generated. The best part is being able to customise your own code generation. I enjoy using those code generation using my way of writing the Java codes.

To do the customisation, you have to change the preference.
Go to Windows->Preferences->Java->Editor->Templates , and put in the following templates



 

Click on the button New, to start adding your own shortcut code generation.

For example, to generate the logger statement when I type "log"

Name: info or log, warn, error
Pattern:
logger.info("");
logger.warn("");
logger.error("");
 

Other example:

Name: logger
Pattern:
${:import(org.apache.commons.logging.Log,org.apache.commons.logging.LogFactory)}
protected static transient final Log logger = LogFactory.getLog(${enclosing_type}.class);


Inserting a variable within a literal string.
Name: var
Pattern:
" + a + "

Inserting a variable within a literal string enclosed within <>.
Name: <>
Pattern:
<" + a + ">

Inserting a variable within a literal string enclosed within "".
Name: var
Pattern:
\"" + a + "\"

Highlighting a variable to check if it is null.
Name: check null
Pattern:
if (${var== null) {
 ${word_selection}${cursor}
}

Name: iterator
Pattern:
Iterator<${type:argType(itr)}> ${itr:var(java.util.Iterator)} = ${hashObj:newName(hashmapObj)}.keySet().iterator();
while (${itr}.hasNext())
{
    ${type${eachitem:newName(item)} (${type}) ${itr}.next();
    ${cursor}
}

Get the name of this class.
Name: class
Pattern:
getClass().getSimpleName()

There are many example in the default template for your reference. The settings can be export/import. I have saved my template here for use.

lsb_editor_templates.xml

2016-05-22 editor_templates.xml


Code Templates

The code template is a bit similar to the editor templates. When eclipse help us to auto generate the setter or getter methods, it is actually using the code templates to do the job.

To do the customisation, you have go to.
Go to Windows->Preferences->Java->Code Style ->Code Templates

My code templates is as follows,

lsb_code_templates.xml


 

Making a *.jar library file from Eclipse

Consolidating all your library into a *.jar file from Eclipse.

Go to the project folder in Eclipse. Right click on the folder and select Export.
Select Java>Jar.
Click next to proceed to the next step.
Select the directories to be included in the jar file.
Type in the export destination file, and click finish to generate the jar file.

- how to make a jar file from Eclipse.pdf

 

Running a Java program

- To run a java class file "myJavaClass.class"
java myJavaClass

- To run a java jar file "myJavaJar.jar"
> java -jar myJavaJar.jar
The -jar option will choose the entry point (entry class) base on the main class specified in the manifest file.
Example of the entry class is MyPackage.MyClass, in the following manifest file content.

Manifest-Version: 1.0  
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: MyPackage.MyClass

Ensure that the program point of entry is specified in the manifest file.
In cases you want to run another main entry point from another class (not the one specified by the manifest),
you can issue the following command,

> java -cp myJavaJar.jar MyPackage.OtherClass

This allows you to run various main entry points which is written for each of your class.
Sometimes this is needed to individually test your class module.

> java -cp myJavaJar.jar MyPackage.MyClass
is equivalent to
> java -jar myJavaJar.jar

 

Using IKVM to convert Java into *.exe or *.dll file.

IKVM is a virtual software written in C# to runs java bytecode. Its main purpose is to convert allow Java source code to be reuse by the native Windows OS. This is achieve by converting the Java program into *.exe or *.dll executable which can be access from Visual C++/C#, Visual Basic, etc... programming language. The API provided by Java can also be available for access by other programming languages.

Installing
- download IKVM from this website "http://www.ikvm.net/"
- copy the folder to c:\ and begin using it.

Running a java class or jar file using IKVM virtual machine is like running the same java program from a JVM (Java Virtual Machine).

Running a java class file using JVM java myClassFile
Running a java class file using IKVM ikvm myClassFile
Running a java jar file using JVM java -jar myJarFile.jar
Running a java jar file using IKVM ikvm -jar myJarFile.jar
   
Compiling a java class file into a *.exe file ikvmc -target:exe myClassFile.class
Compiling a java jar file into a *.dll file ikvmc -target:library myClassFile.class
Compiling a java jar file into a *.exe file ikvmc myJarFile.jar
Compiling a java jar file into a *.dll file. ikvmc myJarFile.jar
  The command for compiling a jar file is the same. If the program entry class is specified in the manifest file in the jar, an *.exe will be generated, else *.dll file will be generated.
   


 

 
Eclipse will copy properties files from the resource folder to the bin folder. To prevent Eclipse from copying to the output bin, you can set from the preference,
Window->Preference, ->Java->Compiler->Building-> Output folder-> Filtered resources "*.properties, *.jar"