启动Docker容器时获取java.lang.ClassNotFoundException

时间:2018-01-30 14:16:05

标签: java maven docker

我在Docker容器中启动.jar文件时遇到问题。我得到的错误信息是:

  

线程“main”中的异常java.lang.ClassNotFoundException:   ccinfw.MainApplication

我在下面添加了信息。为什么我会收到此错误以及如何解决?

TIA

清单文件

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

运行jar -xvf BackEndRestFunctionality-0.0.1-SNAPSHOT.jar>扩大。以下是输出的一部分。 MainApplication.class确实存在于jar文件

 [... 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 ...]

我使用的是以下软件

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依赖

enter image description here

启动时收到以下错误消息:

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)

在Jenkins内执行的maven命令

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

负责构建的pom.xml文件的一部分

<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>

正在访问的Dockerfile

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

以下是Jenkins日志文件的一部分,其中使用生成的.jar文件构建Docker镜像文件 - 再次 - .jar文件在自行运行时工作正常

[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图像图层

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"] 

1 个答案:

答案 0 :(得分:0)

我发现了问题所在 - 非常感谢Gitlab中有一个版本可以引用(以及另一台机器上的&#34;以前的#34;版本)

基本上,我使用Maven将项目放在一起。

要构建项目,我在下面设置命令并在Jenkins下运行:

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

在pom.xml文件中,我没有指定:

<artifactId></artifactId>

当我继续设置它时

ex: <artifactId>stuff</artifactId> 

Docker正确启动了jar文件,一切正常。

<强>更新 需要更准确地报告项目。

已定义。但

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

TIA