IntelliJ won't recognize some imports from gradle project












1















I am working on a Gradle project in IntelliJ and some packages are not recognized by IntelliJ. When I go to Project Structure -> Modules -> <my module> -> Dependencies, the jar file that contains these packages is there. (I've examined the jar file with jar tfv <file> to make sure that the classes in question are in the file.)



The classes in question are in red and when hovering over them, I get errors like "Cannot resolve symbol 'somepackagename'" or "Cannot resolve symbol 'SomeClassName'".



However, the gradle project compiles just fine from the command line.



I've tried all the existing suggestions I could find, but so far none have helped. Primarily, I've already tried:




  • Deleting the .idea folder and re-importing

  • Reimporting the project from the root build.gradle file

  • Clicking on the "Refresh all gradle projects" button

  • Upgrading IntelliJ

  • Clicking on "Invalidate Caches and Restart"


What can I do to get IntelliJ to recognize the packages in these jar files?










share|improve this question























  • Please report at youtrack.jetbrains.com/issues/IDEA with the sample project to reproduce attached.

    – CrazyCoder
    Nov 14 '18 at 20:50











  • @CrazyCoder I will do that if I can get a sample project together, but I was hoping that this may be a common enough problem that someone (such as yourself) might be able to answer here for others to see. I saw that your top answer was about a very similar problem: stackoverflow.com/questions/5905896/… Are there any additional steps you would recommend to rule out this being a bug?

    – Chris Middleton
    Nov 14 '18 at 21:14











  • Since you've already tried all the common solutions and they didn't work, your issue is probably different and is not something common that can be answered right away just from the description.

    – CrazyCoder
    Nov 14 '18 at 21:16











  • @CrazyCoder Fair enough. Since I am a gradle and IntelliJ newbie, I thought maybe I was just missing something obvious, but even if that's the case, you're right that it may be specific to my situation. I will attempt to report at the site.

    – Chris Middleton
    Nov 14 '18 at 21:18











  • @CrazyCoder I came up with a workaround below that I'm honestly surprised works. The details are below. I'm now telling Gradle to consider all the files in the Gradle cache directory as a compile dependency, and it then picks up on the jars that it wouldn't before, even though it's automatically finding other jars in that same Gradle cache. Do you have any thoughts as to why the below workaround would work and what cleaner solution there might be?

    – Chris Middleton
    Nov 15 '18 at 17:32
















1















I am working on a Gradle project in IntelliJ and some packages are not recognized by IntelliJ. When I go to Project Structure -> Modules -> <my module> -> Dependencies, the jar file that contains these packages is there. (I've examined the jar file with jar tfv <file> to make sure that the classes in question are in the file.)



The classes in question are in red and when hovering over them, I get errors like "Cannot resolve symbol 'somepackagename'" or "Cannot resolve symbol 'SomeClassName'".



However, the gradle project compiles just fine from the command line.



I've tried all the existing suggestions I could find, but so far none have helped. Primarily, I've already tried:




  • Deleting the .idea folder and re-importing

  • Reimporting the project from the root build.gradle file

  • Clicking on the "Refresh all gradle projects" button

  • Upgrading IntelliJ

  • Clicking on "Invalidate Caches and Restart"


What can I do to get IntelliJ to recognize the packages in these jar files?










share|improve this question























  • Please report at youtrack.jetbrains.com/issues/IDEA with the sample project to reproduce attached.

    – CrazyCoder
    Nov 14 '18 at 20:50











  • @CrazyCoder I will do that if I can get a sample project together, but I was hoping that this may be a common enough problem that someone (such as yourself) might be able to answer here for others to see. I saw that your top answer was about a very similar problem: stackoverflow.com/questions/5905896/… Are there any additional steps you would recommend to rule out this being a bug?

    – Chris Middleton
    Nov 14 '18 at 21:14











  • Since you've already tried all the common solutions and they didn't work, your issue is probably different and is not something common that can be answered right away just from the description.

    – CrazyCoder
    Nov 14 '18 at 21:16











  • @CrazyCoder Fair enough. Since I am a gradle and IntelliJ newbie, I thought maybe I was just missing something obvious, but even if that's the case, you're right that it may be specific to my situation. I will attempt to report at the site.

    – Chris Middleton
    Nov 14 '18 at 21:18











  • @CrazyCoder I came up with a workaround below that I'm honestly surprised works. The details are below. I'm now telling Gradle to consider all the files in the Gradle cache directory as a compile dependency, and it then picks up on the jars that it wouldn't before, even though it's automatically finding other jars in that same Gradle cache. Do you have any thoughts as to why the below workaround would work and what cleaner solution there might be?

    – Chris Middleton
    Nov 15 '18 at 17:32














1












1








1








I am working on a Gradle project in IntelliJ and some packages are not recognized by IntelliJ. When I go to Project Structure -> Modules -> <my module> -> Dependencies, the jar file that contains these packages is there. (I've examined the jar file with jar tfv <file> to make sure that the classes in question are in the file.)



The classes in question are in red and when hovering over them, I get errors like "Cannot resolve symbol 'somepackagename'" or "Cannot resolve symbol 'SomeClassName'".



However, the gradle project compiles just fine from the command line.



I've tried all the existing suggestions I could find, but so far none have helped. Primarily, I've already tried:




  • Deleting the .idea folder and re-importing

  • Reimporting the project from the root build.gradle file

  • Clicking on the "Refresh all gradle projects" button

  • Upgrading IntelliJ

  • Clicking on "Invalidate Caches and Restart"


What can I do to get IntelliJ to recognize the packages in these jar files?










share|improve this question














I am working on a Gradle project in IntelliJ and some packages are not recognized by IntelliJ. When I go to Project Structure -> Modules -> <my module> -> Dependencies, the jar file that contains these packages is there. (I've examined the jar file with jar tfv <file> to make sure that the classes in question are in the file.)



The classes in question are in red and when hovering over them, I get errors like "Cannot resolve symbol 'somepackagename'" or "Cannot resolve symbol 'SomeClassName'".



However, the gradle project compiles just fine from the command line.



I've tried all the existing suggestions I could find, but so far none have helped. Primarily, I've already tried:




  • Deleting the .idea folder and re-importing

  • Reimporting the project from the root build.gradle file

  • Clicking on the "Refresh all gradle projects" button

  • Upgrading IntelliJ

  • Clicking on "Invalidate Caches and Restart"


What can I do to get IntelliJ to recognize the packages in these jar files?







java gradle intellij-idea






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 20:45









Chris MiddletonChris Middleton

2,53711840




2,53711840













  • Please report at youtrack.jetbrains.com/issues/IDEA with the sample project to reproduce attached.

    – CrazyCoder
    Nov 14 '18 at 20:50











  • @CrazyCoder I will do that if I can get a sample project together, but I was hoping that this may be a common enough problem that someone (such as yourself) might be able to answer here for others to see. I saw that your top answer was about a very similar problem: stackoverflow.com/questions/5905896/… Are there any additional steps you would recommend to rule out this being a bug?

    – Chris Middleton
    Nov 14 '18 at 21:14











  • Since you've already tried all the common solutions and they didn't work, your issue is probably different and is not something common that can be answered right away just from the description.

    – CrazyCoder
    Nov 14 '18 at 21:16











  • @CrazyCoder Fair enough. Since I am a gradle and IntelliJ newbie, I thought maybe I was just missing something obvious, but even if that's the case, you're right that it may be specific to my situation. I will attempt to report at the site.

    – Chris Middleton
    Nov 14 '18 at 21:18











  • @CrazyCoder I came up with a workaround below that I'm honestly surprised works. The details are below. I'm now telling Gradle to consider all the files in the Gradle cache directory as a compile dependency, and it then picks up on the jars that it wouldn't before, even though it's automatically finding other jars in that same Gradle cache. Do you have any thoughts as to why the below workaround would work and what cleaner solution there might be?

    – Chris Middleton
    Nov 15 '18 at 17:32



















  • Please report at youtrack.jetbrains.com/issues/IDEA with the sample project to reproduce attached.

    – CrazyCoder
    Nov 14 '18 at 20:50











  • @CrazyCoder I will do that if I can get a sample project together, but I was hoping that this may be a common enough problem that someone (such as yourself) might be able to answer here for others to see. I saw that your top answer was about a very similar problem: stackoverflow.com/questions/5905896/… Are there any additional steps you would recommend to rule out this being a bug?

    – Chris Middleton
    Nov 14 '18 at 21:14











  • Since you've already tried all the common solutions and they didn't work, your issue is probably different and is not something common that can be answered right away just from the description.

    – CrazyCoder
    Nov 14 '18 at 21:16











  • @CrazyCoder Fair enough. Since I am a gradle and IntelliJ newbie, I thought maybe I was just missing something obvious, but even if that's the case, you're right that it may be specific to my situation. I will attempt to report at the site.

    – Chris Middleton
    Nov 14 '18 at 21:18











  • @CrazyCoder I came up with a workaround below that I'm honestly surprised works. The details are below. I'm now telling Gradle to consider all the files in the Gradle cache directory as a compile dependency, and it then picks up on the jars that it wouldn't before, even though it's automatically finding other jars in that same Gradle cache. Do you have any thoughts as to why the below workaround would work and what cleaner solution there might be?

    – Chris Middleton
    Nov 15 '18 at 17:32

















Please report at youtrack.jetbrains.com/issues/IDEA with the sample project to reproduce attached.

– CrazyCoder
Nov 14 '18 at 20:50





Please report at youtrack.jetbrains.com/issues/IDEA with the sample project to reproduce attached.

– CrazyCoder
Nov 14 '18 at 20:50













@CrazyCoder I will do that if I can get a sample project together, but I was hoping that this may be a common enough problem that someone (such as yourself) might be able to answer here for others to see. I saw that your top answer was about a very similar problem: stackoverflow.com/questions/5905896/… Are there any additional steps you would recommend to rule out this being a bug?

– Chris Middleton
Nov 14 '18 at 21:14





@CrazyCoder I will do that if I can get a sample project together, but I was hoping that this may be a common enough problem that someone (such as yourself) might be able to answer here for others to see. I saw that your top answer was about a very similar problem: stackoverflow.com/questions/5905896/… Are there any additional steps you would recommend to rule out this being a bug?

– Chris Middleton
Nov 14 '18 at 21:14













Since you've already tried all the common solutions and they didn't work, your issue is probably different and is not something common that can be answered right away just from the description.

– CrazyCoder
Nov 14 '18 at 21:16





Since you've already tried all the common solutions and they didn't work, your issue is probably different and is not something common that can be answered right away just from the description.

– CrazyCoder
Nov 14 '18 at 21:16













@CrazyCoder Fair enough. Since I am a gradle and IntelliJ newbie, I thought maybe I was just missing something obvious, but even if that's the case, you're right that it may be specific to my situation. I will attempt to report at the site.

– Chris Middleton
Nov 14 '18 at 21:18





@CrazyCoder Fair enough. Since I am a gradle and IntelliJ newbie, I thought maybe I was just missing something obvious, but even if that's the case, you're right that it may be specific to my situation. I will attempt to report at the site.

– Chris Middleton
Nov 14 '18 at 21:18













@CrazyCoder I came up with a workaround below that I'm honestly surprised works. The details are below. I'm now telling Gradle to consider all the files in the Gradle cache directory as a compile dependency, and it then picks up on the jars that it wouldn't before, even though it's automatically finding other jars in that same Gradle cache. Do you have any thoughts as to why the below workaround would work and what cleaner solution there might be?

– Chris Middleton
Nov 15 '18 at 17:32





@CrazyCoder I came up with a workaround below that I'm honestly surprised works. The details are below. I'm now telling Gradle to consider all the files in the Gradle cache directory as a compile dependency, and it then picks up on the jars that it wouldn't before, even though it's automatically finding other jars in that same Gradle cache. Do you have any thoughts as to why the below workaround would work and what cleaner solution there might be?

– Chris Middleton
Nov 15 '18 at 17:32












1 Answer
1






active

oldest

votes


















-1














After trying all the different suggestions about this issue, the one that seemed to work was to copy the unrecognized jars from my local Gradle cache into a folder within my project. I also had to tell Gradle to look there.



In summary, if I change my root project build.gradle like this:



allprojects {
repositories {
flatDir {
dirs 'lib'
}
}
... existing repos ...
}


and the subproject's build.gradle like so:



dependencies {
... existing dependencies ...
compile fileTree(dir: 'lib', include: '*.jar')
}


and then run the following:



jars=($(find /Users/me/.gradle/caches/modules-2/files-2.1/unrecognizedorg/ -name '*.jar'))
for jar in "${jars[@]}" ; do
cp "$jar" ~/myproject/lib/
done


and then refresh Gradle within IntelliJ, the files are recognized.



Of course, then I have a problem of having a lib folder with jars that I do not want committed in my project, but there are ways around that. There's also the problem of keeping these folders up-to-date if any dependencies change.



I solved this by making a symbolic link to the Gradle cache directory:



ln -s /Users/me/.gradle/caches/modules-2/files-2.1/ lib


So that I don't have to copy the files, and any changes are picked up automatically.



Update



After temporarily removing all my workarounds so that I could assess how to make IntelliJ work without committing the workarounds (the build script changes as well as the symbolic link), I discovered that it still worked (!). That is, it now works in IntelliJ with no changes (by me) from the state that was not working.



The conclusion I draw is that perhaps IntelliJ had a corrupted cache, which was only fixed by getting it to recognize the jars via the symbolic link. Although I haven't tried it, it's possible that both deleting the ~/.gradle/caches and reloading the project would have had the same effect.



Either way, going through the steps above and then undoing them seems to do the trick.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308453%2fintellij-wont-recognize-some-imports-from-gradle-project%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    After trying all the different suggestions about this issue, the one that seemed to work was to copy the unrecognized jars from my local Gradle cache into a folder within my project. I also had to tell Gradle to look there.



    In summary, if I change my root project build.gradle like this:



    allprojects {
    repositories {
    flatDir {
    dirs 'lib'
    }
    }
    ... existing repos ...
    }


    and the subproject's build.gradle like so:



    dependencies {
    ... existing dependencies ...
    compile fileTree(dir: 'lib', include: '*.jar')
    }


    and then run the following:



    jars=($(find /Users/me/.gradle/caches/modules-2/files-2.1/unrecognizedorg/ -name '*.jar'))
    for jar in "${jars[@]}" ; do
    cp "$jar" ~/myproject/lib/
    done


    and then refresh Gradle within IntelliJ, the files are recognized.



    Of course, then I have a problem of having a lib folder with jars that I do not want committed in my project, but there are ways around that. There's also the problem of keeping these folders up-to-date if any dependencies change.



    I solved this by making a symbolic link to the Gradle cache directory:



    ln -s /Users/me/.gradle/caches/modules-2/files-2.1/ lib


    So that I don't have to copy the files, and any changes are picked up automatically.



    Update



    After temporarily removing all my workarounds so that I could assess how to make IntelliJ work without committing the workarounds (the build script changes as well as the symbolic link), I discovered that it still worked (!). That is, it now works in IntelliJ with no changes (by me) from the state that was not working.



    The conclusion I draw is that perhaps IntelliJ had a corrupted cache, which was only fixed by getting it to recognize the jars via the symbolic link. Although I haven't tried it, it's possible that both deleting the ~/.gradle/caches and reloading the project would have had the same effect.



    Either way, going through the steps above and then undoing them seems to do the trick.






    share|improve this answer






























      -1














      After trying all the different suggestions about this issue, the one that seemed to work was to copy the unrecognized jars from my local Gradle cache into a folder within my project. I also had to tell Gradle to look there.



      In summary, if I change my root project build.gradle like this:



      allprojects {
      repositories {
      flatDir {
      dirs 'lib'
      }
      }
      ... existing repos ...
      }


      and the subproject's build.gradle like so:



      dependencies {
      ... existing dependencies ...
      compile fileTree(dir: 'lib', include: '*.jar')
      }


      and then run the following:



      jars=($(find /Users/me/.gradle/caches/modules-2/files-2.1/unrecognizedorg/ -name '*.jar'))
      for jar in "${jars[@]}" ; do
      cp "$jar" ~/myproject/lib/
      done


      and then refresh Gradle within IntelliJ, the files are recognized.



      Of course, then I have a problem of having a lib folder with jars that I do not want committed in my project, but there are ways around that. There's also the problem of keeping these folders up-to-date if any dependencies change.



      I solved this by making a symbolic link to the Gradle cache directory:



      ln -s /Users/me/.gradle/caches/modules-2/files-2.1/ lib


      So that I don't have to copy the files, and any changes are picked up automatically.



      Update



      After temporarily removing all my workarounds so that I could assess how to make IntelliJ work without committing the workarounds (the build script changes as well as the symbolic link), I discovered that it still worked (!). That is, it now works in IntelliJ with no changes (by me) from the state that was not working.



      The conclusion I draw is that perhaps IntelliJ had a corrupted cache, which was only fixed by getting it to recognize the jars via the symbolic link. Although I haven't tried it, it's possible that both deleting the ~/.gradle/caches and reloading the project would have had the same effect.



      Either way, going through the steps above and then undoing them seems to do the trick.






      share|improve this answer




























        -1












        -1








        -1







        After trying all the different suggestions about this issue, the one that seemed to work was to copy the unrecognized jars from my local Gradle cache into a folder within my project. I also had to tell Gradle to look there.



        In summary, if I change my root project build.gradle like this:



        allprojects {
        repositories {
        flatDir {
        dirs 'lib'
        }
        }
        ... existing repos ...
        }


        and the subproject's build.gradle like so:



        dependencies {
        ... existing dependencies ...
        compile fileTree(dir: 'lib', include: '*.jar')
        }


        and then run the following:



        jars=($(find /Users/me/.gradle/caches/modules-2/files-2.1/unrecognizedorg/ -name '*.jar'))
        for jar in "${jars[@]}" ; do
        cp "$jar" ~/myproject/lib/
        done


        and then refresh Gradle within IntelliJ, the files are recognized.



        Of course, then I have a problem of having a lib folder with jars that I do not want committed in my project, but there are ways around that. There's also the problem of keeping these folders up-to-date if any dependencies change.



        I solved this by making a symbolic link to the Gradle cache directory:



        ln -s /Users/me/.gradle/caches/modules-2/files-2.1/ lib


        So that I don't have to copy the files, and any changes are picked up automatically.



        Update



        After temporarily removing all my workarounds so that I could assess how to make IntelliJ work without committing the workarounds (the build script changes as well as the symbolic link), I discovered that it still worked (!). That is, it now works in IntelliJ with no changes (by me) from the state that was not working.



        The conclusion I draw is that perhaps IntelliJ had a corrupted cache, which was only fixed by getting it to recognize the jars via the symbolic link. Although I haven't tried it, it's possible that both deleting the ~/.gradle/caches and reloading the project would have had the same effect.



        Either way, going through the steps above and then undoing them seems to do the trick.






        share|improve this answer















        After trying all the different suggestions about this issue, the one that seemed to work was to copy the unrecognized jars from my local Gradle cache into a folder within my project. I also had to tell Gradle to look there.



        In summary, if I change my root project build.gradle like this:



        allprojects {
        repositories {
        flatDir {
        dirs 'lib'
        }
        }
        ... existing repos ...
        }


        and the subproject's build.gradle like so:



        dependencies {
        ... existing dependencies ...
        compile fileTree(dir: 'lib', include: '*.jar')
        }


        and then run the following:



        jars=($(find /Users/me/.gradle/caches/modules-2/files-2.1/unrecognizedorg/ -name '*.jar'))
        for jar in "${jars[@]}" ; do
        cp "$jar" ~/myproject/lib/
        done


        and then refresh Gradle within IntelliJ, the files are recognized.



        Of course, then I have a problem of having a lib folder with jars that I do not want committed in my project, but there are ways around that. There's also the problem of keeping these folders up-to-date if any dependencies change.



        I solved this by making a symbolic link to the Gradle cache directory:



        ln -s /Users/me/.gradle/caches/modules-2/files-2.1/ lib


        So that I don't have to copy the files, and any changes are picked up automatically.



        Update



        After temporarily removing all my workarounds so that I could assess how to make IntelliJ work without committing the workarounds (the build script changes as well as the symbolic link), I discovered that it still worked (!). That is, it now works in IntelliJ with no changes (by me) from the state that was not working.



        The conclusion I draw is that perhaps IntelliJ had a corrupted cache, which was only fixed by getting it to recognize the jars via the symbolic link. Although I haven't tried it, it's possible that both deleting the ~/.gradle/caches and reloading the project would have had the same effect.



        Either way, going through the steps above and then undoing them seems to do the trick.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 18:04

























        answered Nov 15 '18 at 17:28









        Chris MiddletonChris Middleton

        2,53711840




        2,53711840
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308453%2fintellij-wont-recognize-some-imports-from-gradle-project%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine