简体   繁体   中英

Javax.servlet dependency does not work

I am creating a Gradle project in Intellij to improve my skills because I am learning. Previously, I created the same project with Maven and It is working well.

My project is this: Project

How you can see in the image " HttpServlet " is not recognised and I don't know why because I have the dependency and apparently everything is correct, here is my build.gradle with the dependency:

group 'com.aprendiendo.java'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}

In the first image It can be seen how in the Gadle Project menu -> Dependences folder is javax.servlet.... but however It does not work because I can't use anything of Java Servlet API (Classes, etc...), how you can see in the image of my project which I put before.

I have revised everything and I dont know what is happening.

I tried a similar setup on my IntelliJ version and everything actually ended up working ok. You can try NOT using the fully qualified name in the extension. Use

public class Servlet extends HttpServlet 

instead of

public class Servlet extends javax.servlet.http.HttpServlet

Then hover over HttpServlet with your mouse and see if IntelliJ autosuggest an import. If it does then you can then use Alt + Enter to complete the import. This may juice IntelliJ to recognize the dependency. You can also try Build --> Make Project to see if recognizes.

You may also want to check under File --> Project Structure and select the libraries node. Look and see if the dependencies are listed: javax:servlet-api:3.1.0, junit:junit:4.11, org.hamcrest:hamcrest-core:1.3.

I did only test this on a later version of IntelliJ 2016, but it should work. Feel free to post back comments/questions.

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