r/javahelp • u/Starglema • Oct 24 '20
Creating a .jar file of a java program w/ JavaFX
new to programming, more or less, this is my first time trying to compile a program... let alone one that uses javafx... and I'm not sure what I need to do. I'm trying to create a runnable .jar file that I can then distribute to others and they can use to run the program. I'm having trouble finding out what I need to do to make this work simply by googling it, it seems like there's different answers and I'm not really sure what I need to be doing to make this work. If someone could walk me through it that would be appreciated.
I can post a link to the .java file if you want it, and also I'm using Intellij for whatever that's worth. If you need more information let me know, I'm not sure what all info I'll need to provide.
The program runs perfectly fine if I build & run it through Intellij so there's that.
Please advise.
2
u/wildjokers Oct 24 '20 edited Oct 25 '20
What version of java are you using?
If you are using java 8, 9, or 10 you can use javapackager. If you are using java 14+ you can use javapackager's replacement named jpackage (still incubating in 14 and 15 but will be final in jdk 16).
javapackager (java 8, 9, and 10):https://docs.oracle.com/javase/9/tools/javapackager.htm
jpackage (java 14+): https://docs.oracle.com/en/java/javase/14/jpackage/packaging-overview.html#GUID-C1027043-587D-418D-8188-EF8F44A4C06A
You can also take a look at the BadAss JLink Gradle Plugin:
https://badass-jlink-plugin.beryx.org/releases/latest/
It uses jlink (which javapackager and jpackage also use) and it can also just use jpackage as well.
The packaging tools are in a state of flux right now because javapackager was removed from the JDK in java 11 with JavaFX because javapackager was in the JavaFX project even though it worked fine for non JavaFX apps. It has taken a bit for the replacement jpackage to be created and make its way back to the JDK.
If all else fails you can just create a fatjar with gradle and just add a Manifest file with a Main-Class specified which will make the jar executable. So you can do it without javapackager/jpackage, but those tools do bundle a runtime whereas creating a fatjar with a manfiest file does not.
Here is a fatjar example for Gradle:
apply plugin 'java'
task fatjar(type: Jar) {
manifest {
attributes 'Main-Class': 'com.example.YourClassWithMain'
}
baseName = 'yourAppName'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}That will create a jar that can be double-clicked on and execute but will not bundle a runtime, your users will need to have a runtime already installed.
javapackager/jpackage will also bundle a runtime (which is the advantage those tools have over a fatjar with a manifest file).
2
u/Starglema Oct 25 '20
I gave jpackage a shot. Using jpackage worked perfectly and created, installed etc the app like I expected it to. Hooray! Only problem is the app didn't actually function (double-clicking it doesn't bring anything up)
I suspect the issue must be I'm not creating the JAR properly, that's what this whole post was about in the first place. How do I create myself a JAR properly with this app that uses javafx libraries.
(alternatively can I send my raw .java file and have someone turn it to a jar for me just so i can see what needs to be done to make it work)
3
u/Starglema Oct 25 '20
Neeeever mind I got it, thank you!!!!!
2
u/wildjokers Oct 25 '20
Out of curiosity what was your issue with your jar file?
Glad my info was able to help you out!
2
u/Starglema Oct 25 '20
I'll be honest I'm not entirely sure. I must have built it wrong because the jar file simply wouldn't launch when I tried to run it and I was finding 10 million different suggestions for how to make a jar file. What finally worked was this: https://taylorial.com/cs1021/Jar.htm#jarwithjavafxpackagesincluded
3
u/Ajx33 Oct 24 '20
I was in the same boat as you a few weeks ago I don’t know how to make one with ant and had to learn how to use maven. It’s a lot easier than it seems. You can learn how to create a maven fx project here. And once you’ve done that you can learn how to deploy it as a jar here. Just scroll to runtime images and go to non modular application.
2
2
u/le_bravery Extreme Brewer Oct 24 '20
Just wanted to chime in here: in terms of ease of use I find that Gradle is pretty much always better than Maven. It’s down to preference, but I’d recommend looking it up and comparing.
1
u/Starglema Oct 24 '20
I'm following the guide for Gradle and I can get through as far as building and running the project on Gradle. But I'm still having trouble getting a jar to get created. If I run the jar task it creates a 20 mg file which doesn't launch. I also see that I'm supposed to use something different than the Main extends Application and typical launcher class, however, I'm not sure what I need to be using instead. Trying to run java -jar %filepath%/program.jar always tells me it can't find the main class. What am I doing wrong here?
1
u/le_bravery Extreme Brewer Oct 24 '20
You may have to specify the mainClass somewhere in the build.gradle
1
u/Starglema Oct 24 '20
I thought I did that. Here I'll attempt to post my build.gradle file:
plugins { id 'application' id 'org.openjfx.javafxplugin' version '0.0.8' } repositories { mavenCentral() } dependencies { } javafx { version = "14" modules = [ 'javafx.controls', 'javafx.fxml'] } mainClassName = 'org.openjfx.spawnfx.MainApp' jar { manifest { attributes 'Main-Class': 'spawnfx.MainApp' } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it)} } }Within my code I still have the main app as public class MainApp extends Application [...etc] and it launched via the main method, public static void main(String[] args) { launch(args); }
If this needs to change, which I'm sure it does, I'm... not sure what it needs changed to...
1
u/Nexius74 Oct 24 '20
I will just add my thing here for maven. In most project you will mainly find that maven is used then gradle. It's true that gradle is easier to use but i feel like maven is still great too learn as many use it.
It is just my opinion and is not an indicator to which is the best. It will come down to what you prefer.
1
u/le_bravery Extreme Brewer Oct 24 '20
Yeah, knowing maven will help in the long run. I’m just a gradle fan and prefer it 👍
3
u/Nexius74 Oct 24 '20
If you use maven use the shade plugin. It will pack all dependencies plus your project in a single jar. You can config the shade plugin to make executable jar.
2
u/dionthorn this.isAPro=false; this.helping=true; Oct 24 '20
For my JavaFX game project: https://github.com/dionthorn/2DTacticalRPG
I used to use .jar deployments but I've recently switched over to JLink images:
https://docs.oracle.com/javase/9/tools/jlink.htm#JSWOR-GUID-CECAC52B-CFEE-46CB-8166-F17A8E9280E9
You can see how the deployment works here: https://github.com/dionthorn/2DTacticalRPG/releases/tag/v0.2.3a-ModMaps
Instead of using a .jar file the JLink image packages a custom JRE with only the needed parts of the JRE + JavaFX libraries to run it. Therefore if an end-user doesn't have java OR javaFX it will run regardless.
2
•
u/AutoModerator Oct 24 '20
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Code blocks look like this:
You do not need to repost. Just use the edit function of reddit to make sure your post complies with the above
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.