Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of the problems in the Construction of Image by Jib

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the problem analysis of Jib image construction. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the problem analysis of Jib image construction.

A brief description of the problem

The SpringBoot project was successfully made into a Docker image through the Jib plug-in, but an error was reported when running the image (Could not find or load main class ${start-class})

About the Jib plug-in

In the Maven project, you can use the Jib plug-in to build the current Java project into a Docker image.

Environmental information

Operating system: macOS Mojave 10.14.6 (18G103)

JDK:10.14.6 (18G103)

Docker:10.14.6 (18G103)

SpringBoot:2.1.8.RELEASE

Jib plug-in version: 1.6.1

Source code download

In order to reproduce the problem, I uploaded the problem SpringBoot project to GitHub. The address and link information are shown in the following table:

Name Link Note Project Home Page https://github.com/zq2599/blog_demos the home page of the project on GitHub git warehouse address (https) https://github.com/zq2599/blog_demos.git the warehouse address of the project source code, https protocol git warehouse address (ssh) git@github.com:zq2599/blog_demos.git the warehouse address of the project source code, ssh protocol

There are multiple folders in this git project. The application of this chapter is under the jib-error-demo folder, as shown in the red box below:

Question:

Execute the command mvn clean compile-U in the directory where the pom.xml file resides, and the image can be built successfully, but the console outputs a warning message, as shown below:

Try to create a container with this image, and use the command docker run-- name=test bolingcavalry/hellojib:0.0.1-SNAPSHOT. The error is as follows:

CN0014005932:~ zhaoqin$ docker run-- name=test bolingcavalry/hellojib:0.0.1-SNAPSHOTError: Could not find or load main class ${start-class}

Docker ps-a checks the container information as follows. You can only see that the status is "exit", and nothing else:

CN0014005932:~ zhaoqin$ docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd618f6588821 bolingcavalry/hellojib:0.0.1-SNAPSHOT "java-Xms4g-Xmx4g..." 4 minutes ago Exited (1) 4 minutes ago test

Not reconciled, use the command docker ps-a-- no-trunc to view the untruncated container information:

CN0014005932:~ zhaoqin$ docker ps-a-- no-truncCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd618f6588821f00d3bd0b67a85ff92988b90dfff710370c9d340d5c544c550af bolingcavalry/hellojib:0.0.1-SNAPSHOT "java-Xms4g-Xmx4g-cp / app/resources:/app/classes:/app/libs/* ${start-class}" 7 minutes ago Exited (1) 7 minutes ago test

A new discovery has been made this time. The command to execute when the container starts is java-Xms4g-Xmx4g-cp / app/resources:/app/classes:/app/libs/* ${start-class}. How strange! What is this ${start-class}? Let's take a look at a normal image startup command:

Java-Xms4g-Xmx4g-cp / app/resources:/app/classes:/app/libs/* com.bolingcavalry.jiberrordemo.JibErrorDemoApplication

As shown above, com.bolingcavalry.jiberrordemo.JibErrorDemoApplication is the class where the main method belongs, and this command can run the main method of the JibErrorDemoApplication class normally; 6. Summary problem: when the container starts, it executes the java command and passes ${start-class} as a parameter to java. As a result, java cannot handle this parameter, so the process reports an error, causing the container to exit.

Cause of the problem

The reason for this problem is simple: there is more than one class with main method in java project. Check the code of jib-error-demo project and find that there is indeed a main method in Utils.java:

Public class Utils {public static String time () {return new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") .format (new Date ()) .toString ();} public static void main (String [] args) {System.out.println (time ());}}

Delete the above main method, then build the image and run the container to confirm that the problem has been solved.

Another way to solve the problem

If you do not want to change the code of the Utils class (maybe a class in the jar package has a main method), open the pom.xml file and add a mainClass node to the configuration of the jib plug-in. The node content is the specified class class, as shown in the red box below: the problem can also be solved after the above settings.

Next, if you are interested in understanding deeper reasons, let's explore in depth.

Find the problem

This problem is recorded on Jib's official GitHub. First, the address is: https://github.com/GoogleContainerTools/jib/issues/1601. The red box below shows the same problem. Finally, the initiator of issue shut down the issue himself because he found that there were two classes with main methods in his project:

Let's take a look at this issue, https://github.com/GoogleContainerTools/jib/issues/170, Jib author Q Chen speculates that Spring sets the string ${start-class} to the value of the Main-Class attribute (in my opinion, Spring is spring boot's mave plug-in), so when the Jib plug-in is worth using Main-Class, it gets the string ${start-class}:

The subsequent plot of this issue is very interesting, and Jib author Q Chen also struggles with this problem. If multiple classes with main methods are found in the Java project, what on earth should Jib do? Q Chen finally decides to output a warning, as shown in the following figure:

Let's take a look at this code, that is, in the figure above, the address is https://github.com/GoogleContainerTools/jib/pull/228/files/c8757e1f9ea47edd78df18142de7836a68f22034. If mainClass is not like the name of a class class, output a warning. This logic is written in both Gradle and Maven plug-ins:

One last question: where did the mainClass in the above code come from? Open the source code of the above figure, the address is: https://github.com/GoogleContainerTools/jib/blob/c8757e1f9ea47edd78df18142de7836a68f22034/jib-maven-plugin/src/main/java/com/google/cloud/tools/jib/maven/BuildImageMojo.java, the following figure red box, it can be inferred from the method name, this value comes from the maven plug-in for building the SpringBoot project, so the previous Q Chen mentioned that the value of the main-class variable is modified by Spring, it should come from this code: at this time, if you are still not done. Let's consolidate SpringBoot's start-class.

About start-class

Students who are familiar with SpringBoot are no stranger to ${start-class}. When there are main methods in multiple classes in the project, use this parameter to specify the startup class of SpringBoot.

First, take a look at the official SpringBoot document to familiarize yourself with start-class. The address is: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/. The following figure is more important: the startup class we set is assigned to the Start-Class attribute, and the Main-Class attribute becomes org.springframework.boot.loader.JarLauncher. This is the real startup class of SpringBoot:

As shown in the figure below, this is a supplementary note. The action in which the value of the Main-Class attribute is transferred to the Start-Class attribute is done by the maven plug-in when building the jar:

So the value of start-class comes from main-class. Let's see where the value of main-class comes from. As shown in the red box below, the maven plug-in will look for classes with public static void main (String [] args):

Thank you for your reading. the above is the content of "problem Analysis of Jib Construction Image". After the study of this article, I believe you have a deeper understanding of the problem analysis of Jib construction image, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report