Getting started with Google Guice

This is just a little tutorial that shows you what JARs you need to write some code that links to the Google Guice libraries at runtime and design time. is doing a series of articles on Spring development, the first of which started off with how to configure the most basic environment possible that would support the development and testing of Spring based applications. Of course, Spring isn’t the only game in town when it comes to Dependency Injection(DI) and Inversion of Control (IoC). In fact, the sexy newcomer is Google, or more specifically, Google Guice. So, we thought it would be a good idea to go through the exact same tutorials that are being done with Spring, and show you how you could do the exact same stuff with a far superior dependency injection tool like Guice.

You might want to check out these updated IoC tutorials and dependency injection examples...

TheServerSide has been updating its resources in terms of the Spring framework, Java EE contexts and various different approaches to inversion of control (IoC) and dependency injection (DI). Check out these newer resources on the topic:

Getting Started with Google Guice

So, how do you get started with Google Guice? Well, first you download it. The current URL is as follows:

Of course, these URLs tend to change as soon as articles get published that reference them, but you should be able to find the download somewhere at

The only download you want is the file. Every other file is highly infectious and contagious.

The Guice JAR Files

Using your favorite zip utility, unzip the download into a folder that you’ll be able to find on your computer. My preference is to unzip to a folder named _guice off the root of C:\.

I usually like moving all of the various jar files I need to a special lib folder, but all of the required jar files, and even a few that I don’t think are going to be required at all, like the struts2 plugin jar file, are all nice and neatly tucked away in this _guice folder, so I’m going to leave things just the way they are. Just let it be known that these JAR files are the bytecode embodiment of the Guice framework.

Writing Some Guice Code

So, now I just want to write some extremely simple code that will make sure that my Java runtime environment can link to these Guice JARs. (Say that five times fast.). I’m assuming you can afford one of those fancy-dancy IDE tools like NetBeans or Eclipse, and that you have some knowledge of configuring a project and a classpath in those expensive development tools of yours. All I can afford is the JDK, which is installed in my _jdk1.6 directory off the root of C:\. I’m hoping that if I can get Guice to work using nothing more than a command prompt, Emacs and a JDK, that getting this working in a marvelous IDE should be a lead pipe cinch for you. To code the old-school way, I’ve created a subset of folders off my C:\ drive that looks like this:


Google Guice Code

And smack dab in that guice folder, I’m going to create a class called GumRunner. In the previous example, I created a class called SumRunner because I was using Spring. It just seems appropriate that this class should be called GumRunner out of respect for Guice.

The GumRunner Class

My GumRunner class is slender enough to make Kate Moss want to diet. All it does is import a key Guice class, create an instance of a Guice injector in the main method, and then do a println on that object. There’s no DI (Dependency Injection), there’s no IoC (Inversion of Control) and there’s certainly nobody here from the IOC (International Olympic Committee). All we have are a few simple lines of code that ensure that you can link to important Guice components at design time and runtime; and really, if you can’t do that, what’s the point of doing a bunch of other stuff?

package com.mcnz.guice;

public class GumRunner {

   public static void main(String args[]){

      Injector injector = Guice.createInjector();


Compile and Run That Guice Code

I compile my code by issuing the following command:

C:\_mycode>c:\_jdk1.6\bin\javac -classpath "C:\_guice\*" C:\_mycode\com\mcnz\guice\*.java

With successful compilation, I get absolutely no confirmation messages telling me how wonderful and clever I am. However, I do get a new .class file in my \guice folder.


I then run my code with the following command:

C:\_mycode>c:\_jdk1.6\bin\java -classpath "C:\_guice\*";C:\_mycode com.mcnz.guice.GumRunner

The output is completely incomprehensible gobbledygook:

Injector[bindings=[Key[, annotation=[none]]=Bindi

ngImpl[key=Key[, annotation=[none]], source=com.g

oogle.inject.Guice.createInjector(, provider=Provider<Injector>],

Key[type=java.util.logging.Logger, annotation=[none]]=BindingImpl[key=Key[type=j

ava.util.logging.Logger, annotation=[none]],

teInjector(, provider=Provider<Logger>], Key[

t.Stage, annotation=[none]]=BindingImpl[key=Key[, an



Here's a look at my DOS prompt, just in case you're into those types of things:

And That's It! Now, Onwards and Upwards!

Sure, it's gobbledygook, but gobbledygook or not, a nice little printout like this tells me that my environment is configured, I’m properly linking to the Google Guice JAR files, and I’m ready to move onto the next step, and that is performing some DI and IoC with my new environment. Stay tuned for more Guice tutorials that will do just that, not to mention a bunch of Spring tutorials on the same subject that will make it really fun and easy to compare the two IoC containers.

You should follow Cameron McKenzie on Twitter: @cameronmcnz

Interested in more articles and opinion pieces from Cameron McKenzie? Check these out:

Recommended Books for Learning Spring

Spring in Action  ~ Craig Walls
Spring Recipes: A Problem-Solution Approach  ~ Gary Mak
Professional Java Development with the Spring Framework ~ Rod Johnson PhD
Pro Java EE Spring Patterns: Best Practices and Design Strategies ~ Dhrubojyoti Kayal


Next Steps

New to Git and distributed version control? Here are some Git examples and Jenkins-Git integration tutorials designed to help you master the popular source code versioning tool.

Dig Deeper on Front-end, back-end and middle-tier frameworks

App Architecture
Software Quality
Cloud Computing