Casey Harrils
Casey Harrils

Reputation: 2973

getting a java.lang.ClassNotFoundException when starting a Docker container

I am having a problem starting a .jar file within a Docker Container. The error message I am getting is:

Exception in thread "main" java.lang.ClassNotFoundException: ccinfw.MainApplication

I have added information below. Why am I getting this error and how can I fix it?

TIA

Manifest File

Manifest-Version: 1.0
Implementation-Title: SpringBootErrorHandlingDemoy
Implementation-Version: 0.0.1-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: root
Implementation-Vendor-Id: ccinfw
Spring-Boot-Version: 1.5.4.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: ccinfw.MainApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Implementation-URL: http://maven.apache.org

ran jar -xvf BackEndRestFunctionality-0.0.1-SNAPSHOT.jar > expanded. Below is a portion of the output. MainApplication.class does exist in the jar file

 [... snip ...]

 inflated: BOOT-INF/classes/ccinfw/security/user/ApplicationUser.class
 inflated: BOOT-INF/classes/ccinfw/security/security/SecurityConstants.class
 inflated: BOOT-INF/classes/ccinfw/security/security/AccountCredentials.class
 inflated: BOOT-INF/classes/ccinfw/security/security/WebSecurity.class
 inflated: BOOT-INF/classes/ccinfw/security/security/JWTAuthenticationFilter.class
 inflated: BOOT-INF/classes/ccinfw/security/security/JWTAuthorizationFilter.class
 **inflated: BOOT-INF/classes/ccinfw/MainApplication.class**
 inflated: BOOT-INF/classes/ccinfw/dao/MstrgenroletypeDAO.class
 inflated: BOOT-INF/classes/application.properties
  created: META-INF/maven/
  created: META-INF/maven/ccinfw/
  created: META-INF/maven/ccinfw/BackEndRestFunctionality/
 inflated: META-INF/maven/ccinfw/BackEndRestFunctionality/pom.xml
 inflated: META-INF/maven/ccinfw/BackEndRestFunctionality/pom.properties
  created: BOOT-INF/lib/
extracted: BOOT-INF/lib/slf4j-api-1.7.25.jar

[... snip ...]

I am using the following software

jenkins-slave-one:/var/jenkins/workspace/build-cc-restapi-dev/target/docker# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2ubuntu1.16.04.3-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)

jenkins-slave-one:/var/jenkins/workspace/build-cc-restapi-dev/target/docker# javac -version
javac 1.8.0_131

jenkins-slave-one:/var/jenkins/workspace/build-cc-restapi-dev/target/docker# mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: /opt/maven
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-87-generic", arch: "amd64", family: "unix"

Maven Dependencies

enter image description here

I am getting the following error message when starting it:

Exception in thread "main" java.lang.ClassNotFoundException: ccinfw.MainApplication
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

the maven command executed within Jenkins

$ /opt/maven/bin/mvn -f /var/jenkins/workspace/build-cc-restapi-dev/pom.xml -Pdevelopment clean package docker:build -B

portion of pom.xml file responsible for building

<profile>
    <id>development</id>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>

                                <copy
                                    file="${project.build.outputDirectory}/application.development.properties"
                                    tofile="${project.build.outputDirectory}/application.properties" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>

                <configuration>
                    <imageName>apostx-cc-restapi-dev</imageName>

                    <baseImage>java:8</baseImage>
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <!-- copy the service's jar file from target into the root directory 
                        of the image -->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <!-- <version>2.6</version> -->
                <executions>
                    <execution>
                        <id>analyze</id>
                        <phase>package</phase>
                        <configuration>
                            <failBuild>true</failBuild>
                            <ignoreDirect>false</ignoreDirect>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>ccinfw.MainApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</profile>

the Dockerfile being accessed

FROM java:8
ADD /BackEndRestFunctionality-0.0.1-SNAPSHOT.jar //
ENTRYPOINT ["java", "-jar", "/BackEndRestFunctionality-0.0.1-SNAPSHOT.jar"]
~

Here is a part of the Jenkins log file where the Docker Image file is being built using the generated .jar file - again - the .jar file works fine when ran on its own

[INFO] Copying /var/jenkins/workspace/build-cc-restapi-dev/target/BackEndRestFunctionality-0.0.1-SNAPSHOT.jar -> /var/jenkins/workspace/build-cc-restapi-dev/target/docker/BackEndRestFunctionality-0.0.1-SNAPSHOT.jar
[INFO] Building image apostx-cc-restapi-dev
Step 1/3 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/3 : ADD /BackEndRestFunctionality-0.0.1-SNAPSHOT.jar //
 ---> abfcb05b4173
Removing intermediate container f573f737009a
Step 3/3 : ENTRYPOINT java -jar /BackEndRestFunctionality-0.0.1-SNAPSHOT.jar
 ---> Running in 28bb20467e70
 ---> 2ffb803f96dc
Removing intermediate container 28bb20467e70
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 2ffb803f96dc
Successfully tagged apostx-cc-restapi-dev:latest
[INFO] Built apostx-cc-restapi-dev
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.181 s
[INFO] Finished at: 2018-01-30T06:18:18+00:00
[INFO] Final Memory: 36M/95M
[INFO] ------------------------------------------------------------------------

Docker Image Layers

351.5 MB    
RUN set -x && apt-get update && apt-get install -y openjdk-8-jdk="$JAVA_DEBIAN_VERSION" ca-certificates-java="$CA_CERTIFICATES_JAVA_VERSION" && rm -rf /var/lib/apt/lists/* && [ "$JAVA_HOME" = "$(docker-java-home)" ]
123 MB  
ADD file:89ecb642d662ee7edbb868340551106d51336c7e589fdaca4111725ec64da957 in /
122.6 MB    
RUN apt-get update && apt-get install -y --no-install-recommends bzr git mercurial openssh-client subversion procps && rm -rf /var/lib/apt/lists/*
44.3 MB     
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl wget && rm -rf /var/lib/apt/lists/*
32.9 MB     
ADD file:565dae161b3b516b50d596648650278591d96fe820e36a3eeef0ee7c33d7915d in //
1.3 MB  
RUN apt-get update && apt-get install -y --no-install-recommends bzip2 unzip xz-utils && rm -rf /var/lib/apt/lists/*
418.5 kB    
RUN /var/lib/dpkg/info/ca-certificates-java.postinst configure
87 B    
RUN { echo '#!/bin/sh'; echo 'set -e'; echo; echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; } > /usr/local/bin/docker-java-home && chmod +x /usr/local/bin/docker-java-home
55 B    
RUN echo 'deb http://deb.debian.org/debian jessie-backports main' > /etc/apt/sources.list.d/jessie-backports.list
0 B     
CMD ["/bin/bash"]
0 B     
ENV LANG=C.UTF-8
0 B     
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
0 B     
ENV JAVA_VERSION=8u111
0 B     
ENV JAVA_DEBIAN_VERSION=8u111-b14-2~bpo8+1
0 B     
ENV CA_CERTIFICATES_JAVA_VERSION=20140324
0 B     
ENTRYPOINT ["java" "-jar" "/BackEndRestFunctionality-0.0.1-SNAPSHOT.jar"] 

Upvotes: 6

Views: 15286

Answers (4)

lLagger
lLagger

Reputation: 1

Here is my solition. my old code:

FROM openjdk:11

RUN mkdir /app

COPY src/main/java/org/example/ /app

WORKDIR /app

CMD java Main

here is my new code;

FROM openjdk:11

RUN mkdir /app

COPY src/main/java/org/example/ /app

WORKDIR /app

CMD java Main.java

so i just added .java to my Main from CMD

Upvotes: 0

Carlos No&#233;
Carlos No&#233;

Reputation: 351

(Solved) in my case, I had the same problem (ClassNotfoundException) when starting my docker container.

When I generated my image in docker I would specify a jar from my project which I had specified before with maven.

The error was that when generating said .jar it did not specify the Main Class well, and when docker was executing the jar it did not know where the main was.

To fix it you must specify where your main class is located.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <!-- DO NOT include log4j.properties file in your Jar -->
                    <excludes>
                        <exclude>**/log4j.properties</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <!-- Jar file entry point -->
                            <mainClass>com.carmuno.aleniet.dbcs.dbcdApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

Upvotes: 0

Casey Harrils
Casey Harrils

Reputation: 2973

I found out what the problem was - thank goodness for having a version in Gitlab to refer to (as well as a "previous" version on another machine)

Basically, I used Maven to put the project together.

To build the project, I set up the command below and ran it under Jenkins:

$ /opt/maven/bin/mvn -f /var/jenkins/workspace/build-cc-restapi-dev/pom.xml -Pdevelopment clean package docker:build -B

In the pom.xml file, I did not specify :

<artifactId></artifactId>

When I went ahead and set it

ex: <artifactId>stuff</artifactId> 

Docker started the jar file correctly and everything worked fine.

UPDATE Need to report items more accurately.

<artifactId></artifactId>

was defined. But

<artifactId>stuff</artifactId> => worked
<artifactId>ccinfieldworkserver</artifactId> => worked
<artifactId>BackEndRestFunctionality</artifactId> => did not work.

TIA

Upvotes: 1

Casey Harrils
Casey Harrils

Reputation: 2973

I found out what the problem was - thank goodness for having a version in Gitlab to refer to (as well as a "previous" version on another machine)

Basically, I used Maven to put the project together. I executed the following command from Jenkins to build the .jar file

$ /opt/maven/bin/mvn -f /var/jenkins/workspace/build-cc-restapi-dev/pom.xml -Pdevelopment clean package docker:build -B

In the pom.xml file, I did not specify :

<artifactId></artifactId>

When I went ahead and set it

ex: <artifactId>stuff</artifactId> 

Docker started the jar file correctly and everything worked fine.

Upvotes: 0

Related Questions