【问题标题】:Springboot: Jar not found exception while running from command lineSpringboot:从命令行运行时找不到Jar异常
【发布时间】:2016-06-23 09:22:36
【问题描述】:

我正在使用 springboot 应用程序。在使用java -jar 从命令行运行我的 app.jar 时,我收到以下错误

java.io.FileNotFoundException: C:\MyApp.jar!\lib\jackson-databind-2.6.5.jar (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)

...

java.io.FileNotFoundException: C:\MyApp.jar!\lib\jackson-annotations-2.6.5.jar (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at com.impetus.annovention.Discoverer.getResourceIterator(Discoverer.java:298)
    at com.impetus.annovention.Discoverer.discover(Discoverer.java:142)

这些错误,我得到了多个 jar,尽管这些 jar 在 Spring boot fat jar 下的 lib 文件夹中可用。

现在,要解决这个问题,我可以使用requiresUnpack,如下所示。

          <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${version-spring-boot}</version>
        <configuration>
           <requiresUnpack>
              <dependency>
                 <groupId>xxx</groupId>
                 <artifactId>xxx</artifactId>
              </dependency>
              <dependency>
                 <groupId>yyy</groupId>
                 <artifactId>yyy</artifactId>
              </dependency>
           </requiresUnpack>
        </configuration>
        <executions>
           <execution>
              <goals>
                 <goal>repackage</goal>
              </goals>
           </execution>
        </executions>
     </plugin>

现在,这种方式的问题是针对我所有的 jars,我已将它们一一添加到我的 requiresUnpack 标记中。

但我正在寻找一些通用的方法来做到这一点。就像某个班轮可以解决所有这些问题。SO,有什么办法可以做到吗?


已编辑:添加我正在使用的其他插件...

<plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>2.2.4</version>
            <configuration>
               <defaultOutputDirectory>
                  ${project.build.directory}/generated-sources
               </defaultOutputDirectory>
               <processors>
                  <processor>org.mapstruct.ap.MappingProcessor</processor>
               </processors>
            </configuration>
            <executions>
               <execution>
                  <id>process</id>
                  <phase>generate-sources</phase>
                  <goals>
                     <goal>process</goal>
                  </goals>
               </execution>
            </executions>
            <dependencies>
               <dependency>
                  <groupId>org.mapstruct</groupId>
                  <artifactId>mapstruct-processor</artifactId>
                  <version>1.0.0.CR1</version>
               </dependency>
            </dependencies>
         </plugin>
 <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.7</version>
            <executions>
               <execution>
                  <id>add-source</id>
                  <phase>generate-sources</phase>
                  <goals>
                     <goal>add-source</goal>
                  </goals>
                  <configuration>
                     <sources>
                        <source>target/generated-sources</source>
                     </sources>
                  </configuration>
               </execution>
            </executions>
         </plugin>

这 2 个插件是我用于 Mapstruct 的。

【问题讨论】:

  • 你能发布更多你的 pom,比如你拥有的所有插件吗?
  • 为什么不使用jackson lib的经典依赖?
  • 不仅仅是杰克逊。有很多罐子我得到这样的。jackson-databind-2.6.5.jar jackson-annotations-2.6.5.jar json-patch-1.7.jar jackson-coreutils-1.6.jar msg-simple-1.1.jar btf -1.2.jar commons-rql-1.0.1.jar guava-18.0.jar c3p0-0.9.1.1.jar ... ...等等
  • 抱歉重复我的问题,但为什么不使用经典依赖,为什么使用重新打包?
  • @MickaëlB 好的,我明白了……那我应该在目标中写什么?可以给我看看吗?

标签: java spring maven jar spring-boot


【解决方案1】:

正如我在 cmets 中所说,为什么不使用经典依赖项,例如:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.5</version>
</dependency>

【讨论】:

  • 不,我有.. 我仍然遇到这个问题!我要添加完整的 pom 吗?
  • 忽略最后一条评论。是的,你是对的,如果我在依赖项中添加未找到的 jar 详细信息。然后它不会来。感谢您的帮助。但是 thouse jar 已经在我的依赖层次结构和 lib 中可用。所以必须手动添加所有这些作为依赖项。还是有其他办法解决?
  • 在我看来,这是正确的做法。我不知道任何其他解决方案。
  • aahhh!.. 实际上,当我尝试映射一个 jar 时.. 它正在工作。但是在添加 2 个整数后不起作用?有什么想法吗?
  • 忽略我最后的评论!...实际上,首先我添加了同学,然后那个罐子的未找到问题就消失了。然后我添加杰克逊数据绑定。然后又没有找到两个罐子都在撒着的。但现在我想它很好......我再次清理和构建......现在那个未找到的问题已经消失了
猜你喜欢
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 2020-12-24
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
相关资源
最近更新 更多