简体   繁体   中英

javax.inject does not exists

To investigate troubles with javax.inject I have created minimalistic Gradle project in IntelliJ IDEA.

It contains Main.java only:

import javax.inject.Scope;
public class Main {
    public static void main(String[] args) {}
}

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
}
apply plugin: 'java'
dependencies {
    compileOnly 'javax.annotation:jsr250-api:1.0'
}

and settings.gradle

rootProject.name = 'test'

Building the project I got

Error:(1, 20) java: package javax.inject does not exist

What's wrong with javax, why it doesn't exists??

Hmm, it seems the problem is not specifically with javax. Tried to add different dependencies from mavenCentral, no one works. Like Gradle does not load external libraries at all.

Solved

The problem was with repositories - it must be outside of buildscript .

只需带上javax.inject依赖

implementation 'javax.inject:javax.inject:1'

Compile only dependencies are distinctly different than regular “compile” dependencies. They are not included on the runtime classpath and are non-transitive, meaning they are not included in dependent projects. This is true when using Gradle project dependencies and also when publishing to Maven or Ivy repositories. In the latter case, compile only dependencies are simply omitted from published metadata.

see https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM