我正在尝试通过 mvn package 创建一个 jar 然后运行 java -jar/target/test.jar
Caused by: org.apache.commons.vfs2.FileSystemException: Could not replicate "file:///C:/Users/user/workspace/testProject/target/test.jar!/BOOT-INF/lib/myJar.jar" as it does not exist.
at org.apache.commons.vfs2.provider.AbstractFileSystem.replicateFile(AbstractFileSystem.java:418) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.zip.ZipFileSystem.<init>(ZipFileSystem.java:61) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.jar.JarFileSystem.<init>(JarFileSystem.java:50) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.jar.JarFileProvider.doCreateFileSystem(JarFileProvider.java:82) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.AbstractLayeredFileProvider.createFileSystem(AbstractLayeredFileProvider.java:89) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.AbstractLayeredFileProvider.findFile(AbstractLayeredFileProvider.java:63) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:693) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:649) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:605) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.res.ResourceFileProvider.findFile(ResourceFileProvider.java:81) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:693) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:649) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:605) ~[commons-vfs2-2.0.jar!/:2.0]
... 52 common frames omitted
当我执行 mvn spring-boot:run
时,它工作正常,但是当我打包并运行它时,我遇到了上述异常。
在 pom.xml 中
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
com.client.test.Application
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
最佳答案
可执行 jar 中的大多数嵌套库不需要解压缩即可运行。但是,某些库可能会出现问题。有关更多详细信息,请参阅此 spring boot 文档 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-extract-specific-libraries-when-an-executable-jar-runs
或
使用 maven-dependency-plugin 和 maven-jar-plugin。 尽管需要在 .jar 的同一位置复制“target/lib”文件夹。就像它在目标文件夹中一样。 尝试使用这个:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
<package>.<MainClassName>
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
关于maven - Spring boot - 抛出异常 jar 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51900457/