I am making JavaFX application as a university assignment and I want to include a .exe file of the application. However, while trying to build the application
gradlew run
I ran into java.lang.UnsupportedClassVersionError.
My build.gradle:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
javafx {
modules = [ 'javafx.controls', 'javafx.fxml' ]
version = '13'
}
run{
standardInput = System.in
standardOutput = System.out
}
jar {
from {
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'java.frontend.Main'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
mainClassName = 'frontend.Main'
project structure
If I try to use grade -> build -> build and run the build/libs/"the built file" I have two errors:
Error: Could not find or load main class java.frontend.Main
Caused by: java.lang.ClassNotFoundException: java.frontend.Main
Related
I am using spring-boot version 1.5.12 and gradle 5.2.1.
I have added Sentry library in my project as mentioned in its documents like below :
implementation 'io.sentry:sentry:1.7.23'
Gradle downloads the related jarFile in : \.gradle\caches\modules-2\files-2.1\io.sentry\sentry\1.7.23
and also I can see it in my IDE in External Libraries part
then I configed it in my local environment and everything worked fine and I could see my Exception reports in Sentry dashboard.
but after that when I wanted to create a version of project for my production environment, the build operation ended successfully but this new library is not in the folder(which is the result of my build operation named Distribution) which contains myPro.jar and other libraries that I use , I faced the Exception :
Exception in thread "main" java.lang.NoClassDefFoundError: io/sentry/Sentry
at ir.anarestan.ipc.boot.Boot.initSentry(Boot.java:22)
at ir.anarestan.ipc.boot.Boot.main(Boot.java:17)
Caused by: java.lang.ClassNotFoundException: io.sentry.Sentry
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
here is my build.gradle file :
group 'pc-server'
version '0.2.1-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'groovy'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
<some other dependecies>
implementation 'io.sentry:sentry:1.7.23'
}
apply plugin: 'application'
mainClassName = 'ir.anarestan.ipc.boot.Boot'
jar {
manifest {
attributes 'Main-Class': mainClassName,
'Class-Path': configurations.runtime.files.collect {"$it.name"}.join(' ')
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File PC',
'Implementation-Version': version,
'Main-Class': 'ir.anarestan.ipc.boot.Boot'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
ext.distributionDir= "${rootDir}/dist/core"
ext.mainClassName = 'ir.anarestan.ipc.boot.Boot'
jar {
manifest {
attributes("Implementation-Title": project.name,
"Implementation-Version": version,
"Main-Class": mainClassName,
"Class-Path": configurations.compile.collect { it.getName() }.join(' '))
}
destinationDir = file("$distributionDir")
}
task copyLibs(type: Copy){
from configurations.compile
into "$distributionDir"
}
task copyConfig(type: Copy){
from "$projectDir/src/main/resources/"
into "$distributionDir"
}
task distribution(dependsOn: ['copyLibs', 'copyConfig', 'jar']){
}
and I face Exception here :
#SpringBootApplication
#EnableMongoRepositories(basePackages = {"ir.anarestan.ipc.repository.mongodb"})
public class Boot {
public static void main(String[] args) {
SpringApplication.run(Boot.class, args);
Sentry.init();
}
}
any help would be appreciated, it has take me lots of time!!!
The problem occurred, In eclipse, the project was refreshed by right-clicked project > Gradle > Refresh Gradle Project
I have tried different approaches, downloading \gradle-4.3.1 file, and by going window > preferences > local installation directory > c:\gradle-4.3.1
Gradle Project structure
apply plugin: 'java'
repositories {
mavenCentral()
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:6.1.0"
}
}
apply plugin: "com.github.johnrengelman.shadow"
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'com.XXXX.webservices.samples.Controller'
}
baseName = 'XXXXX'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
jar {
baseName = 'XXXX'
from('src/main') {
include 'resources/*.properties'
}
manifest {
attributes 'Main-Class': 'com.netsuite.webservices.samples.Controller'
}
}
dependencies {
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
compile 'axis:axis:1.4'
runtime 'commons-httpclient:commons-httpclient:3.1'
runtime 'org.slf4j:log4j-over-slf4j:1.7.6'
runtime 'org.slf4j:slf4j-nop:1.7.25'
compileOnly 'com.google.code.findbugs:jsr305:3.0.1'
}
apply plugin: 'com.github.johnrengelman.shadow'
task runApplication(type: JavaExec) {
description "Builds and executes the sample application"
classpath = sourceSets.main.runtimeClasspath
main = "com.netsuite.webservices.samples.Controller"
standardInput = System.in
}
I have a project configured with Gradle and Kotlin. It's a command line utility and I would like to be able to run the generated jar from my terminal. However I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at com.autentia.impt.MainKt.main(Main.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
My gradle configuration is as follows:
buildscript {
ext.kotlin_version = '1.2.20'
ext.junit_platform_version = '1.0.1'
ext.junit_version = '5.0.0'
ext.moshi_version = '1.5.0'
ext.jna_version = '4.5.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.junit.platform:junit-platform-gradle-plugin:$junit_platform_version"
}
}
version '1.0-SNAPSHOT'
apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'org.junit.platform.gradle.plugin'
mainClassName = 'com.autentia.impt.MainKt'
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.jetbrains.kotlin:kotlin-reflect"
implementation "com.squareup.moshi:moshi:$moshi_version"
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
implementation "net.java.dev.jna:jna:$jna_version"
testImplementation("org.junit.jupiter:junit-jupiter-api:$junit_version")
testRuntime("org.junit.jupiter:junit-jupiter-engine:$junit_version")
}
sourceSets {
main.kotlin.srcDirs += 'src/main/kotlin'
}
task wrapper(type: Wrapper) {
gradleVersion = '4.4.1'
}
jar {
manifest {
attributes "Main-Class": mainClassName
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
The command I use to generate the jar is ./gradlew clean build and the command I use to run the jar is java -jar build/libs/impt-1.0-SNAPSHOT.jar.
I've tried following the official docs and also I've tried with these resources: 1, 2 and 3 without any luck.
Just change implementation configurations to compile. (At least for kotlin-stdlib)
The compile configuration is deprecated and should be replaced by implementation or api in Gradle plugin for Android, however for kotlin-gradle-plugin, I think you still need compile.
Alright I had the same problem and finally figured this out:
My main class name was Main.kt and this is what I had in my build.gradle file when I was getting the same error:
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': "play.Main"
)
}
}
Although I noticed when I create the jar artifact in Intellij, it uses play.MainKt as my Main-Class name (yeah, weird naming convention), then I changed my build.gradle file to this:
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': "play.MainKt"
)
}
}
and now I am not getting that error.
I'm having an issue where gradle fatJar/uberJar is causing the following exception when trying to run the jar:
Error: Could not find or load main class com.domhauton.membrane.Main
The simple jar task without
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
works with no issues (up to the point where it starts needing dependencies).
I presume this is to do with one of my dependencies changing the classpath, but I'm not sure why that would be happening.
Relevant part of build.gradle
project.version = '1.0.0-alpha.2'
project.group = 'com.domhauton.membrane'
jar {
baseName = 'membrane-daemon-simple'
version = project.version
manifest {
attributes 'Implementation-Title': 'Membrane Daemon',
'Implementation-Version': project.version,
'Main-Class': project.group + '.Main'
}
}
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Membrane Daemon',
'Implementation-Version': project.version,
'Main-Class': project.group + '.Main'
}
baseName = 'membrane-daemon'
version = project.version
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Gradle build file is here (with the remainder of code):
https://github.com/domhauton/membraned/blob/master/build.gradle
The META-INF folder is very full of files from the other dependencies so I'm not sure where to start looking for conflicts.
Took another run at the problem using shadowjar and it worked flawlessly.
Relevant code:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath group: 'com.github.jengelman.gradle.plugins', name: 'shadow', version: '1.2.4'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
project.version = '1.0.0-alpha.2'
project.group = 'com.domhauton.membrane'
jar {
baseName = 'membrane-daemon'
version = project.version
manifest {
attributes 'Implementation-Title': 'Membrane Daemon',
'Implementation-Version': project.version,
'Main-Class': project.group + '.Main'
}
}
Can be seen in context here: https://github.com/domhauton/membraned/blob/4d28ea451acc5bf52724a0cc5c94823659236287/build.gradle
I have a project that requires native libraries to run.
I'm using the Gradle-Support Netbeans plugin.
apply plugin: "java"
apply plugin: "application"
apply plugin: "eclipse"
sourceCompatibility = 1.7
mainClassName = "com.myPackage.MainClass"
if (!project.hasProperty('mainClass')) {
ext.mainClass = mainClassName
}
repositories {
mavenCentral()
maven {
url "http://teleal.org/m2"
}
}
dependencies {
compile group: "com.esotericsoftware.kryo", name: "kryo", version: "2.23.0"
compile group: "net.java.jinput", name: "jinput", version: "2.0.5"
compile group: "org.jcraft", name: "jorbis", version: "0.0.17"
compile group: "org.jdom", name: "jdom2", version: "2.0.5"
compile group: "org.lwjgl.lwjgl", name: "lwjgl", version: "2.9.0"
compile group: "org.lwjgl.lwjgl", name: "lwjgl_util", version: "2.9.0"
compile group: "org.teleal.cling", name: "cling-core", version: "1.0.5"
compile group: "org.teleal.cling", name: "cling-support", version: "1.0.5"
compile group: "xpp3", name: "xpp3", version: "1.1.4c"
compile files("./lib/jars/kryonet-2.21.jar")
compile files("./lib/jars/slick.jar")
compile files("./lib/jars/slick-util.jar")
compile files("./lib/jars/TWL.jar")
}
jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
}
manifest {
attributes "Main-Class": project.mainClassName
}
}
run {
configureRun(it)
}
task(debug, dependsOn: 'classes', type: JavaExec) {
configureRun(it)
classpath = sourceSets.main.runtimeClasspath
}
void configureRun (def task){
main = mainClass
task.systemProperty "java.library.path", "./lib/native/"
}
The application will launch fine in run mode but debug mode yields the following error:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':debug'.
> No main class specified
I assume, you are using the application plugin and that is why this configuration for the run task is enough. That is, most likely you should have something like this:
if (!project.hasProperty('mainClass')) {
ext.mainClass = mainClassName
}
run {
configureRun(it)
}
task(debug, dependsOn: 'classes', type: JavaExec) {
configureRun(it)
classpath = sourceSets.main.runtimeClasspath
}
void configureRun(def task) {
main = mainClass
task.systemProperty "java.library.path", "./lib/native/"
}
task(debug, dependsOn: 'classes', type: JavaExec) {
Looks like you're adding an action instead of configuring the the task, all your configuration on the task happens too late, that is at the execution time.
Either that or make sure your main (you must have one somewhere, right? I'm new to this) looks like:
public static void main (String[] args)throws Exception
My research led me to here: (this site may help you with gradle) http://forums.gradle.org/gradle/topics/problem_with_javaexec