0

I build a jar file with Intellij IDEA 2019.1.3. It's a very simple Helloworld project. But once i use java jar xxx.jar, an error happened: Can not find or load main class. But i have defined main class. Here is the content in my META-INF/MANIFEST.MF file:

Manifest-Version: 1.0
Class-Path: .
Main-Class: apk.Main

And here is my jar file structure:

-apk
--Main
-META-INF
--MANIFEST.MF
--abc.kotlin_module

My build.grale file as below:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.31'
}

group 'abc'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

jar {
    manifest {
        attributes (
                "Class-Path": ".",
                "Main-Class": "apk.Main"
        )
        /*from {
            configurations.dependencies.collect { it.isDirectory() ? it : zipTree(it) }
        }*/
    }
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

It seems everything is ok in this jar. But what is wrong with my jar?

boybeak
  • 417
  • 5
  • 19
  • 1
    The correct comma,d is `java -jar`, not `java jar`. – JB Nizet Jun 16 '19 at 16:15
  • OK, i use java -jar command, but another error: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics – boybeak Jun 16 '19 at 16:23
  • Your app needs its dependencies (the kotlin library in this case) in its classpath. https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html – JB Nizet Jun 16 '19 at 16:28
  • Thank you. Finally this article solved my problem.https://medium.com/@preslavrachev/kotlin-basics-create-executable-kotlin-jars-using-gradle-d17e9a8384b9 – boybeak Jun 17 '19 at 05:38
  • You must use `compile` instead of `implementation` in dependencies – boybeak Jun 17 '19 at 05:42

0 Answers0