Gradle jar error
Closed this issue · 2 comments
Hi guys, I'm using gradle to manage dependencies.
When exporting the jar, I run it using cmd java -jar app.jar
However, when trying to connect via websocket, the error.
I've tried everything but I can't run my project through .jar
New state is connecting
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: org/java_websocket/exceptions/WebsocketNotConnectedException
at io.ably.lib.transport.WebSocketTransport$Factory.getTransport(WebSocketTransport.java:39)
at io.ably.lib.transport.WebSocketTransport$Factory.getTransport(WebSocketTransport.java:36)
at io.ably.lib.transport.ConnectionManager.connectImpl(ConnectionManager.java:1518)
at io.ably.lib.transport.ConnectionManager.access$400(ConnectionManager.java:38)
at io.ably.lib.transport.ConnectionManager$Connecting.enact(ConnectionManager.java:227)
at io.ably.lib.transport.ConnectionManager$StateChangeAction.enactState(ConnectionManager.java:572)
at io.ably.lib.transport.ConnectionManager$AsynchronousStateChangeAction.run(ConnectionManager.java:616)
at io.ably.lib.transport.ConnectionManager$ActionHandler.run(ConnectionManager.java:726)
at java.base/java.lang.Thread.run(Thread.java:1575)
Caused by: java.lang.ClassNotFoundException: org.java_websocket.exceptions.WebsocketNotConnectedException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:528)
... 9 more
Hey @schneiderbruno,
Thanks for raising this! Could you provide your build.gradle(.kts)
file? Note that by default, Gradle builds a JAR archive without dependencies. A JAR archive that includes all dependencies is usually called a 'fat' JAR, and you need to configure a task to build it. Example in Kotlin:
tasks.register<Jar>("fatJar") {
manifest { attributes(mapOf("Main-Class" to application.mainClass)) } // Provided we set it up in the application plugin configuration
archiveClassifier.set("standalone") // Naming the jar
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
with(tasks.jar.get())
}
Example in Groovy:
task fatJar(type: Jar) {
manifest { attributes 'Main-Class': application.mainClass }
archiveBaseName = 'standalone'
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
@ttypic
Thanks for reply
Of course,
follow gradle file
I'm using netbeans because I'm more familiar
`plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
// This dependency is used by the application.
//implementation 'com.google.guava:guava:31.1-jre'
implementation 'net.java.dev.jna:jna:4.5.0' // multipluscard tef
implementation 'io.ably:ably-java:1.2.42' // ably
implementation 'com.google.code.gson:gson:2.8.6' // ably
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(12)
}
}
application {
// Define the main class for the application.
mainClass = 'SisfoodTefTotemGradle.Main'
}
tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
jar {
manifest {
attributes(
//'Main-Class': 'com.SisfoodTefTotemGradle.Main' nao deu certo
'Main-Class': 'SisfoodTefTotemGradle.Main',
'Class-Path': '/libs/*.jar'
)
}
//duplicatesStrategy = DuplicatesStrategy.INCLUDE
from {
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}`