title: Guice and Reflections inside Jar File
body: <p>I am experimenting with Google Guice (3.0) and Google Reflections (0.9.6).</p>
  
  <p>I have the following files:</p>
  
  <p><strong>Operation.java:</strong></p>
  
  <pre><code>package com.company1.calculator;
  
  public interface Operation {
      public int apply(int a, int b);
  }
  </code></pre>
  
  <p><strong>Addition.java:</strong></p>
  
  <pre><code>package com.company1.calculator;
  
  public class Addition implements Operation {
      @Override
      public int apply(int a, int b) {
          return a + b;
      }
  }
  </code></pre>
  
  <p><strong>Other various "operation" classes spread across multiple packages.</strong></p>
  
  <p><strong>CalculatorModule.java:</strong></p>
  
  <pre><code>package com.company1.calculator;
  
  import com.google.inject.AbstractModule;
  import com.google.inject.multibindings.MapBinder;
  import org.reflections.Reflections;
  
  public class CalculatorModule extends AbstractModule {
      @Override
      protected void configure() {
          Reflections reflections = new Reflections("");
          MapBinder&lt;String, Operation&gt; map = MapBinder.newMapBinder(binder(), String.class, Operation.class);
          for (Class&lt;? extends Operation&gt; o : reflections.getSubTypesOf(Operation.class)) {
              map.addBinding(o.getSimpleName()).to(o);
          }
      }
  }
  </code></pre>
  
  <p><strong>Calculator.java:</strong></p>
  
  <pre><code>package com.company1.calculator;
  
  import com.google.inject.Inject;
  
  import java.util.Map;
  
  public class Calculator {
      private Map&lt;String, Operation&gt; operations;
  
      @Inject
      public Calculator(Map&lt;String, Operation&gt; operations) {
          this.operations = operations;
      }
  
      public void printCalculations(int a, int b) {
          System.out.println("Calculator: " + a + " " + b);
  
          for (String s : operations.keySet()) {
              System.out.print(s + ": ");
              System.out.println(operations.get(s).apply(a, b));
          }
      }
  }
  </code></pre>
  
  <p>And finally, <strong>App.java:</strong></p>
  
  <pre><code>package com.company1.calculator;
  
  import com.google.inject.Guice;
  import com.google.inject.Injector;
  
  public class App {
      public static void main(String[] args) {
          Injector injector = Guice.createInjector(new CalculatorModule());
          Calculator c = injector.getInstance(Calculator.class);
          c.printCalculations(3, 3);
      }
  }
  </code></pre>
  
  <p>After executing <strong>App.java</strong> inside of IntelliJ I get the following output, as expected:</p>
  
  <blockquote>
    <p>Calculator: 3 3<br>
    Modulus: 0<br>
    Division: 1<br>
    Multiplication: 9<br>
    Addition: 6</p>
  </blockquote>
  
  <p>However, when I package this application as a jar I only get the following output:</p>
  
  <blockquote>
    <p>Calculator: 3 3</p>
  </blockquote>
  
  <p>Why is this and how can I fix it? The closest thing I found was <a href="http://code.google.com/p/reflections/issues/detail?id=48" rel="nofollow">Issue 48</a>, but that says fixed as of May 2011. Surely that would have made it into Maven by now...</p>