I'm trying to create a console application with Kotlin, so when trying to run the jar, it returns the message "Could not find or load main class". When I run it through IntelliJ, it runs normally but when I try to run it via the command line java -jar C:\wavecounter\bin\wavecounter_service.jar
, the error occurs.
WaveCounterService.kt
package br.com.mawan.waveCounterService
fun main(args: Array<String>) {
println("Hello world!!")
}
build.gradle
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
}
group = "br.com.mawan.waveCounterService"
version = "1.0.0"
ext {
dir_install = "c:\\wavecounter"
}
repositories {
mavenCentral()
jcenter()
maven { setUrl("https://dl.bintray.com/kotlin/exposed") }
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.72")
implementation("org.jetbrains.exposed:exposed:0.10.4")
implementation("org.slf4j:slf4j-api:1.7.26")
implementation("ch.qos.logback:logback-classic:1.2.3")
implementation("ch.qos.logback:logback-core:1.2.3")
implementation("com.google.code.gson:gson:2.8.5")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5")
implementation("com.microsoft.sqlserver:mssql-jdbc:9.4.1.jre8")
implementation("mysql:mysql-connector-java:8.0.18")
implementation("net.sourceforge.jtds:jtds:1.3.1")
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
task backup_file_config(type: Copy) {
from "${dir_install}\\config\\config_service.json"
into "src/main/resources/"
}
task copy_files_bin(type: Copy) {
from "${buildDir}\\libs\\wavecounter_service-${version}.jar"
into "${dir_install}\\bin"
rename ("wavecounter_service-${version}.jar", "wavecounter_service.jar")
}
task waveCounterServiceJar(type: Jar) {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
manifest {
attributes 'Implementation-Title': 'WaveCounterService',
'Main-Class': 'br.com.mawan.waveCounterService.WaveCounterServiceKt'
}
archivesBaseName = "wavecounter_service"
configurations.implementation.setCanBeResolved(true)
from {
exclude '**/module-info.class'
configurations.implementation.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
doFirst {
mkdir "${dir_install}\\backup\\mysql"
}
finalizedBy(copy_files_bin, backup_file_config)
}
CodePudding user response:
I solved the problem but I can't explain why it worked. If anyone knows I would spend an explanation. I was looking at an old project and in its gradle there was the command "exclude 'META-INF/.RSA'*". I put this command in the jar creation task and it worked.
task waveCounterServiceJar(type: Jar) {
exclude 'META-INF/*.RSA'
duplicatesStrategy = DuplicatesStrategy.INCLUDE
manifest {
attributes 'Implementation-Title': 'WaveCounterService',
'Main-Class': 'br.com.mawan.waveCounterService.WaveCounterServiceKt'
}
archivesBaseName = "wavecounter_service"
configurations.implementation.setCanBeResolved(true)
from {
exclude '**/module-info.class'
configurations.implementation.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
doFirst {
mkdir "${dir_install}\\backup\\mysql"
}
finalizedBy(copy_files_bin, backup_file_config)
}