Error: Invalid or corrupt jarfile /app.jar

9/11/2019

I am trying to push a spring boot created microservice to an ibm cloud hosted K8 cluster but am constantly getting the below error on startup:

Error: Invalid or corrupt jarfile /app.jar

My dockerfile:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>projects</groupId>
  <artifactId>mydb2jdbcproject</artifactId>
  <version>1</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.SR4</spring-cloud.version>
    <docker.image.prefix>springio</docker.image.prefix>
    <app.name>mydb2jdbcproject</app.name>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.15.RELEASE</version>
  </parent>

  <dependencies>
  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>com.ibm.db2.jcc</groupId>
      <artifactId>db2jcc4</artifactId>
      <version>4.26.14</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.json</groupId>
      <artifactId>javax.json-api</artifactId>
      <version>1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.jayway.jsonpath</groupId>
      <artifactId>json-path</artifactId>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupId>com.ibm.cloud</groupId>
      <artifactId>ibm-cloud-spring-boot-service-bind</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.microprofile.config</groupId>
      <artifactId>microprofile-config-api</artifactId>
      <version>1.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <repositories>
    <repository>
      <id>repo</id>
      <url>file://${project.basedir}/lib</url>
    </repository>
  </repositories>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <version>1.4.9</version>
        <configuration>
          <repository>${docker.image.prefix}/${project.artifactId}</repository>
          <buildArgs>
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
          </buildArgs>
        </configuration>
      </plugin>
        <plugin>
  <groupId>io.fabric8</groupId>
  <artifactId>docker-maven-plugin</artifactId>
  <extensions>true</extensions>
  <configuration>
    <images>
      <image>
        <name>us.icr.io/rbs1/rbstest</name>
        <build>
          <from>adoptopenjdk/openjdk8-openj9</from>
          <entryPoint>
            <exec>
              <arg>java</arg>
              <arg>-jar</arg>
              <arg>/${project.build.finalName}.jar</arg>
            </exec>
          </entryPoint>
          <assembly>
            <targetDir>/</targetDir>
            <mode>dir</mode>
            <descriptorRef>artifact</descriptorRef>
          </assembly>
        </build>
      </image>
    </images>
  </configuration>
</plugin>
    </plugins>
  </build>

</project>'

my event log:

Events:
  Type     Reason     Age                    From                    Message
  ----     ------     ----                   ----                    -------
  Normal   Scheduled  9m10s                  default-scheduler       Successfully assigned default/basic-deployment-768559cfc6-6fd2j to 10.144.195.59
  Normal   Created    8m15s (x4 over 9m)     kubelet, 10.144.195.59  Created container
  Normal   Started    8m15s (x4 over 9m)     kubelet, 10.144.195.59  Started container
  Normal   Pulling    7m35s (x5 over 9m8s)   kubelet, 10.144.195.59  pulling image "------/---------"
  Normal   Pulled     7m34s (x5 over 9m)     kubelet, 10.144.195.59  Successfully pulled image "--------/---------"
  Warning  BackOff    4m6s (x24 over 8m57s)  kubelet, 10.144.195.59  Back-off restarting failed container

The images are held in a docker repository and then pulled into the cluster. I am fully logged in to both docker and ibmcloud. Each time the container restarts 6 times but is always unsuccessful. I run

mvn package

every time before i build the docker image to make sure there is a jar available and the jar is held with in the target folder named mydb2jdbcproject-1.

-- SaskatoonDiver
docker
ibm-cloud
jar
kubernetes

2 Answers

9/30/2019

Rather embarrassingly i hadn't realised that i needed to point towards the right jar file with ${JAR_FILE}. After ensuring it was directed at my app jar it all worked. Thankyou!

Keeping here incase someone needs it.

-- SaskatoonDiver
Source: StackOverflow

9/12/2019

Replace in Dockerfile

COPY ${JAR_FILE} app.jar

with

COPY ${JAR_FILE} /app.jar
-- apisim
Source: StackOverflow