Tag Archives: java

Set up JAVA_HOME to track Java Preferences.app on Mac OS X

/Applications/Utilities/Java Preferences.appMac OS X’s Java Preferences.app has a pane for switching between versions of the JDK, but I just found out from a coworker (thanks, Mike!) that you can make your shell match that preference easily — just add this to your ~.bashrc:

export JAVA_HOME=$(/usr/libexec/java_home)

If you change your JDK priority preference, you’ll need to re-source your ~/.bashrc or just open a new terminal window.

How to serve a transparent 1×1 pixel GIF from a servlet

The first issue was how to build the smallest possible byte array that represents a 1×1 GIF. Using ImageMagick piped to base64 made it easy to embed into java code:

convert -size 1x1 xc:transparent gif:- | base64

At servlet load time, un-base64 the gif back into the byte array:

import org.apache.commons.codec.binary.Base64;
...
private static final String PIXEL_B64  = "R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
private static final byte[] PIXEL_BYTES = Base64.decode(PIXEL_B64.getBytes());

in your handle method, then write those bytes to the output stream:

httpServletResponse.setContentType("image/gif");
httpServletResponse.getOutputStream().write(PIXEL_BYTES);

How to increase maven heapspace in hudson builds

If your maven-built project fails in hudson (especially when you’re using the assembly plugin) and it isn’t a compile or test failure, check the console output. If it says “java.lang.OutOfMemoryError: Java heap space”, you need to configure your hudson job to add heap space to the maven process.

  1. Navigate to your hudson job,
  2. click Configure,
  3. scroll down to the Build section, and
  4. click the Advanced button.
  5. Enter this into MAVEN_OPTS: -Xmx512m -XX:MaxPermSize=128m

Continue reading How to increase maven heapspace in hudson builds

How to set up your Maven2 POM to support Java5

Seeing this?

$ mvn compile
...
generics are not supported in -source 1.3
(try -source 1.5 to enable generics)

You need to tell the maven-compiler-plugin to use java 1.5. Add this to your pom.xml:

<project>
...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

IDEA versus Eclipse

IDEA has been giving me grief lately. Fed up with mysterious pauses, bugs in svn moves, and crashes even after performing cache-deletion voodoo in IDE, I’m back to trying Eclipse.

Keystroke differences and functionality differences have been the major stumbling blocks. The things I miss from IDEA:

  • Refactor > Safe delete
  • A single keystroke that does svn update for the whole project
  • smart-complete that builds anonymous inner classes with the methods stubbed out automatically (I found the secret — ctrl-space after typing “new”, the interface, then left paren)
  • smart-complete that only offers classes or fields that are semantically correct (rather than eclipse’s string-matching-based complete). This is my most frequent grief.
  • The ability to group together sets of files for committing (but I think Mylyn does this) (Switch to the SVN Repository perspective, and open the Synchronize window)
  • The ability to rollback or make changes to files while still in the commit window (again, you can surf your pending changes in the SVN Repository’s “Synchronize” window)
  • There are bugs in Refactor > Move — not all string or spring XML references get fixed properly.
  • The code formatting preferences in Eclipse are much less flexible than IDEA’s.
  • I haven’t found the keystroke yet to “go to next error/warning”.
  • Spring IDE gets confused if the XML file isn’t fairly well formed. IDEA seems to still be OK if the file is in tatters.
  • ctrl/cmd-clicking targets in ant build files don’t take you to the target definition. They take you to a nearby matching string, which may or may not be the target.

Things that I found that work better in eclipse:

  • Eclipse kicks IDEA’s booty up and down the street when it comes to performance. Eclipse startup time for my large-ish work project: 19 seconds. IDEA, opening the same project: 2 minutes, 25 seconds. That’s after an initial launch and quit, so they both should be using only cached-in-RAM files. Eclipse never pauses while typing. Ever. IDEA momentarily wedges every few seconds.
  • Projects in your workspace (IDEA calls them “modules”) can be “closed” temporarily when you aren’t actively working on them. That saves memory and recompilation time.
  • You can run automatic code-cleanup fixes on save (and only apply those fixes on the lines of code that you touched)
  • The templates support auto-imports and auto-static-imports (fancy!)
  • IDEA has this artificial partitioning of file types in the project tree based on what facets are enabled in a module — there’s no simple filesystem tree widget for me to find, say, the web.xml that lives in ../web/WEB-INF/. IDEA shoves it into a “libraries” root node. Eclipse has both a project explorer and a package explorer that don’t do this weirdness.
  • Creating new JUnit tests in Eclipse is sane and intuitive. IDEA doesn’t have a junit test plugin that worked reliably for me.

Things that are a blessing and a curse:

  • Eclipse has this weird “it’s not real until you hit Save,” whereas you never click save in IDEA. A bunch of things (incremental compilation and spring validation) only happen after Save All. But you get used to that.
  • Eclipse has very flexible window management — far exceeding IDEA’s. You can dock, stack, drag, quick-hide, pile, and generally get very confused or very satisfied (depending on your experience level) with where windows are. Is an editor on the wrong side? Drag the tab where you want it. IDEA’s editor tabs on the mac simply don’t work — after N tabs are shown, the other tabs disappear, and you can activate an editor where the tab isn’t visible or in focus. So at least Eclipse’s tabs seem to be bug free.
  • Eclipse update sites that are external to eclipse.org — I added a couple plugins that destabilized things to where I couldn’t bring any window up without an exception being raised. I ended up having to start from a new bare-metal install again, but my project settings were picked up (including the new keymaps) so that wasn’t too bad.

Updated May 7th with some of the solutions I’ve come across.

Simple Log4J eclipse template

Do you use eclipse and log4j? Do you have a template to add a static Logger instance in classes? Do you have to manually add the import? HA! NO MORE!

Under Preferences > Java > Editor > Templates, click New…

Give the template a name (like “logger”).

Use this for the Pattern:

${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);

Click OK, OK, then incant the template by typing the name of the template and invoke Content Assist (ctrl-space on most platforms).

The import line will add org.apache.log4j.Logger to your imports automatically if it doesn’t exist already. Nifty, eh?

java rake db:migrate implementations

I was surprised that there were so many libraries that do the same thing — manage change in  your database schema. Here’s what I’ve found so far:

  • Liquibase (migrations are specified in db-generic XML)
  • migrate4j (migrations must be writted in java, and has “down” and “up” support)
  • AutoPatch (“down” and “up”, with both .sql and .java support)
  • dbmigrate (no “down” — only “up”, with both .sql and .java support, but no test coverage)
  • c5-db-migration (nice integration with maven)

I’ve used a heavily patched version of dbmigrate on my last project at work, and it’s doing what it’s supposed to. I’ve only read documentation for the other projects.

Update March 2010:

dbmigrate is abandonware that doesn’t have any unit test coverage. I’d suggest looking at c5-db-migration.