ClassNotFoundException When running jar file but runs fine in Intellij - java

I created a small mqtt application using eclipse paho mqtt library in kotlin with Gradle in Intellij IDE. it runs fine when running it through Intellij but when I build it and run the jar file that gets created I get a NoClassDefFoundError error.
From other questions I have seen about this it looks like it has something to do with the class path but I am not sure what needs to be done if that is indeed the issue because I am using gradle and not jar files for libraries.
I was following this tutorial
Here is my gradle file
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.31'
id 'application'
}
group = 'me.package'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
}
}
dependencies {
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
test {
useJUnit()
}
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
}
compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
}
application {
mainClassName = 'com.publisher.MainKt'
}
tasks.jar {
manifest {
attributes 'Main-Class': 'com.publisher.MainKt'
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
And my MainKt file
package com.publisher
import org.eclipse.paho.client.mqttv3.*
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence
import java.io.File
fun main(args: Array<String>) {
val client = MqttClient("tcp://192.168.0.55:1883","publisher", MemoryPersistence())
val connOpts = MqttConnectOptions()
connOpts.isCleanSession = false
connOpts.isAutomaticReconnect = true
client.setCallback(object: MqttCallback {
override fun connectionLost(cause: Throwable?) {
println("Connection lost")
println(cause!!.message)
}
override fun messageArrived(topic: String?, message: MqttMessage?) {
println("Message Received for topic: $topic")
println("Message: ${message!!.payload}")
}
override fun deliveryComplete(token: IMqttDeliveryToken?) {
println("Message delivered")
}
})
try{
client.connect(connOpts)
println("Connected")
client.subscribe("config/+", 1) { topic, message ->
println("Getting configuration for $message")
val path = System.getProperty("user.dir")
val file = File("$path/${message}.json")
if(file.exists()){
client.publish("/devices/ + $message + /config", MqttMessage(file.readBytes()))
}
}
}catch (e: MqttException){
println("Error: ${e.localizedMessage}")
e.printStackTrace()
}
}

The way you start your application does not include the dependencies, meaning your MQTT driver and the Kotlin dependencies are not included.
Do the following:
gradle distZip
# alternatively
gradle distTar
This will create a zip/tar file containing all the dependencies and a start script. Use that to start your application.

You could consider the Shadow plugin, as it is straightforward to use. Your build.gradle would look something like this:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.31'
// Shadow plugin
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'java'
}
group = 'me.package'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
}
}
dependencies {
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
test {
useJUnit()
}
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
}
compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
}
application {
mainClassName = 'com.publisher.MainKt'
}
tasks.jar {
manifest {
attributes 'Main-Class': 'com.publisher.MainKt'
}
}
So your fat JAR is generated in the /build/libs directory with all the dependencies included.

Related

Web3j plugin for gradle task not found

I am beating against a wall for a whole week. I can't really see the reason why it doesn't work. I have a project where I want to run my Java project with the following build.gradle config:
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.3'
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id 'org.web3j' version "4.8.4"
}
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
jcenter()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
group = 'com.suplab'
sourceSets {
main {
solidity {
srcDir "contracts"
}
}
test {
solidity {
srcDir "contracts"
}
}
}
ext {
web3jVersion = '4.8.4'
}
npmInstall {
enabled false
}
dependencies {
implementation "org.web3j:core:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate:hibernate-core:5.6.5.Final'
}
I use gradle version 6.8, since it was specified by developer of the plugin. When I try to build this project from the parent, root folder that is the root of the project, it fails with following error:
A problem occurred configuring project ':api'.
> Failed to notify project evaluation listener.
> Task with name 'resolveSolidity' not found in project ':api'.
> Could not create task ':api:generateTestContractWrappers'.
> Task with name 'compileTestSolidity' not found in project ':api'.
I can't figure it out. I checked it against the build that being generated with web3j-cli, and it's the same for the configuration property (both configs have it blank). So, I don't see any reason WHY it fails. Here's the webj3-cli generated build file:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'application'
id "com.github.johnrengelman.shadow" version "5.2.0"
id 'org.web3j' version '4.8.4'
}
group 'org.web3j'
version '0.1.0'
sourceCompatibility = 1.8
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
jcenter()
}
web3j {
generatedPackageName = 'org.web3j.generated.contracts'
excludedContracts = ['Mortal']
}
ext {
web3jVersion = '4.8.4'
logbackVersion = '1.2.3'
}
dependencies {
implementation "org.web3j:core:$web3jVersion",
"ch.qos.logback:logback-core:$logbackVersion",
"ch.qos.logback:logback-classic:$logbackVersion"
implementation "org.web3j:web3j-unit:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
jar {
manifest {
attributes(
'Main-Class': 'org.web3j.Web3App',
'Multi-Release':'true'
)
}
}
application {
mainClassName = 'org.web3j.Web3App'
}
test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

unnamed module #0x7cd3a5 error while running tests for java application(with module system enabled) using gradle

I am trying to run the test for java applications (with module system enabled) using Gradle and getting the following error.
java.lang.IllegalAccessError: class org.junit.platform.launcher.core.LauncherFactory (in unnamed module #0x7cd3a5) cannot access class org.junit.platform.commons.util.Preconditions (in module org.junit.platform.commons) because module org.junit.platform.commons does not export org.junit.platform.commons.util to unnamed module #0x7cd3a5
Error says module org.junit.platform.commons does not export org.junit.platform.commons.util
This is how my build file looks like:
import org.springframework.boot.gradle.plugin.SpringBootPlugin
plugins {
id 'java'
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id "org.beryx.jlink" version "2.21.0"
id "org.javamodularity.moduleplugin" version "1.6.0"
}
repositories {
gradlePluginPortal()
jcenter()
}
sourceCompatibility = JavaVersion.VERSION_14
targetCompatibility = JavaVersion.VERSION_14
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
jar {
enabled = true;
}
application{
mainModule='com.sudhir.registration'
}
test {
useJUnitPlatform()
}
configurations {
springFactoriesHolder { transitive = false }
}
dependencyManagement {
imports {
mavenBom SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
springFactoriesHolder 'org.springframework.boot:spring-boot-actuator-autoconfigure'
springFactoriesHolder 'org.springframework.boot:spring-boot-autoconfigure'
springFactoriesHolder 'org.springframework.boot:spring-boot'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
exclude group: 'com.vaadin.external.google', module: 'android-json'
}
}
mainClassName = "com.sudhir.registration.ApiApplication"
prepareMergedJarsDir.doLast {
// extract and merge META-INF/spring.factories from springFactoriesHolder
def factories = configurations.springFactoriesHolder.files.collect {
def props = new Properties()
props.load(zipTree(it).matching { include 'META-INF/spring.factories' }.singleFile.newInputStream())
props
}
def mergedProps = new Properties()
factories.each { props ->
props.each { key, value ->
def oldVal = mergedProps[key]
mergedProps[key] = oldVal ? "$oldVal,$value" : value
}
}
def content = mergedProps.collect { key, value ->
def v = (value as String).replace(',', ',\\\n')
"$key=$v"
}.join('\n\n')
mkdir("$jlinkBasePath/META-INF")
new File("$jlinkBasePath/META-INF/spring.factories").text = content
// insert META-INF/spring.factories into the main jar
ant.zip(update: "true", destfile: jar.archivePath, keepcompression: true) {
fileset(dir: "$jlinkBasePath", includes: 'META-INF/**')
}
}
jlink {
imageZip = file("$buildDir/image-zip/registration-service-image.zip")
options = ['--strip-java-debug-attributes', '--compress', '2', '--no-header-files', '--no-man-pages']
forceMerge 'jaxb-api', 'byte-buddy', 'classgraph'
mergedModule {
uses 'ch.qos.logback.classic.spi.Configurator'
excludeRequires 'com.fasterxml.jackson.module.paramnames'
excludeProvides implementation: 'com.sun.xml.bind.v2.ContextFactory'
excludeProvides servicePattern: 'javax.enterprise.inject.*'
excludeProvides service: 'org.apache.logging.log4j.spi.Provider'
excludeProvides servicePattern: 'reactor.blockhound.integration.*'
}
launcher {
name = 'run'
jvmArgs = [
'--add-reads', 'registration.service.merged.module=com.sudhir.registration',
'-cp', 'config/',
]
}
}
tasks.jlink.doLast {
copy {
from "src/main/resources"
into "$imageDir.asFile/bin/config"
}
copy {
from "$buildDir/classes/java/main/com/sudhir/registration"
into "$imageDir.asFile/bin/config/com/sudhir/registration/for-spring-classpath-scanner"
}
}
However, I am able to run specific tests in IntelliJ. I think it's because Intellij is using classpath instead of module path.
I am using gradle-module-system plugin to build, test and run. sample code can be found here.
Can someone please assist me how to deal with this issue. I am very new to using java module system.
Not quite sure on the reason of this problem. These threads may help to understand it: this, this, this or this
But this is what helped me in a similar case:
test {
moduleOptions {
runOnClasspath = true
}
useJUnitPlatform()
}
(Adding moduleOptions setting to the test task)

Building Java 11 Project using Gradle plus JavaFX and dependencies

I am trying to build my Java 11 project to have either an executable jar (FatJar, SuperJar, whatever its called) or an EXE or any form of runnable version even a batch file (Using Application). Everything I try I either get JavaFX is missing or my dependencies included the packages arent visible and it error's out.
Here is my build.gradle
plugins {
id 'application'
id 'java'
id 'maven-publish'
id 'org.openjfx.javafxplugin' version '0.0.8'
id 'edu.sc.seis.launch4j' version '2.4.6'
id 'org.beryx.jlink' version '2.12.0'
}
application {
mainClassName = 'sassa.sassa.Main'
}
repositories {
mavenLocal()
maven {
url = uri('https://jitpack.io')
}
maven {
url = uri('https://repo.maven.apache.org/maven2')
}
}
javafx {
version = "11.0.2"
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.graphics' ]
}
jlink {
launcher {
name = 'sassa'
}
}
dependencies {
implementation 'com.github.toolbox4minecraft:amidst:v4.4-beta1'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
}
jar {
manifest {
attributes("Manifest-Version": "1.0",
"Main-Class": "sassa.main.Main");
}
}
compileJava {
doFirst {
println "CLASSPATH IS $classpath.asPath"
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'javafx.graphics',
'--add-modules', 'javafx.controls',
'--add-modules', 'javafx.fxml'
]
classpath = files()
}
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
launch4j {
mainClassName = 'sassa.main.Main'
icon = "${projectDir}/src/main/resources/sassa/sassa.ico"
jreMinVersion = '11'
jreMaxVersion = '14'
jdkPreference = 'preferJre'
initialHeapSize = 128
maxHeapSize = 512
stayAlive = false
bundledJre64Bit = true
dontWrapJar = true
bundledJrePath = 'jre'
}
group = 'sassa'
version = '0.5.0'
sourceCompatibility = '11'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
Also for the layout of the project it is on github (I was using maven before but figured Gradle might work better. (The maven code is still on github) https://github.com/Zodsmar/SeedSearcherStandaloneTool/tree/development
Literally I have tried everything and I just can't seem to get a buildable version to distribute...
Also I have read up about module.info files I do not have any I want to have a simple build.gradle that just includes everything I need to build an executable.
To anyone whoever comes across this and wants to know how I fixed it. I created a Gradle build task and then was able to build Jars, EXEs, Tar, and Zip this is the gradle:
plugins {
id 'application'
id 'java'
id 'maven-publish'
id 'org.openjfx.javafxplugin' version '0.0.8'
id 'edu.sc.seis.launch4j' version '2.4.6'
}
dependencies {
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation ('com.github.KaptainWutax:BiomeUtils:master-SNAPSHOT') {
transitive = false;
changing = true;
}
implementation ('com.github.KaptainWutax:FeatureUtils:master-SNAPSHOT')
{
transitive = false;
changing = true;
}
implementation ('com.github.KaptainWutax:SeedUtils:master-SNAPSHOT')
{
transitive = false;
changing = true;
}
}
group = 'sassa'
version = 'v0.6.0'
sourceCompatibility = '1.8'
String stringVersion = version
javafx {
version = "11.0.2"
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.graphics' ]
}
application {
mainClassName = 'sassa.main.Main'
}
jar {
manifest {
attributes 'Main-Class': 'sassa.main.Main'
}
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
launch4j {
mainClassName = 'sassa.main.Main'
outfile = group + "-" + stringVersion + ".exe"
icon = "${projectDir}/src/main/resources/sassa/sassa.ico"
}
repositories {
mavenLocal()
maven {
url = uri('https://jitpack.io')
}
maven {
url = uri('https://repo.maven.apache.org/maven2')
}
}
task buildAll(type: GradleBuild) {
tasks = ['jar', 'createExe', 'assemble']
}
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}

Kotlin+Gradle runnable jar - dependency causing "Could not find or load main class"

Gradle build file:
buildscript {
ext.kotlin_version = '1.2.41'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
group 'com.anivive'
version '0.1'
apply plugin: 'kotlin'
sourceSets {
main.java.srcDirs += 'src/main/kotlin/'
test.java.srcDirs += 'src/test/kotlin/'
}
repositories {
mavenLocal()
maven {
url "https://packagecloud.io/anivive/anivive/maven2"
}
mavenCentral()
}
dependencies {
compile 'anivive:Import_Utils:1.2.44'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar {
baseName = 'XMLModelBuilder'
version = '0.1'
manifest {
attributes 'Main-Class': 'com.anivive.Main'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
File structure:
src
- main
- kotlin
- com
- anivive
- Main.kt
Main.kt:
package com.anivive
import com.anivive.util.ExampleClass
object Main {
#JvmStatic
fun main(args: Array<String>) {
ExampleClass.exampleMethod()
}
}
So, I'm trying to build a runnable jar file that will call a single method from a class in the dependency 'anivive:Import_Utils:1.2.44'.
If I remove the dependency, and change my main method to just println("Test"), the jar will run fine via java -jar build/libs/jarFile.jar and it will print out Test.
However, with the dependency included and when trying to call a function from it, I will get Error: Could not find or load main class com.anivive.Main whenever I try to run the Jar file. I suspect it has something to do with hosting the jar file on packagecloud, but I'm surprised this is a problem, considering I can run the program just fine from IntelliJ.
What gives?

Add Library to Gradle Kotlin app

I converted simple Kitlon file into Library, the file is:
Display.kt:
package hello
fun main(args: Array<String>) {
println("Hello World!")
}
had been compiled into library using the command:
kotlinc Display.kt -d Display.jar
The output had been cross checked to be worked using the command:
kotlin -classpath Display.jar hello.DisplayKt
Then I moved it to folder src/main/resources, then tried calling it from another app, using the below code:
Hello.kt:
package hello
import hello.DisplayKt
fun main(args: Array<String>) {
println("Hi")
}
and defined the build.gradle file as below (tried to put all option I read about to solve my case):
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenCentral()
}
dependencies {
//dependencies from a remote repositor
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
//local file
compile files('src/main/resources/Display.jar')
compile fileTree(dir: 'src/main/resources', include: '*.jar')
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes ('Main-Class': 'hello.HelloKt', "Implementation-Title": "Gradle",
"Implementation-Version": 1)
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceSets {
main {
java {
srcDirs = ['src/kotlin']
}
resources {
srcDirs = ['src/resources']
}
}
}
but, after running gradle build command, I got the below error:
Unresolved reference: DisplayKt
Note: I'm very very new to JAVA/KOTLIN and GRADLE
I found the answer, the full path of the function is hello.main

Categories

Resources