简体   繁体   English

Maven-将类添加为依赖项后导入类

[英]Maven - importing a class after adding it as a dependency

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: 我在pom.xml中添加了依赖项gson,如下所示:

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

And I ran mvn install after that. 然后我运行了mvn install 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. 现在,我可以在C:\\Users\\%Name%\\.m2\\repository\\com\\google\\code\\gson\\gson\\2.8.5找到jar依赖项,因此我假设它已正确安装。

When I try to import the class Gson in Java, several problems occurs: 当我尝试在Java中导入类Gson时,会发生一些问题:

  • The path to import isn't com.google.code.gson but com.google.gson , I don't understand why. 导入的路径不是com.google.code.gson而是com.google.gson ,我不明白为什么。
  • When I compile with mvn package, everything runs smoothly. 当我用mvn软件包编译时,一切运行顺利。 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 : 但是如果我尝试使用java -cp .\\target\\testJSON-1.0-SNAPSHOT.jar json.App运行代码, java -cp .\\target\\testJSON-1.0-SNAPSHOT.jar json.App收到以下错误,告诉我未找到Gson类:

    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. 现在,从我通过寻找解决方案所了解的内容来看:尽管maven知道项目的依赖项,但java不会。 And I need to manually add the jar to the classpath. 我需要手动将jar添加到类路径。

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 ? 如果我不敢告诉Maven我需要gson来运行我的项目,为什么我需要手动包括所有依赖项来运行它? 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 . 当我寻找将多个jars添加到类路径的方法时,我发现的唯一答案是假设所有jars都位于同一文件夹中,因此建议使用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. 由Maven生成的jar位于我项目的目标子文件夹中,而gson jar位于.m2文件夹中。 How to add multiple jars (located in different locations) to the classpath ? 如何将多个jar(位于不同位置)添加到classpath?

Thank you, 谢谢,

The path to import isn't com.google.code.gson 导入路径不是com.google.code.gson

Google Code got shut down in 2016, they have changed their group IDs. Google Code于2016年关闭,他们更改了组ID。

the Gson class isn't found 找不到Gson类

The GSON JAR isn't on the classpath. GSON JAR不在类路径中。

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 ? 如果我不敢告诉Maven我需要gson来运行我的项目,为什么我需要手动包括所有依赖项来运行它? 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. Maven是一个构建工具,它已经成功构建了您的应用程序。 Java requires you to include the JARs on the classpath. Java要求您在类路径中包括JAR。 This not a deficiency of Maven. 这不是Maven的不足。 If you want to bundle all the dependencies together into one JAR that "just runs" then create a fat JAR . 如果要将所有依赖项捆绑到一个“可以运行”的JAR中,请创建一个胖JAR

How to add multiple jars (located in different locations) to the classpath ? 如何将多个jar(位于不同位置)添加到classpath?

If you create a fat JAR, you won't have to, but you can separate classpath entries like so ( OS-specific ): 如果创建胖的JAR,则不必这样做,但是您可以这样分隔类路径条目( 特定于OS ):

java -cp folder/*.jar;another/folder/*.jar

You may want to include the dependencies in when creating a jar with maven. 使用maven创建jar时,可能需要包括依赖项。 You can use maven-assembly plugin, 您可以使用maven-assembly插件,

 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM