简体   繁体   中英

How do I add dependencies/3rd party java libraries into Maven for Intellij and/or VS Code?

I am trying to make a JSON parser for a project using GSON in java with Maven in Intellij or VS Code. Whenever I try to compile/run the debugger on the file (ex: javac jsonParser.java) it always says the "com.google.gson" and related objects such as "GSON gson = new Gson();" does not exist.

Things I have tried:

  • Watching many tutorials/read many walkthroughs on how to add a dependency/3rd party JAR, however, the best I was able to do was make the App.java file in a default Maven project template be able to use the GSON library, but any additional java files in the folder would still get the above compilation error.

  • Manually add the dependency information in the pom.xml file

  • Use the built in "add dependency" features in both Intellij and VS Code
  • I have tried on both Ubuntu and Windows 10 (including adding things to the environment variables)... Often times, I am able to see the dependency automatically added to the pom.xml file, as well as see the JAR file in my project folder
  • Running sets of Maven commands in VS Code ---> "mvn install" "mvn package" "mvn dependency:resolve" which run fine with no errors

TLDR: Would someone be able to walk me through adding a 3rd party library in Maven/Gradle and/or Intellij/VS Code? If it helps, I am willing to use Gradle or some other (freely available) software if that is easier/preferred? I am new to any sort of software development (first year CS student) so I have no experience in terms of making a completed project.

  1. You look up the GSON coordinates (groupId, artifactId, version) in MavenCentral( http://search.maven.org )

  2. You take the XML from MavenCentral and copy it to the <dependencies> section of your pom.xml

  3. You use GSON in your Java code

  4. You build your project with mvn clean verify

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