3

I am trying to create a jar through mvn package and then run through 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

When I do mvn spring-boot:run then it works fine but when I package and run it then I get above exception.

In 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>
user1298426
  • 3,467
  • 15
  • 50
  • 96

2 Answers2

2

Most nested libraries in an executable jar do not need to be unpacked in order to run. However, certain libraries can have problems. for more details refer this spring boot doc https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-extract-specific-libraries-when-an-executable-jar-runs

OR

Use maven-dependency-plugin and maven-jar-plugin. Although one needs to copy "target/lib" folder at the same location of .jar. Like it is in the target folder. Try using this :

<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>
sarang
  • 104
  • 6
  • This issue occurs especially when project have custom /proprietary dependency. In this case test.jar is that dependecy, I suspect. – sarang Nov 23 '18 at 14:55
1

Commons-VFS does not know how to handle Spring Boot's nested Jar paths. You have to teach it by extending its ResourceFileProvider. In fact, Spring Boot's classloader returns resource URLs in the form jar:file:foo.jar!bar.jar!baz.jar (one jar: prefix), while Commons-VFS expects multiple "jar:" prefixes like so: jar:jar:file:foo.jar!bar.jar!baz.jar (one "jar:" per bang character "!").

See example fixes in LGPL-licensed Portofino (https://github.com/ManyDesigns/Portofino/blob/02550f5789c307ff668b4a778328c027ec2d7fcc/microservices/spring-boot/src/main/java/com/manydesigns/portofino/microservices/boot/PortofinoBootApplication.java#L56-L59, https://github.com/ManyDesigns/Portofino/blob/02550f5789c307ff668b4a778328c027ec2d7fcc/microservices/spring-boot/src/main/java/com/manydesigns/portofino/microservices/boot/SpringBootResourceFileProvider.java) and GPL-licensed Renjin (https://github.com/bedatadriven/renjin/blob/cac412d232ad66d4ee8e37cfc8cb70a45e676e19/core/src/main/java/org/renjin/util/ClasspathFileProvider.java#L88-L126).

Alessio Stalla
  • 1,022
  • 6
  • 22