4

In attempts to make a standalone JAR that I can launch cleanly and independently, I've ran into issues involving Jersey and my desirable, fat JAR. The final jar will be moved to a Docker image.

The error that I am getting is essentially this:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.glassfish.jersey.server.ResourceConfig]: Factory method 'jerseyResourceConfig' threw exception; nested exception is org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /dir/myproject-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 48 common frames omitted
Caused by: org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /dir/myproject/target/myproject-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory)
    at org.glassfish.jersey.server.internal.scanning.JarZipSchemeResourceFinderFactory.create(JarZipSchemeResourceFinderFactory.java:89)
    at org.glassfish.jersey.server.internal.scanning.JarZipSchemeResourceFinderFactory.create(JarZipSchemeResourceFinderFactory.java:65)
    at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.addResourceFinder(PackageNamesScanner.java:282)
    at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.init(PackageNamesScanner.java:198)
    at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.<init>(PackageNamesScanner.java:154)
    at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.<init>(PackageNamesScanner.java:110)
    at org.glassfish.jersey.server.ResourceConfig.packages(ResourceConfig.java:680)
    at org.glassfish.jersey.server.ResourceConfig.packages(ResourceConfig.java:660)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 49 common frames omitted
Caused by: java.io.FileNotFoundException: /dir/myproject/target/myproject-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes (No such file or directory)

Here is my build profile in my POM file:

<profile>
    <id>d2</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <properties>
        <packaging.type>jar</packaging.type>
        <log.dir>logs</log.dir>
        <!-- updates bootstrap.properties -->
        <config.override.path>./conf</config.override.path>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>my.com</groupId>
                            <artifactId>myArtifact</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

Finally, here are all of the jersey dependencies that I am using:

org.springframework.boot:spring-boot-starter-jersey:jar:1.5.12.RELEASE:compile
|  |  +- org.glassfish.jersey.core:jersey-server:jar:2.25.1:compile
|  |  |  +- org.glassfish.jersey.core:jersey-client:jar:2.25.1:compile
|  |  |  +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.25.1:compile
|  |  +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.25.1:compile
|  |  +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.25.1:compile
|  |  +- org.glassfish.jersey.ext:jersey-spring3:jar:2.25.1:compile
|  |  \- org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.25.1:compile
|  |     +- org.glassfish.jersey.ext:jersey-entity-filtering:jar:2.25.1:compile
................................................................................
|     |  +- org.glassfish.jersey.connectors:jersey-apache-connector:jar:2.17:compile
................................................................................
|  \- io.swagger:swagger-jersey2-jaxrs:jar:1.5.6:compile
|     +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.25.1:compile
|     |  +- org.glassfish.hk2.external:javax.inject:jar:2.5.0-b32:compile
|     |  \- org.glassfish.jersey.core:jersey-common:jar:2.25.1:compile
|     |     +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.25.1:compile
|     \- org.glassfish.jersey.media:jersey-media-multipart:jar:2.25.1:compile

I've read through all of the sources that are referenced from this SO question. I was led through a few github issues that both Spring and Jersey have apparently fixed, yet I am still dealing with these complications. The unique thing about my complication is that the boot-inf/classes that are not found is directly inside my jar and referencing the project itself. Its not complaining about a different, depended on jar, its complaining about the project's own /BOOT-INF/classes.

Any additional insight not mentioned in the aforementioned SO question would be extremely useful! I find that Java is incredibly more difficult to work with in Docker than any other stack that I've experienced (python-django and node-js). Let me know if more information is desired.

Chad Van De Hey
  • 2,716
  • 3
  • 29
  • 46
  • I would also be willing to use a plugin such as [**spotify**](https://github.com/spotify/docker-maven-plugin) that can produce the docker images automatically; however, the same issues seem to exist. – Chad Van De Hey May 31 '18 at 21:36
  • I had similar problem when I used ResourceConfig.package(...) to register endpoints. – Pat Aug 17 '18 at 08:23
  • Did u ever get a solution to this? – Ankur Garg Apr 01 '19 at 03:26

1 Answers1

1

Try adding the following plugin

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> </plugin>

Read more on this https://maven.apache.org/plugins/maven-shade-plugin/

Ankur Garg
  • 2,553
  • 8
  • 30
  • 41