Home > front end >  Java Gradle : Could not find or load main class
Java Gradle : Could not find or load main class

Time:01-12

I'm trying to build and run a java project using Gradle.
The project building succeeded. However, the error "Could not find or load main class ~" has occurred when running it.
I found and tried a solution like this. But the solution didn't work.

My project structure is here.

javaAddressRecorder
├── build
│   ├── classes
│   │   └── java
│   │       └── main
│   │           ├── DbAddressRecorder.class
│   │           ├── Geth.class
│   │           └── JavaAddressRecorder.class
│   ├── generated
│   │   └── sources
│   │       ├── annotationProcessor
│   │       │   └── java
│   │       │       └── main
│   │       └── headers
│   │           └── java
│   │               └── main
│   ├── libs
│   │   └── JavaAddressRecorder-1.0-SNAPSHOT.jar
│   └── tmp
│       ├── compileJava
│       │   └── previous-compilation-data.bin
│       └── jar
│           └── MANIFEST.MF
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   ├── DbAddressRecorder.java
    │   │   ├── Geth.java
    │   │   └── JavaAddressRecorder.java
    │   └── resources
    └── test
        ├── java
        └── resources

And my build.gradle file is here.

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
    implementation 'org.web3j:core:5.0.0'
    implementation 'org.slf4j:slf4j-simple:2.0.5'
    runtimeOnly 'mysql:mysql-connector-java:8.0.30'
}

test {
    useJUnitPlatform()
}

jar {
    manifest {
        attributes(
                'Main-Class': 'JavaAddressRecorder'
        )
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

The src/main/java/JavaAddressRecorder.java has main() and doesn't have package name. The src/main/java/DbAddressRecorder.java and src/main/java/Geth.java are used from the main().
So the ./gradlew build works without any errors, but java -jar JavaAddressRecorder-1.0-SNAPSHOT.jar doesn't work with the error below.

Error: Could not find or load main class JavaAddressRecorder
Caused by: java.lang.ClassNotFoundException: JavaAddressRecorder

I confirmed the JavaAddressRecorder is included the jar file like below.

******@****** libs % jar tf javaAddressRecorder-1.0-SNAPSHOT.jar | grep JavaAddressRecorder.class
JavaAddressRecorder.class

I'm a newbie to the Java language.
Anyone who knows how to solve this problem, please let me know.

IDE: IntelliJ IDEA
OS: MacOS 11.6.8
Java: Java(TM) SE Runtime Environment (build 17.0.5 9-LTS-191)
Gradle: 7.5.1

Add META-INF/MANIFEST.MF from the .jar file

******** META-INF % cat MANIFEST.MF 
Manifest-Version: 1.0
Main-Class: JavaAddressRecorder

CodePudding user response:

I found a solution that uses the shadowJar.
The build.gradle is here.

plugins {
    id 'java'
    id ("com.github.johnrengelman.shadow") version "7.1.2"
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
    implementation 'org.web3j:core:5.0.0'
    implementation 'org.slf4j:slf4j-simple:2.0.5'
    runtimeOnly 'mysql:mysql-connector-java:8.0.30'
}

test {
    useJUnitPlatform()
}

tasks.jar {
    manifest.attributes["Main-Class"] = "JavaAddressRecorder"
}

And run

./gradlew shadowJar
  • Related