Building JAR file with IntelliJ, OpenJFX and Gradle - java

What is in 2021 the best way to create an executable jar file with Gradle in IntelliJ using OpenJFX? Every solution I found is very complex and uses workarounds. This is my current build.gradle file
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
repositories {
mavenCentral()
}
javafx {
version = "15.0.1"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
mainClassName = 'com.company.crawl.main.Main'
group 'com.company'
version '1.0-SNAPSHOT'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '9.2.0.jre15'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
compile group: 'org.apache.commons', name: 'commons-csv', version: '1.8'
}
jar {
from configurations.compile.collect {zipTree it}
manifest.attributes "Main-Class": "com.company.crawl.main.Main"
}
If I run gradle :jar, the jar file includes the dependencies but not the openjfx sdk. What do I have to do to run the jar using the openjfx sdk?
UPDATE 1
changed the jar path to this:
jar {
manifest {
attributes 'Main-Class': 'com.company.crawl.main.Main'
}
from {
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
}
}
after rebuilding the jar, I get the error: "JavaFX runtime components are missing". I don't know why because in the jar file the javafx folders are included. After that I added the VM options:
--module-path /path/to/javafx-sdk/lib --add-modules=javafx.controls,javafx.fxml
and the jar file runs. So what am I doing wrong?

Related

Error "Cannot convert the provided notation to a File or URI" When trying to build JAR with Gradle

I am building a bot in Intellij using Gradle. I've completed the bot and try to execute the jar task in build a .jar to with all the dependancies. However, I keep running into an error.
Cannot convert the provided notation to a File or URI: [ZIP 'C:\Users\name.gradle\caches\modules-2\files-2.1\net.dv8tion\JDA\4.3.0_282\c2e3739477a8e59699355db3bae73b3ab0a1c62e\JDA-4.3.0_282.jar', ZIP 'C:\Users\name.gradle\caches\modules-2\files-2.1\me.duncte123\botCommons\1.0.73\bdadec013dad0b8dc6c582ddc080677978d7adcd\botCommons-1.0.73.jar', ZIP 'C:\Users\name.gradle\caches\modules-2\files-2.1\com.sedmelluq\lavaplayer\1.3.64\69e1889fc5db48dafd1a22f76721d3d95a5b06be\lavaplayer-1.3.64.jar', ZIP 'C:\Users\name.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.2.3\7c4f3c474fb2c041d8028740440937705ebb473a\logback-classic-1.2.3.jar', ZIP 'C:\Users\name.gradle\caches\modules-2\files-2.1\io.github.cdimascio\java-dotenv\5.1.1\6c5712d04751e707b9f925aab756302e74103f59\java-dotenv-5.1.1.jar', ZIP 'C:\Users\name.gradle\caches\modules-2\files-2.1\mysql\mysql-connector-java\5.1.6\380ef5226de2c85ff3b38cbfefeea881c5fce09d\mysql-connector-java-5.1.6.jar', ZIP 'C:\Users\name.gradle\caches\modules-2\files-2.1\net.sf.trove4j\trove4j\3.0.3\42ccaf4761f0dfdfa805c9e340d99a755907e2dd\trove4j-3.0.3.jar', ZIP 'C:\Users\name.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.10.1\18eee15ffc662d27538d5b6ee84e4c92c0a9d03e\jackson-databind-2.10.1.jar', ...].
The following types/formats are supported:
- A String or CharSequence path, for example 'src/main/java' or '/usr/include'.
- A String or CharSequence URI, for example 'file:/usr/include'.
- A File instance.
- A Path instance.
- A Directory instance.
- A RegularFile instance.
- A URI or URL instance.
- A TextResource instance.
(The list of ZIPs was cut short due to there being way too many to list out.)
Below is what my build.gradle file looks like when I try to run.
plugins {
id 'java'
id 'application'
}
group 'com.darkascendants'
version '1.0-SNAPSHOT'
application {
mainClassName = 'com.darkascendants.DABot'
}
repositories {
mavenCentral()
jcenter()
maven {
name 'm2-dv8tion'
url 'https://m2.dv8tion.net/releases'
}
}
apply plugin: 'java'
dependencies {
implementation group: 'net.dv8tion', name: 'JDA', version: '4.3.0_282'
implementation group: 'me.duncte123', name: 'botCommons', version: '1.0.73'
implementation group: 'com.jagrosh', name: 'jda-utilities', version: '3.0.1'
implementation group: 'com.sedmelluq', name: 'lavaplayer', version: '1.3.64'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
implementation group: 'io.github.cdimascio', name: 'java-dotenv', version: '5.1.1'
implementation group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
implementation fileTree('libs') { include '*.jar' }
}
compileJava.options.encoding = 'UTF-8'
jar {
manifest {
attributes(
'Main-Class': 'com.darkascendants.DABot'
)
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
}
}
Why am I running into this issue?

"Cannot find or load main class net.epsilon.app.Main" Java [Gradle]

when I launch my .jar with this command "java -jar epsilon-1.0.jar"
He says "Cannot find or load main class net.epsilon.app.Main"
plugins {
id 'java'
}
group 'net.epsilon'
version '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
task fatJar(type: Jar) {
manifest {
attributes["Main-Class"] = "net.epsilon.app.Main"
}
destinationDir = file("C:/Users/EliXorZz/Desktop/EpsilonAPP")
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.13'
compile group: 'com.github.docker-java', name: 'docker-java', version: '3.2.0'
compile group: 'redis.clients', name: 'jedis', version: '3.3.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
compile 'me.tongfei:progressbar:0.8.1'
}
Help me please.
Thank you.
Sorry, I'm not english.
No it can't work this way.
First, you are copying all the jars to a folder called C:/Users/EliXorZz/Desktop/EpsilonAPP, but when you run your jar, you never tell java to place those jars you copied into the classpath.
So actually the command to run your jar is:
java -jar epsilon-1.0.jar -classpath 'C:/Users/EliXorZz/Desktop/EpsilonAPP/*'

Gradle Spring Boot plugin broken on Linux

I have a multimodule gradle build with a single module containing a spring boot application that consumes the artifacts of the other modules. I have the spring boot plugin applied to the single spring boot module.
Everything works correctly in Windows, but once I try the build on a Linux machine if fails. I have tried multiple linux machines (Jenkins server, Ubuntu VM, even bitbucket pipeline) and it fails with this error:
Problems reading data from Binary store in /tmp/gradle2075404181876889604.bin (exist: false)
I am not entirely sure what gradle uses this binary store for, but after debugging I found that this file name is different than binary store that every other module is using while building.
I have tried multiple gradle versions(2, 3, and 4) and several spring boot versions and they all fail with the same error. The only thing that fixes this is removing the spring boot plugin.
Here is the parent build.gradle
repositories {
mavenCentral()
}
def javaLangVersion = '1.8'
def gradleDir = "${rootProject.rootDir}/gradle"
def realProjects = allprojects - [project(':external'), project(':delivery')]
def javaProjects = realProjects - rootProject
def integrationTestProjects = javaProjects
configure(realProjects) {
group 'com.packagename'
version '1.0-SNAPSHOT'
apply plugin: 'eclipse'
apply plugin: 'idea'
}
configure(javaProjects) {
apply plugin: 'java'
targetCompatibility = javaLangVersion
sourceCompatibility = javaLangVersion
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jVersion}"
compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: "${slf4jVersion}"
testCompile group: 'junit', name: 'junit', version: "${junitVersion}"
testCompile group: 'org.mockito', name: 'mockito-core', version: "${mockitoVersion}"
testCompile group: 'nl.jqno.equalsverifier', name: 'equalsverifier', version: "${equalsverifierVersion}"
}
}
configure(integrationTestProjects) {
apply from: "${gradleDir}/integrationTest.gradle"
}
task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}
Here is the spring boot module build.gradle file:
buildscript {
ext {
springBootVersion = '1.5.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootStarterVersion}")
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
war {
baseName = "${project.name}"
version = "${project.version}"
}
dependencies {
compile project(':module1')
compile project(':module2')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: "${httpCoreVersion}"
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: "${httpClientVersion}"
compile group: 'com.h2database', name: 'h2', version: "${h2Version}"
testCompile project(':test-utils')
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}
All other modules are plain java modules and only declare a dependencies block in the build file.
I have been working on this for 2 days now so any help or suggestions would be greatly appreciated. Thanks!
Well this is super embarrasing... I had a unit test using FileUtils.getTempDirectory() and then I was cleaning that directory using FileUtils.deleteQuietly after the test and it was deleting the gradle binary store that was in the /tmp folder. The reason it wasnt showing on windows is because windows locks files.
did you verify that your gradle.properties is OK on the Linux machine ?

Kotlin Capsule Gradle Error

I'm getting an error when trying to create a capsule using Gradle.
Failed to find Premain-Class manifest attribute in
E:\Dropbox\Projects\Kotlin\Games\CSGO\Charlatano\build\libs\capsule.jar
Error occurred during initialization of VM
agent library failed to init: instrument
CAPSULE: Client connection failed.
CAPSULE EXCEPTION: Accept timed out while processing null null: null (for stack trace, run with -Dcapsule.log=verbose)
Press any key to continue . . .
Here is my build.script
buildscript {
ext.kotlin_version = '1.1-M01'
ext.jna_version = '4.3.0-SNAPSHOT'
ext.quasar_version = '0.7.6'
ext.gdxVersion = '1.9.4'
repositories {
mavenCentral()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap-1.1' }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'
group 'com.charlatano'
version '0.4.3'
mainClassName = 'com.charlatano.Charlatano'
sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap-1.1' }
}
configurations {
quasar
capsule
}
dependencies {
compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: kotlin_version
compile group: 'net.java.dev.jna', name: 'jna', version: jna_version
compile group: 'net.java.dev.jna', name: 'jna-platform', version: jna_version
compile group: 'org.jire.arrowhead', name: 'arrowhead', version: '1.2.1'
capsule group: 'co.paralleluniverse', name: 'capsule', version: '1.0.3'
quasar group: 'co.paralleluniverse', name: 'quasar-core', version: quasar_version
compile group: 'co.paralleluniverse', name: 'quasar-core', version: quasar_version
compile group: 'co.paralleluniverse', name: 'quasar-actors', version: quasar_version
compile group: 'co.paralleluniverse', name: 'quasar-kotlin', version: quasar_version
compile "com.badlogicgames.gdx:gdx:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
}
task capsule(type: Jar, dependsOn: jar) {
archiveName = "capsule.jar"
from jar // embed our application jar
from { configurations.runtime } // embed dependencies
from(configurations.capsule.collect { zipTree(it) }) { include 'Capsule.class' } // we just need the single Capsule class
manifest {
attributes(
'Main-Class' : 'Capsule',
'Application-Class' : mainClassName,
'Extract-Capsule' : 'false', // no need to extract the capsule
'Min-Java-Version' : '1.8.0',
'JVM-Args' : run.jvmArgs.join(' '),
'System-Properties' : run.systemProperties.collect { k,v -> "$k=$v" }.join(' '),
'Java-Agents' : configurations.quasar.iterator().next().getName()
)
}
}
run {
jvmArgs "-javaagent:${configurations.quasar.iterator().next()}"
}
Here are the jar files it generates.
https://dl.dropboxusercontent.com/u/91292881/ShareX/2016/09/libs.zip
Please tell me what is wrong, thanks!
It seems that MANIFEST.md in capsule is broken and it misses a Premain-Class attribute like below:
Premain-Class: org.eclipse.package.ObjectSizeFetcher
Your's capsule manifest file should look like:
Manifest-Version: 1.0
Created-By: 1.5.0_18 (Sun Microsystems Inc.)
Premain-Class: org.eclipse.package.ObjectSizeFetcher
Check similar issue to find more: "Failed to load Premain-Class manifest attribute" while trying to get the size of an object using java agent

Define compile dependencies in gradle that dont get packaged

I'm trying to configure gradle to use lombok to compile my project but I don't want the classes appearing in my jar. On the other side I need the mysql-connector dependency packages in the jar, but it's not needed for compiling. This is my build.gradle file:
group 'de.albritter'
version '1.0-SNAPSHOT'
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'de.albritter.main.Main'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
classpath group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
}
}
I've seen some solutions using compileOnly but if i try to use it I just gete an error that this method is not known.
My gradle version is 2.9
How do I tell gradle that I don't need lombok in my jar?
What you're asking for is variously known as compileOnly or in the maven world, provided dependency. The compileOnly configuration was introduced in gradle in version 2.12. I would strongly recommend moving to the latest version of gradle (2.14 at the time of writing this).
If you need to stick to the older version, there are some workarounds you can find by looking for "gradle provided dependency". One way to do this is to declare your own configuration, lets call it provided and adding its dependencies to compile time classpath. So in your build.gradle:
configurations{
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
dependencies {
...
provided 'group:module:version'
...
}
Or alternatively you can use the prodeps plugin which does most of this work for you.

Categories

Resources