1

I'm building a java application with Intellij-idea, Gradle and Mongo. I created a new Gradle project and I was having some trouble importing mongodb-java-driver inside my project. I modified this piece of code inside build.gradle:

dependencies {
 testCompile group: 'junit', name: 'junit', version: '4.11'
 compile 'org.mongodb:mongo-java-driver:3.4.1'
}

But was unable to use Mongo with Java. My External Libraries didn't have any Mongo jar. Then I saw here that I should add the application plugin to my build.gradle. I searched on the official docs that this plugin "will automatically add run task that will execute the specified main class with all the runtime dependencies automatically put on the classpath:" as the answer linked above stated. But this didn't solved my problem.

Then I added the idea plugin, since I saw here that "The IDEA plugin generates files that are used by IntelliJ IDEA, thus making it possible to open the project from IDEA". I didn't think that would help me but was worth a try. Unfortunately, this didn't helped either.

After this I saw here that I should synchronize my project. Since I created this from zero and didn't import any complete project from outside I didn't understand why I should synchronize it. While it was synchronized, I saw Intellij was downloading mongo-java-driver. After that, I still was unable to use Mongo. I had to add the downloaded jar to my project class path, as this was one of the suggested corrections given by Intellij.

I've come from Eclipse/Maven and I'm discovering how Intellij/Gradle behave. So my question is what is strictly necessary to use mongodb-java-driver on Intellij/Gradle and why I had to synchronize my project? Is this the correct way of using Gradle? Always building and synchronizing after?

Ruslan López
  • 4,433
  • 2
  • 26
  • 37
rado
  • 5,720
  • 5
  • 29
  • 51
  • It's not clear what you are trying to do. Mongo is a DB. What do you mean when you say you want to use Mongo in your java? – Guy Grin Jan 15 '17 at 21:47
  • I'm trying to correctly set up a simple application that uses Mongo as DB as this [tutorial for Eclipse/Maven](http://www.mkyong.com/mongodb/java-mongodb-hello-world-example/) – rado Jan 15 '17 at 21:48
  • And what does it mean when you say "I was unable yo use Mongo with Java"? Can I assume you mean that the code form the tutorial didn't compile? Or it compiled but didn't work? – Guy Grin Jan 15 '17 at 21:54
  • The code didn't compile because Intellij was unable to find any mongo classes – rado Jan 15 '17 at 21:56
  • `Gradle` can auto import libraries in intellij go to File->Settings->Build->Gradle->use auto-import. Otherwise, you need manually import the libraries on your own (through the synchronize button) the pulgins you mentioned are not related to this subject – Guy Grin Jan 15 '17 at 22:05
  • But importing the library was only available after I synchronized my project. I would like to discover the purpose or correct way of always synchronizing after adding a new dependency – rado Jan 15 '17 at 22:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133227/discussion-between-guy-grin-and-gabrielrado). – Guy Grin Jan 15 '17 at 22:19

0 Answers0