Practical
Embedded Java

Eclipse Tips


A practical engineering approach to using embedded Java in real-world applications.


...



Here are some techniques which we've found useful:

White space in paths - don't use it!
Setting up Eclipse Project Folders in a logical, consistent way
Eclipse Incremental Builds don't seem reliable, use an init target in Ant instead
Setting line widths and indents for code and comments
Getting a Java Package Perspective
Setting Target and Source - vital when using Eclipse for embedded targets such as JStik, JStamp, SaJe, TStik/TINI, SNAP
Error: java.lang.Object cannot be resolved. It is indirectly referenced from the required .class files
Eclipse/Ant Build Gotchas and how to fix them
Using Eclipse Java Classpath Variables to make your build.xml files highly portable and shareable



No white space in paths (2008 Jul 25)
Don't use white space in class paths, Windows paths, or any other paths. This means you should not install Java tools in, or below folders such as "Program Files". The same holds true in other cases such as image filenames - use underscores and mixed case rather than white space. Recent versions of some Unix-originated tools now seem to tolerate white space OK but I still don't like to use it unless there's a good reason.

Eclipse project folders

Here's how we do it at Systronix. This approach simplifies using code from public repositories such as java.net. It also eases sharing JAR files, aJile Jembuilder projects, and other aspects of a build.

  1. Create a PROJECTS folder in each programmer's local or network hard drive.This is the "sandbox" into which programmers check out files from our version control system (we use CVS). Periodically (at least daily if changes are being made), code is checked into CVS.
  2. For each project you wish to manage, create a meaningfully-named folder directly below the PROJECTS folder. This is the root folder which Eclipse uses for each defined Eclipse project. For example, JCX is a project.
  3. When you start a new Eclipse project (File>New>Project>Java Project), do not use the default "Project Contents" location. Use a subfolder below the PROJECTS folder. This Eclipse project folder contains:
    1. your Ant build.xml file
    2. Eclipse .project and .classpath files
    3. JemBuilder .ajp project files
    4. Subfolders for source, class, doc and other files as described below
  4. Within each Eclipse project, create subfolders:
    1. src (for source code)
    2. classes (for class files)
    3. doc (for Javadocs)
    4. html (for Amulet compiled HTML files)
    5. JStampRamBuild (this is where output from JemBuilder should go for a JStamp build which will execute from SRAM)
    6. TiniBuild for executable output from a TINI build
    7. JStikFlashBuild for output from JemBuilder for a JStik build which will execute from Flash
    8. SnapBuild for SNAP's executables (there is only one kind of SNAP build)

    Note that there are separate executable files for each specific embedded platform build, including RAM and Flash versions, as applicable.

eclipse folders screen capture
Here's a screen capture of a typical Eclipse project path, using the guidelines above. (Click on the image for a version which you can actually read.)

Compiling for JStamp, JStik or SaJe with Eclipse
See the tutorial http://www.practicalembeddedjava.com/tutorials/BlinkLevelOne.html for an example of setting up the Eclipse Java build path for aJile targets. The process is similar for TStik, TINI390 and SNAP.

Setting Eclipse Target and Source Options for Embedded Java Cross-compilation

Target 1.1 classfile compatibility


(Eclipse compiler option)
Eclipse Target 1.1 Option

For aJile and TINI hardware you must use the "target 1.1" class file compatibility option. Window->Preferences->Java->Compiler->Compliance and Classfiles and in my Eclipse the "Use default compliance settings" checkbox generates "1.1" compatible files.

Note that this has nothing to do with the JDK version or the Java compiler version you use - we use 1.4.2 but still generate "1.1" compatible class files.

You can do the same thing within an Ant build.xml file, within the javac task.

Target 1.1 classfile compatibility


(Ant build file option)
< target name="ajile_compile" depends="init">
< echo>javac should have the -target 1.1 switch</echo>
< javac target="1.1"
bootclasspath="${env.AJILE_CLDC}:${env.AJILE_CLDC}/../classes.jar"
srcdir="src" includes="**/*.java"
destdir="classes" />
< echo>Compilation Complete</echo>
< /target>

Here's a snippet showing just the -target 1.1 option in an Ant file, within the javac task.

You can invoke Ant from within Eclipse or from the command line.

Source 1.3 sourcefile compatibility
(for use with javac from JDK 1.5 or later)
(Eclipse compiler option)
Eclipse Target 1.1 Option
In the Window->Preferences->Java->Compiler dialogs, uncheck "Use Default Compliance Settings" and set Source Compatibility to 1.3 at the same time you set Target Compatibility to 1.1
Source 1.3 sourcefile compatibility
(for use with javac from JDK 1.5 or later)

< target name="ajile_compile" depends="init">
< echo>javac should have the -target 1.1 switch</echo>
< javac target="1.1" source="1.3"
... rest of file

Just add a source version compatibility option along with the target option in your Ant build file.

Configuring Eclipse
Tolerating "unreachable code" in your program It's common to comment out blocks of code or return prematurely to help with debugging. This creates blocks of what the java compiler considers "unreachable code". Unreachable code should be noticed, but should generate a warning, not an error. To control the unreachable code warning level in Eclipse, use Window>Preferences>Java>Compiler and select the Problems tab. You can choose Error, Warning, or Ignore. I recommend Warning.
Controlling line splitting in your code Window>Preferences>Java>Code Formatter and select the Line Splitting tab. The default is 80 columns but 100-132 is typically better. Use whatever fits your screen and printer.
Number of spaces per indent level Window>Preferences>Java>Code Formatter and select the Style tab. Four spaces per indentation is better than eight, but some people prefer two. Pick one value for your whole company and stick with it so that you can share code and have it look consistent on all programmer's machines. Never use tabs instead of spaces since hard tabs make reformatting and refactoring code much more difficult.
New project default folders I like to create new projects with src and classes subfolders. Click on Window>Preferences>Java>New Project. Select the "Folders" option in the "Source and Output Folders" dialog and change the "bin" folder default to "classes".
Perspectives I like the Package Explorer and Outline perspectives on the left of my screen, with the Outline at the bottom.

Using Code Templates (or "Formatter Profiles") in Eclipse
Sample code generation templates XML file for Eclipse 2.X
SystronixTemplate.xml
2006 Apr 10
Here are the code generation templates we use at Systronix. We're still refining these, with the goal of making them as clear as the Sun Javadocs, but also with feaures to support our use of CVS, our code release guidelines, etc. Download and save the template file. To import it into Eclipse, use the Window>Preferences>Java>Code Style>Formatter, then click the Import button, then browse to the template file and select it. Eclipse 3 started calling these "profiles" instead of templates.

Generating Javadocs within Eclipse
Eclipse has a Javadoc generation wizard which will also create a javadoc.xml file for you. First, be sure that the Eclipse project for which you want Javadocs is selected (I highlight it in the Package Explorer perspective). Use Project>Generate Javadoc to start the wizard.

Eclipse/Ant Gotchas - Using Ant within Eclipse, and from a command line

Eclipse has Ant integration but there are some gotchas which have bitten us. There are actually three ways to build a program if you use Eclipse and Ant, and you don't necessarily get the same results from all three. In fact you may not realize that there are three distinct build sequences, and this can give you unexpected results.

In the enviroment assumed here, there is an Eclipse Java installation (with Ant and other plugins), a Sun JDK installation, and an Ant installation (apart from the Eclipse Ant plugin). In other words, it's the tool installation described here.

This whole issue of "surprise - there are actually three different ways my project is building" still seems confusing to me, so don't be dismayed if it seems confusing to you too. So be prepared to be a bit perplexed and read the box to the right.

Here are the ways you can build a program using Eclipse and Ant:

  1. When Eclipse builds class files (every time you save a file, or use the Eclipse "rebuild project" or "rebuild all" menus, it's not clear to me exactly where it gets the java compiler. Does it have its own (I think so). So I think this uses the incremental-compile-and-syntax check tool built into Eclipse. These compilation settings are in the Eclipse Project->Properties->Java Compiler menu and can use global workspace settings or per-project settings.
  2. When you select an Ant build file in Eclipse, right-click it and select "run Ant", you are using Eclipse's Ant plugin and the java compiler com.sun.javac.Main.class, along with any explicit or implicit Ant build file parameters. Note that this Ant build does *not* use the same java compiler as the Eclipse builds. You must include the JDK tools.jar file in your system classpath, or explicitly add it to Ant in the Eclipse Windows->Preferences->Ant->Runtime menu in the classpath tab (see this screen shot). You are using the Eclipse Ant plugin, not some other Ant which you have on your system. The System environment settings ANT_HOME and JAVA_HOME don't appear to be used by Eclipse and Ant.
  3. When you run Ant from the command line, you are using your System path to find a java compiler (in this case javac.exe, not a .class file), and your system environment variables and classpath settings
Include a tools.jar file from your JDK in your system classpath and/or in the Eclipse Ant runtime settings. This prevents the Eclipse/Ant "cannot find javac" on your system. In any case, Eclipse has its own Ant plugins, and Eclipse does not use your system environment PATH variable to find anything, including a java compiler, javac.exe. Instead, Eclipse/Ant must be able to find "com.sun.javac.Main.class" in a jar file or folder, and it will only search your system classpath or in Eclipse project settings to find this compiler class.. The only place this class seems to exist is in %JAVA_HOME%/lib/tools.jar which in my case is the path D:\j2sdk14\lib\tools.jar. So one secret to getting Ant builds to work within Eclipse is to add tools.jar to your Eclipse Ant settings (see #2 above and the screen shot there).
Always clean your output folders as part of your build process. This prevents you from doing a build with old class files or files left over from an alternate build. If you don't do this, you may find that Eclipse was generating class files using its default build process. Then later, when you attempt to run an Ant build process, Ant will report that the class files are up to date and quietly *not* build them. So any bootclasspath errors in your Ant build file will go undetected, and you will be puzzled when others report that they cannot get your build file to work when they run it from the command line or from a non-Eclipse environment. Add a delete task to your Ant build init area, like this:
<echo>Cleaning build and output folders</echo>
<delete>
     <fileset dir="classes" includes="**/*.class"/>
</delete>

Use Eclipse Java Classpath Variables to refer to your tool and library paths (2004 July 19)

Make your local cross-compilation paths relative to Eclipse Java Classpath Variables. This provides the same portability and easy maintainability as System Environment Variables (see setting up environment variables). Eclipse does not use your System Environment Variables, and neither does Ant when executed within Eclipse (but Ant executed from a command line does). Setting these Eclipse Java Classpath variables to be identical to your computer's System Environment Variables makes all three builds a lot more compatible.

This also lets you install tools such as Dallas TINI runtime in folders such as TINI1.12, TINI1.13, etc, and refer to them in a given Eclipse project by changing a single project variable.

Set these up in the Eclipse Window->Preferences->Java->Build Path->Classpath Variables menu (see this screen shot). Now you can have a local hard disk folder such as "D:\TINI1.12" and refer to it as TINI_HOME both within Eclipse and within your Ant build files. So far, we've just created the variable -- it hasn't been actually used.

Now that the Eclipse Java Classpath Variable is set up, you need to use it in your Eclipse project. This must be done on a project-by-project basis (I haven't discovered how to make it apply to ALL Eclipse projects by making it an Eclipse Workspace setting). Open the Eclipse Project->Properities menu, select the Libraries tab and then click the Add Variable Button. A New Classpath Variable Entry box will popup. Select the TINI_HOME variable and click the Extend button (screen shot). Browse to a subfolder which you want to make relative to the Classpath Variable (screen shot) and select it. Now you have a new Java Build Path Library which is relative to your new Eclipse Variable (screen shot).

In this example we created a library path for tiniclasses.jar which is relative to our TINI_HOME Eclipse Java Classpath Variable. Now we can redirect that Eclipse project to a newer or older TINI runtime just by changing that one Eclipse Java Classpath Variable in one place.


Eclipse error: The type java.lang.Object cannot be resolved. It is indirectly referenced from the required .class files (2005 Sep 09)

This error popped up today when I was checking out projects from a CVS respository into a new, clean Eclipse workspace.This error had me going for a while since the projects in question built fine on another PC, and built fine in the new workspace, using Ant instead of Eclipse. It turned out that the reason the base class java.lang.Object could not be found is because the new workspace did not have Eclipse variables set, and they were referenced by the checked-out project. So in the project properties Build Path I'd see Classpath variables such as AJILE_HOME and JAVA_LIB being used to reference required libraries, but in fact Eclipse had no idea where "AJILE_HOME" was, and therefore could not find the CLDC base classes. Once I created the Eclipse classpath variables and rebuilt the project, the errors went away.


Getting a Java Package Perspective (2006 Mar 25)

The Eclipse Java Development Tools (JDT) include a perspective designed for working with Java projects. It consists of an editor area and the following views:
Package Explorer - to open this, use Window->Show View-> Other->Java->Package Explorer
Hierarchy - to open this, use Window->Show View-> Other->Java->Hierarchy
Outline
Search
Console
Tasks


Setting code and comments line widths and text wrapping (2006 Apr 10)

It's not terribly obvious how to control Eclipse line width and line wrapping in your Java source files. Here's how and where:

  1. Comment width and line wrapping is set in Preferences->Java->Code Style->Formatter, then click on the Edit button and select the Comments tab. I like Line Width for Comments to be 120.
  2. Code line wrapping is set nearby, in Preferences->Java->Code Style- >Formatter, then click on the Edit button and select the Line Wrapping tab. I like a line width of 120 and indent size of 4.
  3. Indentation is set separately, in Preferences->Java->Code Style- >Formatter, then click on the Edit button and select the Indentation tab. I like an indent size of 4, consistent with the Line Wrapping indent setting.
  4. As if that's not enough, you can also set printer margins, tab size, etc, in Preferences>General>Editors>Text Editors where I set the Displayed Tab Width to 4 and Print Margin Column to 120 or more. You can also check the Show Print Margin box to get a faint vertical line at the printer margin column

Remember to save these in your code template!

Then to automatically buff up your code appearance, save your file first and then use the main task bar Source>Format option. I'm not aware of a way to "unformat" so this is why it's a good idea to save first, so you can cancel out of any drastic surprises and try again.

A possible bug? (2006.04.10) In Eclipse 3.1.0, code formatting applies to "single line" comments ( // this is an example) when they are wider than the current comment margin, but not when they are narrower. Example: I applied Source>Format to a .java file and it made all my single line (and multi-line) comments fit within 80 columns. Yuck. I changed the comment column width to 120, reapplied the Format and only the multi-line comments changed. Hmmm, rather annoying.


Eclipse 'Incremental Builds' don't seem reliable (2006 Apr 11)

By default Eclipse performs and incremental build. This just doesn't seem to work reliably with the aJile tools. Admittedly that sounds purely anecdotal. So in my Ant build.xml file I always delete all class files and the aJile binaries so that a complete, 'clean' build is always forced. Here's the Ant init target to do this.

<echo>Cleaning build and output folders</echo>
<!-- purge all .class and key aJile binaries so we can never have an invalid build -->
<delete>
<fileset dir="classes" includes="**/*.class"/>
<fileset dir="." includes="**/build.bin, **/load.sod, **/JVM0.*"/>
</delete>

Note that this action deletes *all* aJile binaries in a project's subfolders. I typically have multiple binary output folders in a project - a different output folder for unit tests, sample application tests, and different folders for RAM and Flash builds, so the above action deletes the binaries from all of them.

 

.


 
Systronix® 939 Edison St, Salt Lake City, Utah, USA 84111
Tel +1-801-534-1017, Fax +1-801-534-1019
contact us     Time Zone: MDT (UTC-6)
 

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
Systronix is independent of Sun Microsystems, Inc.
TStik, JStik, JCX, JStamp, JSimm, JDroid, and JRealTime are trademarks of Systronix, Inc.
1-Wire, iButton and TINI are trademarks of Dallas Semiconductor
Simmstick is a trademark of Dontronics
LEGO® is a trademark of Lego A/S, Denmark