JavaFX FatJar using Gradle and IntelliJ - java

I've tried everything by now, so I hope someone in here can tell me more...
Im trying to produce an executable .jar from a IntelliJ Gradle JavaFX project. I used the standard setup that IntelliJ provided, I changed the Gradle.build file however.
The new file I got from here: Non-Modular Gradle (openjfx.io)
I have a main class that has some basic code in it and a launcher class that does not extend Application and is specified as the Main class in the jar manifest.
For now I only use javafx.controls and basically everything is as the example they provided here.
When doing the ./gradlew jar command I get the error:
no module-info.java found
Which - as I understand - is not required if I use the Non-Modular approach?
However if I add it I get the error:
Entry module-info.class is a duplicate but no duplicate handling strategy has been set.
I tried every other option out there, all of them lead to either the 2. error or the jar was produced but not executable due to the fact that it can't find the Application class...
Any help is greatly appreciated.
I just want to point out that I've never really used Gradle before and have never formally learned any coding, but can fiddle my way around usually.
For the sake if it my build file:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.9' // this is old right?
}
repositories {
mavenCentral()
}
dependencies {
/* uncomment for cross-platform jar: */
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:win"
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:linux"
runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:mac"
}
javafx {
version = "16"
modules = [ 'javafx.controls' ]
}
mainClassName = 'main.class.with.Code'
jar {
manifest {
attributes 'Main-Class': 'main.class.with.Launcher'
}
from {
// this is what causes the module duplicate error I think (at least it did in my other tries)
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}

Related

How would you add a library downloaded as a folder to gradle?

I'm new to Gradle and finding the documentation confusing.
I've downloaded the Jaunt webscraping API, however I can't seem to add it to my project. When downloaded, Jaunt is a folder that contains the jauntx.x.x.jar file inside it as a root for the library. In a normal IntelliJ project, I would simply add a reference to that .jar file.
However, I'm using Gradle as this project requires being built into an executable jar
I need to add the Jaunt library to the project but if I do it the normal way my project recognises the library while I'm developing it but as soon as I try to run it, it tells me that the com.jaunt package is not found.
I assume that it needs to be added to the build.gradle file, however I'm not sure how.
Can anyone advise?
Build.gradle as follows:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
repositories {
mavenCentral()
}
dependencies {
implementation files("libs/jaunt1.6.0.jar")
}
javafx {
version = "14"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
jar {
manifest {
attributes 'Main-Class': 'org.openjfx.Main'
attributes 'Class-Path': 'org/openjfx/ '
}
}
application {
mainClassName = 'org.openjfx.Main'
}

Building FatJar for JavaFX with gradle and intellij, getting NoClassDefFOundError

I have set up an OpenJDK 12 project in IntelliJ (2019.2) using the built-in Gradle support. To design the GUI I'm using JavaFX 12. I have followed and read the setup guide several times, I have no issues running the program in my IDE, the issue is when I try to build a .jar file for distribution that I run into problems. I have not been able to find a solution that works so far and I've searched QUITE a lot, nearly tearing my hair out over this. Currently when i try to run my jar file with java -jar "jarName".jar I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at com.CAM.Starter.main(Starter.java:6)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 10 more
I have tried moving my main class to a separate one that doesn't extend Application, which is what gives the above error. Without moving my Main class I get a different error.
My build.gradle looks like this currently:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
group 'ClassicAddonManager'
version '0.2'
sourceCompatibility = 11
repositories {
mavenCentral()
}
javafx {
version = "12.0.2"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'net.sourceforge.htmlunit:htmlunit:2.13'
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
compile group: 'net.lingala.zip4j', name: 'zip4j', version: '1.2.4'
}
jar {
manifest {
attributes 'Main-Class': 'com.CAM.Starter'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
If I have my main method extending Application then I get an error stating that my main class could not be found even though I can see it is present in the generated .jar file.
All I'm trying to do, is to generate a file that a friend with no knowledge of programming could run. Ideally, a file that they could run without having to install Java first. I know it should be possible to do this, but Gradle is new to me so I'm not sure if that is what is causing all this headache. Is there potentially an easier way to deploy? Especially given that this is a solo-project?
EDIT
I have tried the modular part of the guide. Doing that I have over 100 error when attempting to build. They are all something in the vein of:
javafx.graphicsEmpty reads package org.xml.sax from both xml.apis and java.xml
If you want to do a fat jar using Gradle but not a shadow plugin, usually you will do:
jar {
manifest {
attributes 'Main-Class': 'com.CAM.Starter'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
However, there is an important fact: compile is deprecated, and the JavaFX plugin uses implementation by default.
As a consequence, configuration.compile might be empty, or at least, it won't contain the JavaFX classes.
The solution is to check the runtimeClasspath configuration, as we will have all the classes there, and we can make the fat jar:
jar {
manifest {
attributes 'Main-Class': 'com.CAM.Starter'
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
This is explained in the OpenJFX docs, https://openjfx.io/openjfx-docs/#modular, section non-modular projects, subsection gradle.
Once you have your fat jar, you can run it with:
java -jar yourFatJar.jar
Note that double-clicking on it won't work, as explained in detail here, so it can be convenient to create a small batch instead.
A better solution is to do a modular project and use jlink to create a runtime image (it also includes a launcher script). You can distribute this image to other users that don't have even JDK installed. With gradle, you can just include the 'org.beryx.jlink' plugin, like in this sample.
And you can also use the early version of jpackage to create and distribute an installer.

How to pack dependencies for a forge mod while also avoiding class conflicts?

This is currently my build.gradle file (I removed unnecessary task references, which are not only working perfectly fine, but are irrelevant to the current question):
buildscript {
repositories {
jcenter()
maven {
name = 'forge'
url = 'https://files.minecraftforge.net/maven'
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.1-SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: 'java'
dependencies {
compile('some.library:here:1.2.3') // no extra modules
compile('other.library:here:4.5.6') // multiple modules
}
jar {
from project.configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
Currently I have a system which will automatically pack all the dependencies into my jar file. The main issue I have is that if another mod or file loads the same library before my mod, I will end up with a class loading problem, because I'll be loading the same class twice (one from another jar, and one from my own).
With that being said, is it possible to avoid such an issue? How would I realistically account for such a thing? Does forge perhaps have a method to control class loading to the extent that duplicate libraries will not be loaded, hence there will be no class conflicts?
Minecraft Forge supports a feature called shading to deal with this problem. When you shade a jar, it's integrated into your jar, but the package is renamed to something unique (along with all references to it from your code).
Source: https://github.com/MinecraftForge/ForgeGradle/blob/FG_1.2/docs/user-guide/shading.md

Additional gradle configuration breaking compilation when using flatDir repo

I'm using the approach from Gradle - extract file from depended jar to extact a .so file from inside a native JAR.
configurations {
special
}
dependencies {
special('org.jogamp.jogl:jogl-all:2.3.2:natives-linux-i586')
}
task extract(type: Copy) {
from({ zipTree(configurations.special.singleFile) })
include 'natives/linux-i586/*.so'
into "$buildDir/extracted"
}
This works fine, however it appears to break compilation of code that depends on org.jogamp.jogl:jogl-all:2.3.2, the non-native Java part.
TestJogl.java:1: error: package com.jogamp.opengl does not exist
import com.jogamp.opengl.GL;
The compilation fails if the project is built with clean extract build but not clean build
I've simplified the code to
import com.jogamp.opengl.GL;
public class TestJogl {
private GL gl;
}
and corresponding build.gradle
apply plugin: "java"
dependencies {
compile "org.jogamp.jogl:jogl-all:2.3.2"
}
I've isolated this issue to the usage of "flatDir" repo. The exact same project compiles fine when using mavenCentral(). Note using a legacy corporate network without artifactory or direct Internet access.
allprojects {
repositories {
flatDir {
dirs "$rootProject.projectDir/local-repo"
// contains jogl-all-2.3.2-natives-linux-i586.jar
// jogl-all-2.3.2.jar
}
}
}
I've managed to work around the issue by changing the dependency to explicity specify #jar, which should be implicit
compile "org.jogamp.jogl:jogl-all:2.3.2#jar"
The same problem occurs in both single and multi project layouts.
My analysis: This is a bug in Gradle. Somehow when using flatDir Gradle gets confused and thinks that the dependency has been setup, but uses the native JAR instead of the Java JAR.
Questions: Am I doing something wrong? Is this a bug? Is there another way to workaround it?
Environment: Gradle 3.5, JDK 1.8u144

No Main Maifest attribute Gradle

I have read all the other threads about this problem and applied all the solutions I could find. Nothing helped. When I run the gradle.build task I get a .jar file. But when running the file I get no main manifest attribute, in DiscordBotJDA-1.0.jar
Can annyone provide help?
Thanks a lot!
Here is my gradle.buidl file:
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'Timbo' at '7/13/16 2:08 PM' with Gradle 2.9
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/2.9/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'start.StartUp'
version = '1.0'
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'MCI_Bot',
'Implementation-Version': version,
'Main-Class': 'start.StartUp'
manifest.attributes("Main-Class": 'start.startUp')
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'net.dv8tion:JDA:2.1.3_327'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
I am using eclipse in case it matters.
Were you expecting that your "fatJar" task would be doing that? Just defining the task doesn't put it into the task execution tree. You have to either run the task directly, or specify a task dependency relationship including your task. For instance, saying that another task depends on it.
You might be better off just specifying a "jar" configuration block, which will configure the already existing "jar" task, which already has proper dependency relationships defined.
Read the User Guide for examples of configuring the "jar" task.
It might be more convenient to download the Gradle distribution, which provides the PDF of the User Guide, which might be easier to search.

Categories

Resources