0

I'm trying to use(import) a class added by a dependency of my project. I added the dependency gson in my pom.xml as follows:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

And I ran mvn install after that. Now I can find the jar dependencies in C:\Users\%Name%\.m2\repository\com\google\code\gson\gson\2.8.5, so I assume it is correctly installed.

When I try to import the class Gson in Java, several problems occurs:

  • The path to import isn't com.google.code.gson but com.google.gson, I don't understand why.
  • When I compile with mvn package, everything runs smoothly. But if I try to run the code with java -cp .\target\testJSON-1.0-SNAPSHOT.jar json.App I get the following error telling me the Gson class isn't found :

    Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson at json.App.main(App.java:13) Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more

Now from what I understood by looking for solutions: While maven know the dependencies of the project, java won't. And I need to manually add the jar to the classpath.

So my questions are:

  • If I bother to tell maven I need gson to run my project, why do I need to manually include all dependencies to run it ? Is there a way to automate this thing in an easy and intuitive manner ?
  • When I look for ways to add multiples jars to the classpath, the only answers I find assume that all the jars are in the same folder, hence proposing to do java -cp folder/*.jar. The jar generated by Maven is located in the target subfolder of my project, while the gson jar is located in the .m2 folder. How to add multiple jars (located in different locations) to the classpath ?

Thank you,

Pythalex
  • 143
  • 1
  • 8
  • I'm going to recommend that you start using a modern IDE such as IntelliJ. IDEs can automatically figure out what the correct `import` path is. There is a free community edition, if you don't want to pull out your credit card just yet. – Tim Biegeleisen Apr 18 '19 at 11:05
  • I currently use VS Code and it works well most of the time. It seems the problem here came from gson because the same manipulation (with the explicit class path) worked with org.json. – Pythalex Apr 18 '19 at 11:58

2 Answers2

3

The path to import isn't com.google.code.gson

Google Code got shut down in 2016, they have changed their group IDs.

the Gson class isn't found

The GSON JAR isn't on the classpath.

If I bother to tell maven I need gson to run my project, why do I need to manually include all dependencies to run it ? Is there a way to automate this thing in an easy and intuitive manner ?

Maven is a build tool, and it has built your app successfully. Java requires you to include the JARs on the classpath. This not a deficiency of Maven. If you want to bundle all the dependencies together into one JAR that "just runs" then create a fat JAR.

How to add multiple jars (located in different locations) to the classpath ?

If you create a fat JAR, you won't have to, but you can separate classpath entries like so (OS-specific):

java -cp folder/*.jar;another/folder/*.jar
Michael
  • 41,989
  • 11
  • 82
  • 128
  • Ok so it seems there was a problem with the dependency I tried to add in the first place. I tested with another package and I could run it successfully by giving the path of the dependency jar. I think the fat jar solution is the way to go here but none of the plugin I tried could give me a working jar in the end. – Pythalex Apr 18 '19 at 11:57
1

You may want to include the dependencies in when creating a jar with maven. You can use maven-assembly plugin,

 <build>
    <plugins>
      <!-- any other plugins -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>
Vikas
  • 6,868
  • 4
  • 27
  • 41