Search code examples
javamavenjavafxguice

Cannot find symbol with Guice's method createInjector


I am getting a weird error with my project, I added Guice to my dependencies in the POM.xml, everything seems fine the IDE gives the suggestions an all, even when I was writing the code to start the Injector it suggested me the method I wanted to use, but later it marks it in red, saying it cannot be found, I have never experienced something like this. Maybe I am forgetting something. Can anyone help me?

My project is a maven JavaFX desktop app.

This is the output of the error

/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/ochamo/Documents/Proyectos/apde-alumnus/APDEAlumnus "-Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=52291:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1 com.zenjava:javafx-maven-plugin:8.8.3:jar
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building APDE-Alumnus 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> javafx-maven-plugin:8.8.3:jar (default-cli) > [jfxjar]package @ APDE-Alumnus >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ APDE-Alumnus ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 10 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ APDE-Alumnus ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 13 source files to /Users/ochamo/Documents/Proyectos/apde-alumnus/APDEAlumnus/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/ochamo/Documents/Proyectos/apde-alumnus/APDEAlumnus/src/main/java/edu/apde/alumnus/main/Main.java:[22,47] cannot find symbol
  symbol:   class createInjector
  location: class com.google.inject.Guice
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.584 s
[INFO] Finished at: 2019-04-03T12:10:32-06:00
[INFO] Final Memory: 21M/309M
[INFO] ------------------------------------------------------------------------

Here is the Guice dependency in the pom.xml

<dependency>
   <groupId>com.google.inject</groupId>
   <artifactId>guice</artifactId>
   <version>4.2.1</version>
</dependency>

The code I am using to bootstrap my main class in

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

       try {
           final Injector injector = new Guice.createInjector(new MainModule()); // this is the source of the error It cannot find createInjector.

           FXMLLoader fxmlLoader = new FXMLLoader();
           fxmlLoader.setLocation(Main.class.getResource("/fxml/main/main-view.fxml"));
           System.out.printf("location: " + fxmlLoader.getLocation());
           GridPane root = fxmlLoader.load();
           Scene scene = new Scene(root);

           primaryStage.setScene(scene);
           primaryStage.setTitle("Login");
           primaryStage.show();
       } catch (IllegalStateException ex) {
           ex.printStackTrace();
       }
    }
}

Solution

  • The createInjector method is a static method (see the Javadocs)...

    public static Injector createInjector(Module... modules) {
        return createInjector((Iterable)Arrays.asList(modules));
    }    
    

    When you attempt to invoke it like this ...

    new Guice().createInjector(...)
    

    ... you are attempting to invoke an instance method and this does not compile since there is no instance method named createInjector in the Guice class..

    Instead, you should invoke it like this:

    Guice.createInjector(...)
    

    To be clear; instead of invoking new Guice() you just invoke on Guice.

    For background:

    • An instance method must be invoked on an instance of its class; to invoke a instance method, you must create an instance of the class in which the method is defined. For example: new Foo().doIt()
    • A static method is invoked without creating an instance of its class; to invoke a static method you just reference it by its class name. For example: Foo.doIt()