48

I'm trying to implement Dagger as a dependency injector in an IntelliJ project, but my code is failing on:

import javax.inject.Inject;

Intellij is finding the 'javax' package, but not the 'inject' package, so it fails.

I am new to Android, so I apologize if this is a no brainer, but can anyone tell me why the inject package is not being found?

Arty
  • 859
  • 2
  • 12
  • 31
Indigo Nai
  • 531
  • 1
  • 4
  • 7
  • 1
    Make sure you have java EE 6 packages download, alternatively: on intelliJ, you can hit alt+enter and there will be an option such as "search the web for this package" and it will give you a list of jars that have that package. – Epicblood Nov 01 '13 at 00:09
  • When I use IntelliJ to search for jars, it tells me that there are no jars available for 'javax.inject.Inject'. – Indigo Nai Nov 01 '13 at 00:19
  • 1
    Turns out it was a combination of factors: 1.Turn on Annotations in your IntelliJ preferences: http://www.jetbrains.com/idea/webhelp/using-external-annotations.html 2.Include the inject jars: http://mvnrepository.com/artifact/javax.inject/javax.inject 3.Profit – Indigo Nai Nov 01 '13 at 03:12

6 Answers6

30

Dagger depends on JSR 330, the Java standard annotations which are used for dependency injection (think: @Inject, @Singleton, etc.).

This is a separate jar that you have to include. If you were using a build system with integrated dependency management (Maven, Gradle, Ant+Ivy, sbt) you'd get this for free. If you're still copying around jars then you have to add it manually.

You can download the latest jar from Maven central (at the bottom).

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • 4
    You should not, per maven guidelines, ever "get for free" transitive dependencies you are actually using in your own code, so I recommend importing the javax.inject artifact in any case, if you are using its annotations. – Christian Gruber Nov 01 '13 at 15:12
30

add this to your pom.xml

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>
Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59
Dwight Lisper
  • 435
  • 5
  • 9
8

Add the inject library directly from Maven like this:

  • File -> Project Structure -> Project Settings -> Libraries -> Add -> From Maven
  • Search for javax.inject:javax.inject:1
  • After it's been found click OK

enter image description here

donturner
  • 17,867
  • 8
  • 59
  • 81
4

In case if anyone using plain Java project not Maven or Gradle or e.t.c. You can download a separate Jar file from here Inject Jar file

and then add to your external libraries, in IDEA you can do that as follows: File -> Project Structure -> Libraries -> New Project Library (+)

Then find the path to library and job is done.

hasskell
  • 441
  • 3
  • 7
  • have you an idea why it is compile with java 8 (don't need to this library) and not for Java 7 ? – Aguid Oct 12 '17 at 11:38
  • 1
    This approach will not scale to real world projects and when working with a team. It is always preferred to use a dependency handling framework like maven or gradle. – René Jahn Oct 24 '19 at 14:16
3

put to gradle

implementation 'javax.inject:javax.inject:1'
Josef Vancura
  • 1,053
  • 10
  • 18
0

// dependency injection implementation "com.google.dagger:dagger:$rootProject.dagger2Version"

// dependency injection
    implementation "com.google.dagger:dagger:$rootProject.dagger2Version"
    implementation {
        exclude(group: 'javax.inject', module: 'javax.inject')
    }
  • This caused me so many headaches, This bit of code somehow breaks gradle and results in "java.lang.IllegalArgumentException: Cannot convert string value 'UNIFIED_TEST_PLATFORM' to an enum value of type 'com.android.builder.model.AndroidGradlePluginProjectFlags$BooleanFlag' (valid case insensitive values: APPLICATION_R_CLASS_CONSTANT_IDS, TEST_R_CLASS_CONSTANT_IDS, TRANSITIVE_R_CLASS, JETPACK_COMPOSE, ML_MODEL_BINDING)" and as you can guess, there is close to NONE solutions that I could find and I had to change my gradle version... – JustSightseeing Jan 01 '22 at 14:45