question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multi-module spring-boot project, container can't be started, got Error "Could not find or load main class"

See original GitHub issue

Environment:

  • *Jib version: * 2.2.0
  • Build tool: Maven 3.6.3
  • *OS: * Windows 10

Description of the issue: Hello,

i have a multi-module spring-boot-project, as follows the project structure: (root - Parent Project “Zi_DeliveryService”) ± delivery-service # Sub-Module with a main class, it has module “delivery-common” as dependency ± delivery-common #Sub-Module, does not have main class

However, by running “mvn clean deploy jib:build” in the root, the image can be built and pushed into image-repo, but the container cannot be started, and the error message is “Could not find or load main class com.zi.delivery.Application”.

If i want to containerize the sub-module, by runing “mvn clean package jib:build -pl delivery-service -am” Image can not even be built, I got unauthorized error

Ways to reproduce: Way 1.
If i run Jenkins-Skript: sh ‘mvn clean deploy jib:build -Dmaven.test.skip=true -Djib.from.auth.username=$imageRepoUser -Djib.from.auth.password=$imageRepoPassword -Djib.to.auth.username=$imageRepoUser -Djib.to.auth.password=$imageRepoPassword’

Image can be built and pushed to image-repo, no unauthorized error. But Container cannot be started, Error message: “Could not find or load main class com.zi.delivery.Application” in the pod

Way 2. Jenkins-Skript: sh ‘mvn clean deploy jib:build -pl delivery-service -am -Dmaven.test.skip=true -Djib.from.auth.username=$imageRepoUser -Djib.from.auth.password=$imageRepoPassword -Djib.to.auth.username=$imageRepoUser -Djib.to.auth.password=$imageRepoPassword’

The jenkins-job will fail and the error message is: [ERROR] I/O error for image [harbor.zi.com:443/open-jdk-8u242-base-image]: [ERROR] java.net.SocketException [ERROR] Socket closed [INFO] ------------------------------------------------------------------------ [INFO] ZiDeliveryService …SUCCESS [ 13.479 s] [INFO] delivery-common …SUCCESS [ 4.247 s] [INFO] delivery-service …FAILURE [ 8.629 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [ERROR] Failed to execute goal com.google.cloud.tools:jib-maven-plugin:2.2.0:build (default-cli) on project delivery-service: Build image failed, perhaps you should make sure your credentials for ‘harbor.zi.com:443’ are set up correctly. Unauthorized for harbor.zi.com:443: 500 Internal Server Error -> [Help 1]

Details: (1) There is @SpringBootApplication Annotation in the main Class (2) I only wrote one main class, which is in the delivery-service module (3) In the parent-pom, the jib-plugin is in the part PluginManagement; in the child-pom of module “delivery-service”, i must specify the mainClass with <mainClass>com.zi.delivery.Application</mainClass>, otherwise the error message will be “Could not find or load main class ${start-class}”; in the child-pom of the module “delivery-common”, i set jib.skip as true.

jib-maven-plugin Configuration in parent pom:

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.google.cloud.tools</groupId>
                    <artifactId>jib-maven-plugin</artifactId>
                    <version>${jib-maven-plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring.boot.version}</version>
                </plugin>
              </plugins>
            </pluginManagement>
         </build>

jib-maven-plugin Configuration in child pom of the module “delivery-service”, which contains a main class:

<build>
                <plugins>
                    <plugin>
                        <groupId>com.google.cloud.tools</groupId>
                        <artifactId>jib-maven-plugin</artifactId>
                        <configuration>
                            <container>
                                <mainClass>com.zi.delivery.Application</mainClass>
                                <jvmFlags>
                                    <jvmFlag>-Xmx3G</jvmFlag>
                                </jvmFlags>
                            </container>
                            <from>
                                <image>${image.from}</image>
                            </from>
                            <to>
                                <image>${image.to}</image>
                                <tags>
                                    <tag>${project.version}</tag>
                                    <tag>${image.tag}</tag>
                                </tags>
                            </to>
                        </configuration>
                    </plugin>
                    <plugin>
                       <groupId>org.springframework.boot</groupId>
                       <artifactId>spring-boot-maven-plugin</artifactId>
                       <configuration>
                             <mainClass>com.zi.delivery.Application</mainClass>
                            <executable>true</executable>
                       </configuration>
                       <executions>
                          <execution>
                             <goals>
                                <goal>repackage</goal>
                             </goals>
                         </execution>
                      </executions>
                   </plugin>
                </plugins>
            </build>

jib-maven-plugin Configuration in child pom of the other module “delivery-common”, which does not have any main class, and jib.skip is set as true:

    <properties>
        <jib.skip>true</jib.skip>
    </properties>

Please give me some advise, thank you very much.

Best regards, Zi

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SangZicommented, May 25, 2020

Hi @chanseokoh,

today i tried the following and the Error “Could not find or load main class” is gone, and i can build and push image to harbor, and i also can start container.

What i did:

1. parent-pom: only do one thing, set jib in as profile

    <profile>
        <id>jib</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.google.cloud.tools</groupId>
                    <artifactId>jib-maven-plugin</artifactId>
                    <version>${jib-maven-plugin.version}</version>
                    <configuration>
                        <container>
                           <!-- THE MAIN CLASS PATH OF YOUR MODULE WITH MAIN CLASS -->
                            <mainClass>com.zi.delivery.Application</mainClass>
                        </container>
                        <from>
                            <image>${image.from}</image>
                        </from>
                        <to>
                            <image>${image.to}</image>
                            <tags>
                                <tag>${project.version}</tag>
                                <tag>${image.tag}</tag>
                            </tags>
                        </to>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

2. In the module with mainClass, in my use-case “delivery-service”

  • set NOTHING regarding jib

  • set “spring-boot-maven-plugin” as follows:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.zi.delivery.Application</mainClass>
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </build>

I refered to the link https://github.com/GoogleContainerTools/jib/issues/1539 , and the answer from andxu

3. In all the other modules, without mainClass, set jib.skip, in my use-case “delivery-common”

<properties>
    <jib.skip>true</jib.skip>
</properties>

4. Run with

mvn clean deploy jib:build -Pjib -Dmaven.test.skip=true -Djib.from.auth.username=$harboruser -Djib.from.auth.password=$harborpassword -Djib.to.auth.username=$harboruser -Djib.to.auth.password=$harborpassword

PS: at the end, through it works. But, i could not explain, why i cannot jib:build locally in console and get the Unauthorized 500 Internal Server Error from Harbor, and why it only works via jenkins-job.

BR, Zi

0reactions
SangZicommented, May 26, 2020

Hi @chanseokoh

thank you very much!

Yes, as you said, in my case, the problem is, spring-boot-maven-plugin runs in both delivery-service and deliver-common, as this plugin is defined in <pluginManagement>, and run in all sub-modules, so <goal>repackage</goal> is set in the module with mainClass and not only .jar file, but also a .jar.original file is created in this sub-module.

I am thinking maybe some configuration is missing in my computer (proxy or something else), i am trying to set no_proxy for the harbor link on my computer. If i can do it sucessfully, I will give an update here.

BR, Zi

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve could not find or load main class error with ...
Go to Run -->Run Configurations- classpath , in user entries add your project. · Go to source tab of Run configurations and add...
Read more >
Could Not Find or Load Main Class Error | Java - StackChief
This error is very common when creating new Java based projects. Whether you're using Gradle or Maven, Spring Boot or Kafka, chances are...
Read more >
[Solved] Error: Could not find or load main class | Java Hungry
Today we will look into another common problem for java beginners,i.e how to fix "error: could not find or load main class" error...
Read more >
Run and Debug Java in Visual Studio Code
See how you can run and debug your Java source code locally, and in the cloud.
Read more >
Developing with Spring Boot
In practice, you do not need to provide a version for any of these dependencies in your build configuration, as Spring Boot manages...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found