Setup Eclipse

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.

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
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
{
}


Useful Shortcuts for Eclipse

Ctrl+F11 -Run console
Ctrl+F2 -Stop console
Crtl+1 -assist
Crtl+Space -auto complete
Crtl+Shift+Space -assist
Ctrl+M -max coding space
Alt+(left/right) -scroll through history file
Alt+(up/down) -shift code selection up/down
Alt+Shift+(up/down) -select chunck of codes with the brackets
Ctrl+click -go to declaration
Ctrl+Shift+F -format code style
Ctrl+Shift+G -find name
Ctrl+Shift+O -Organise imports statement
Ctrl+/ -comment/uncomment the selection of codes

 

 

more amazing shortcut key,

http://theshyam.com/2009/07/eclipse-productivity-shortcuts/

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: log
Pattern: logger.info("");

 

Other example:

Name: getLog
Pattern: protected static transient final Log logger = LogFactory.getLog(${enclosing_type}.class);

Name: while
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}
}

 

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

 

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