How to create a modular JavaFX11 runnable jar/exe? - java

I have successfully followed the instructions mentioned in openjfx-docs (using maven)
I'm able to run it in my eclipse. I'm also able to create custom runtime images using jlink. The application could be run through the generated launcher.
Now, how do we create a runnable jar/exe with the stripped down JRE generated by jlink?

I just recently started experimenting with the jlink tool of Java 11 and can provide a partial answer. The java command in the bin directory of a custom runtime image works just like java of the standard JRE:
/path/to/custom/runtime/image/bin/java -jar myjar.jar
The custom runtime image must of course contain all the required dependencies, which I made sure by making myjar.jar a modularized JAR and by providing the switch
--add-modules myjar.jar
when creating the custom runtime image with jlink. With the additional switch
--launcher mylaunchername=modulename.of.my.jar/mypackage.with.MainClass
the directly executable binary
/path/to/custom/runtime/image/bin/mylaunchername
is generated. On my Mac, I created an alias and moved that to the desktop. That's as close to an application as I could get so far, with the following caveats:
The app icon is the standard green "exec"
double clicking on this icon launches a terminal, which is undesirable in case of a JavaFX application
And, of course, the "application" is not a single file, but the complete custom runtime image folder.

Related

"Graphics Device initialization failed" when running JavaFX application after running jpackage? [duplicate]

I have some really noob question.
I tried to create installation for my test app with jpackage in OpenJDK 14. Here is what I did:
first, created custom JRE with
jlink --module-path "C:\Java\javafx-sdk-14\lib" --add-modules javafx.controls,javafx.fxml --output hello\myjre
and that was successful. I copied arguments from my Eclipse from Run Configurations. After that made installation with jpackage
jpackage --name HelloFX --input hello --main-jar HelloFX.jar --runtime-image hello\myjre
That created .msi file, I run it and it created entry in my Win10 applications. Of course, I have no idea how to find that in windows menu, but it is placed in my C:\Program Files\HelloFX. I located Icon and Application file with Duke image, when I tried to run application meesage "Failed to launch JVM" pop up.
Can someone help me, what am I doing wrong? I really want to make this work and dive deeply in JavaFX.
The JavaFX JARs included with the JavaFX SDK do not include the native code. Instead, said code is in the bin directory. That means your custom runtime image created by jlink does not have the necessary native code to run JavaFX. You have two options:
Download the JMOD files from Gluon and use those when creating the custom runtime image. You would put the JMOD files on the --module-path instead of the regular JAR files.
Note another way to get the JMOD files is to use a JDK distribution that includes JavaFX. Such distributions may no longer be provided by Oracle but other distributors still offer them (e.g. Azul Zulu, BellSoft Liberica, etc.). If you have a JDK that includes JavaFX then you don't need to place JavaFX on the --module-path, just make sure the JavaFX modules are resolved while packaging and during execution (via requires directives or --add-modules arguments).
Use the JavaFX JARs that are published to Maven Central instead of the SDK. The Maven Central JARs embed the native code.
In both cases, make sure to use the JMOD/JAR files for your operating system—JavaFX is platform-specific.
I believe the first option is the best. When using JMOD files with jlink the native code is included with the custom runtime image in the same way as the native code specific to the JRE. If you use the second approach the native code will still be included with the custom runtime image but it will have to be extracted to some location on your computer (e.g. <user-home>/.openjfx) before it can be used1. In other words, the first option is cleaner.
1. Note this extraction is done automatically by JavaFX.

Running java runnable JAR

I have created JavaFX app with OpenJDK13 and OpenJFX13. In Eclipse I have made Runnable JAR but it doesn't work (at least on my Win 10 installation) unless I add VM arguments in command line.
So, to fix this I made *.bat file with text:
java -jar --module-path "C:\Java\javafx-sdk-13.0.1\lib" --add-modules javafx.controls,javafx.fxml Testing.jar
It working that way. But, there is 2 things about my solution I would like to change.
Is there any way to avoid Command prompt being shown when starting app?
Package required libraries into selected JAR is selected. When I remove
--module-path "C:\Java\javafx-sdk-13.0.1\lib"
from arguments in *.bat file it doesn't work. Any way to make it work without this? If I understand correctly end user should have everything he need in runnable JAR?
Creating runnable jars is a very old-style technique. They would only work on the platform on which you have built them anyway due to the platform specific native code of JavaFX. So why not go the full way and create a real application for your platform? This can be done with the jpackage tool which comes with the EA release of JDK 14. Have a look here for a tutorial: https://github.com/dlemmermann/JPackageScriptFX .

How do I make a final executable file for my Java program?

I created a tic-tac-toe game in Java. It runs fine on Eclipse.
How do I compile this file (which is currently a .java file) to the standard file format of Java applications, so it can be run from the desktop like a normal program?
What is the standard file type for the final executable Java application? What should be the file type if I want people to easily and without any computer knowledge run my program on their computers?
with eclipse right click on your project. then export it as a runnable .jar file.
Project Right Click > Export > Runnable .jar File.
First choose your project under "Launch configuration", then choose your destination.
After that click finish. Your program should be in your destination folder. Double click to start (just like an .exe file)
For example: If you export it to your desktop, and you name it "TicTacToe", the file on your desktop is "TicTacToe.jar" - ".jar" is your executable file
Done
You have to compile your java class first
javac TicTac.java
and then execute it
java TicTac
Note: that here you provide the name of the class with the main method!
As the other answers indicated, you can create an executable jar using eclipse (or a number of other tools). What these tools are doing under the hood is defining the Main-Class: attribute in the jar's manifest.
In Windows, your users can double click on an executable .jar to launch it, as long as the file associations are configured correctly. However this may not be obvious to windows users who are trained to expect some sort of .exe extension.
To solve this, you could use launch4j to wrap your executable jar in a windows executable. Note: this doesn't change your java application into a native application (it still requires the JVM, etc), it simply makes it launch more like a native application.
For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start. JWS works on Windows, OS X & *nix.
Note that JWS is more effort for us to deploy (it involves not only Jarring and signing the code, but creating a JNLP launch file and a page on the net or network to check that Java is installed & serve the files to the user), but is super easy for the end user.
If there is a JWS deployment, the Jar does not have to be an 'executable Jar' as described in the other answers.
The command should be javac yourFile.java from you command prompt and then after compilation, class file is created. You can run it using java MailClassName
You can find a good tutorial on using javac and java commands here.

How to run Java programs by clicking on their icon on Windows?

I have written a Java program that uses Java swing library. Now I would like to execute this program by double clicking on the executable file on Windows just like any other program with a GUI. How do I do that?
Since it is Java based and has a GUI, the obvious answer is to deploy it using Java Web Start.
Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, AWT, SWT) desktop applications directly from a network or internet link. It offers 'one click' installation for platforms that support Java.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
By 'desktop integration' read desktop shortcuts and menu items on supported platforms.
The 2 icons on the right (JotPad & Star Zoom Animation) are both Java based apps., installed using Java Web Start. Since JotPad is sand-boxed, the user will be prompted as to whether to create the shortcut. That choice is not offered for apps. with higher permission levels, so it would make more sense to install/remove the shortcuts and menu items using the IntegrationService - which allows an app. (after prompting the user) to create/remove them at run-time.
There are number of options:
Create an executable jar of your project. for this jar to work you have to have javaw as default application to open it.
Create an exe of your project.
Create a bat file which runs your jar file.
Take a look at this: How can I convert my Java program to an .exe file?
While the others mention excellent choices like creating a native executable, there is another useful method: creating a shortcut.
Right click your desktop, expand the "New" option, and click on "Shortcut".
Type "javaw.exe". Click next.
Name it whatever you want. Click done.
You'll notice the newly created shortcut on your desktop. Right click it and choose "Properties"
In the "Target" textfield, append "-jar path-to-your-jar.jar" where you replace "path-to-your-jar.jar" with the actual path to your jar
You can also now optionally change the icon to whatever icon you want
This shortcut can be pinned to the taskbar and be used from anywhere (considering you provided an absolute path to your JAR).
you need to create exe from the java program.
Creating executable jar files
First, make sure you have installed Java 1.2 or above. This facility is not available in previous versions of Java.
Next, create your working java system. In general, you will want to put it into a package. For this example, I created a trivial HelloWorld application that prints out "Hello World" plus the first command line argument, and placed it into the package "psae". Therefore, the HelloWorld files (HelloWorld.class, HelloWorld.java) were located in the directory psae. I tested the system to make sure it worked before going on to the next step.
In the directory in which the psae is located, created a file called "mainClass". This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package specification. Here is the single line:
Main-Class: psae.HelloWorld
Note: make sure you type a carriage return after this line; some windows systems need it and will report a "Failed to load Main-Class manifest attribute" error.
Next, I create a jar file called psae.jar using the "jar" command in Java2. I use the "m" command line argument to specify the manifest file mainClass, which adds information to the jar file on where the main class will be found. Here is the jar command:
bertha:~ > jar cmf mainClass psae.jar psae
Just for fun, and to check what's happened, I print the table of contents for the jar file I just created. Here's the command and its result:
bertha:~ > jar tf psae.jar
META-INF/
META-INF/MANIFEST.MF
psae/
psae/HelloWorld.java
psae/HelloWorld.class
Having successfully created the jar file, I can now invoke java2 on it with the command line argument:
bertha:~ > java -jar psae.jar Philip
Hello World Philip
There are a few projects, like http://jsmooth.sourceforge.net/ and http://launch4j.sourceforge.net/
you can use something like Launch4j.
also look at JSMooth.
Hope it helps
There are two ways. Both involve packaging your code in a .jar.
The first way is to build an actual .exe file using a tool like Launch4j. It will require you to set up things like tell it which class to execute, which icon to use, which JRE is OK, what JRE parameters to use, etc.
The second option is to make the .jar itself executable. You do this by adding a manifest to the .jar. The manifest is a small configuration file that describes the jar. One of the attributes is Main-Class which defines the entry point. In other words, it says which class has the main function that should be called when the user double-clicks the file.
Here's a basic tutorial about manifests: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
The 2nd option is easier to get going, but users will know what to do with a .exe far more often.
Note that if either approach complains that it can't find the class, make sure to set the classpath manifest attribute to match your project.
If you have an executable jar file, just shift-right click on your file and set it to be opened by javaw. The other option (in case you want to pass in parameters to your application) is to create a .bat file where you spin off your application via java or javaw
Right click to your "project" in eclipse and select "export" then choose "Java->Runnable Jar File" select your project name and finish.
Seems you want to deploy and run the standalone application of swings. Being a java developer you should understand the power of jar files. Those are executable in themselves {so no need to create .exe files :)} .
The below code will help you to create a jar file.
Creating a jar File in Command Prompt
Start Command Prompt.
Navigate to the folder that holds your class files:
C:\>cd \mywork
Set path to include JDK’s bin. For example:
C:\mywork> path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Compile your class(es):
C:\mywork> javac *.java
Create a manifest file:
C:\mywork> echo Main-Class: NameOfProject >manifest.txt
Create a jar file:
C:\mywork> jar cvfm NameOfProject.jar manifest.txt *.class
Test your jar:
C:\mywork> DanceStudio.jar
After creating a jar just double click on it and you are done.
You have to create an executable jar file. For that you just simply add a META-INF folder to the jar, then add a MANIFEST.MF text file with two lines:
Manifest-Version: 1.0
Main-Class: your.package.YourMainClass
Here's how to run a Java program by RIGHT-CLICKING on it (in other words, from the Windows Explorer context menu). This handy trick is great for beginners who need to test their simple programs on the fly. Works on both Win7 and XP rigs.
[ATTN: Depending on the situation, you may need to remove the package directive from the top of your Java file.]
Step 1. Create a batch file (e.g., RWJ.bat) inside a folder of your choice (say, in C:\Program Files\Java.)
Step 2. Fill RWJ.bat with the following commands (they will work just fine as is with simple classes but you can, of course, tweak them according to your particular needs by specifying compiler / interpreter switches, passing args, adding echo off, removing pause or whatever):
javac %1
java %~n1
pause
The first command passes the full name of your right-clicked file to the Java compiler; the second one strips the file extension and feeds JVM with the class name only.
Step 3. Add the following key to your Registry: HKEY_CLASSES_ROOT\*\shell\Run With Java and then create its command (default value):
C:\Program Files\Java\RWJ.bat %1.
Step 4. Run your Java class by right-clicking it and selecting Run with Java option.
That's all there is to it.
Another way to run Java programs by pointing and clicking is to use AOT compilers. For example, GCC has an entry point named GCJ, which can be used to compile the source code into both byte codes and standard executable file for your particular OS.
And finally, instead ot batch files one can run WSH, etc.

SWT jar for different platform

I am using JWebBrowser in a swing application. This class belongs to The DJ Project. It needs swt jar to execute. Now I have included swt jar for windows to my jar packaging of the application. I want to know how can I include swt jars for linux/mac in the same packaging? I am using ant to build the application jar. Should I build the jar putting different swt jar for different platform?
if you want to have a single build that runs on different platforms (Win/Mac/Linux/*nix) or architectures (32/64 bit) then you can bundle the SWT jar for each target platform with your installer and then load the correct one dynamically at runtime (or have your installer copy the correct SWT jar at installation time).
E.g. say you want to support 32 and 64 bit Windows and Linux you would have SWT jars:
lib/swt_win_32.jar
lib/swt_win_64.jar
lib/swt_linux_32.jar
lib/swt_linux_32.jar
Make your ant script / installer include all of these (they are about 1.6MB each) and then at runtime in your code you can detect the OS and architecture using the Java system properties
System.getProperty("os.name");
System.getProperty("os.arch");
to build the name of the correct jar file.
Loading the jar at runtime can be performed by a custom classloader or by calling the protected method URLClassloader.addURL(URL url) using reflection.
I've put working code to perform this exact task on my website: http://www.chrisnewland.com/select-correct-swt-jar-for-your-os-and-jvm-at-runtime-191
If you can stand the code-smell then it's a quick solution to a very common SWT problem.
On Mac OS +X, you can incorporate the required JAR and JNI libraries in an application bundle, as shown in this project. See also Deploying SWT Applications on Mac OS X.
On Linux, most platforms make an swt-gtk package available. As a concrete example, here's a startup script for AppleCommander:
java -Djava.library.path=/usr/lib/jni \
-cp /usr/lib/java/swt-gtk-3.5.1.jar:AppleCommander-1.3.5.8.jar \
com.webcodepro.applecommander.ui.AppleCommander -swt
This answer contains the code to select the correct SWT JAR when you start your application: Create cross platform Java SWT Application
All you need to do is put all the JARs in the correct folder and the code will pick them up.

Categories

Resources