Can't locate import javax.inject.Inject package
Asked Answered
G

8

52

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?

Glottochronology answered 31/10, 2013 at 23:55 Comment(3)
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.Possing
When I use IntelliJ to search for jars, it tells me that there are no jars available for 'javax.inject.Inject'.Glottochronology
Turns out it was a combination of factors: 1.Turn on Annotations in your IntelliJ preferences: jetbrains.com/idea/webhelp/using-external-annotations.html 2.Include the inject jars: mvnrepository.com/artifact/javax.inject/javax.inject 3.ProfitGlottochronology
U
32

add this to your pom.xml

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>
Unaccustomed answered 21/3, 2016 at 13:53 Comment(0)
O
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).

Oversize answered 1/11, 2013 at 2:18 Comment(1)
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.Irina
C
11

put to gradle

implementation 'javax.inject:javax.inject:1'
Cretaceous answered 8/4, 2022 at 9:36 Comment(0)
T
10

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

Tripletail answered 16/4, 2020 at 8:53 Comment(0)
F
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.

Flamethrower answered 23/6, 2016 at 6:55 Comment(2)
have you an idea why it is compile with java 8 (don't need to this library) and not for Java 7 ?Paroicous
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.Synoptic
C
2

Just an FYI in case someone stumbles in here (like me) who is currently running a (mega) JakartaEE-upgrade-session:

Jakarta EE 10 moved a lot to jakarta-namespace - including javax.inject - see https://mvnrepository.com/artifact/javax.inject/javax.inject

The class Inject can now be found in the Maven dependency https://mvnrepository.com/artifact/jakarta.inject/jakarta.inject-api

Carl answered 13/2 at 18:1 Comment(0)
D
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')
    }
Denton answered 8/10, 2018 at 4:38 Comment(1)
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...Pleasurable
T
0

Thank you, Josef Vancura, for your answer!
Now that we have Gradle Kotlin DSL and Gradle Version Catalogs, we can do it like this:

In the version catalog, such as gradle/libs.versions.toml:

[versions]
# ...
javax-inject = "1"

[libraries]
# ...
javax-inject = { group = "javax.inject", name = "javax.inject", version.ref = "javax-inject" }

In the module's build file, such as app/build.gradle.kts:

dependencies {
    // ...
    implementation(libs.javax.inject)
}

And that's it, we should be ready to rock!

Tricostate answered 26/6 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.