Home > other >  Could not find or load main java class jar on AWS EC2 Linux
Could not find or load main java class jar on AWS EC2 Linux

Time:12-04

I need to run on Linux on AWS EC2 me jar file. I have a multi-project on Gradle. I know that there are many analogs of my problem, but I did not find anything with the multi-project.

The structure of my project is as follows

demo
   -core
       com.example
       build.gradle
   -web
       build.gradle
       com.example
          DemoApplication.java(main)
   build.gradle

My build.gradle in demo.

plugins {
    id 'org.springframework.boot' version '2.6.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

jar {
    manifest {
        attributes 'Main-class' : 'com.example.DemoApplication'
    }
}


configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-ldap'
    implementation 'org.springframework.boot:spring-boot-starter-hateoas'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    runtimeOnly 'org.postgresql:postgresql'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

test {
    useJUnitPlatform()
}

When I execute the command jar, I get jar file that I run on Linux java -jar '/home/ec2-user/demo-0.0.1-SNAPSHOT-plain.jar' and it gives me an error.

Could not find or load main class com.example.DemoApplication

CodePudding user response:

As a result, I tricky with the Gradle and Spring-boot versions. What did I do to make it work:

  1. Created a new project in start.spring.io with Java 11 and Spring Boot 2.5.8(SNAPSHOT).
  2. Moved the whole project to a new one.
  3. I also used 2 commands before gradle clean and gradle build. And in the working version I used gradlew clean build.
  4. And launched the jar file in Linux as usual.

Hope it helps someone someday :)

  • Related