Home > OS >  ClassNotFoundException in Micronaut
ClassNotFoundException in Micronaut

Time:10-13

few days back i starting learning micronaut ,Since last one week i am trying to resolve below exception but unable to resolve this .

I don't know why this application not able to find main class .Everything seems correct

I try many solution but none of them work for me .

I created basic demo application and created jar file then i used these command to run micronaut application using jar file but got below exception CMD1

java -cp build/libs/demo2-0.1.jar  com.example.Application

CMD2

 java -jar build/libs/demo2-0.1.jar 

Gradle file

plugins {
    id("com.github.johnrengelman.shadow") version "7.0.0"
    id("io.micronaut.application") version "2.0.6"
    id 'java'
}

version = "0.1"
group = "com.example"

repositories {
    mavenCentral()
}

micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.example.*")
    }
}

dependencies {
    // https://mvnrepository.com/artifact/io.micronaut/micronaut-runtime
    implementation("io.micronaut:micronaut-runtime:3.1.0")

    annotationProcessor("io.micronaut:micronaut-http-validation")
    implementation("io.micronaut:micronaut-http-client")
    runtimeOnly("ch.qos.logback:logback-classic")
    implementation("io.micronaut:micronaut-validation")

}


application {
    mainClass.set("com.example.Application")
}
java {
    sourceCompatibility = JavaVersion.toVersion("1.8")
    targetCompatibility = JavaVersion.toVersion("1.8")
}





jar {
    manifest {
        attributes(
                'Main-Class': 'com.example.Application'
        )
    }
}

Exception

Exception in thread "main" java.lang.NoClassDefFoundError: io/micronaut/runtime/Micronaut
        at com.example.Application.main(Application.java:8)
Caused by: java.lang.ClassNotFoundException: io.micronaut.runtime.Micronaut
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

enter image description here

After running given cmd in ans:

BLRETV-MMMMMM:demo2 User$ java -jar build/libs/demo2-0.1-all.jar
Error: Unable to access jarfile build/libs/demo2-0.1-all.jar
BLRETV-MMMMMM:demo2 User$ java -cp build/libs/demo2-0.1-all.jar  com.example.Application
Error: Could not find or load main class com.example.Application

CodePudding user response:

You need to use the -all jar, i.e.

java -cp build/libs/demo2-0.1-all.jar com.example.Application

or

java -jar build/libs//demo2-0.1-all.jar

CodePudding user response:

I would advice first try and run the command line gradlew commands to make sure the app works with runtime. try gradlew test and also gradlew run

You may find our guides helpful as well.

https://guides.micronaut.io/latest/creating-your-first-micronaut-app-gradle-java.html

  • Related