B4J Question How to create cross platform distribution ❓

androidsoftcoder

Member
Licensed User
Longtime User
Hi community

I actually tried to search the site and forum but drowned in tons of results. So let me ask. I'm a newbie in B4J. Excuse me if the answer to my question well-known - just give me a proper direct link and I'll be happy.

I installed the latest B4J on my Windows 10 PC. Created a simple application. Now I want to share it as a separate standalone file that may run on Windows and macOS.
I expected that B4J will produce a single JAR file that can be executed on both OSes according to their command syntax. But I found that I was wrong.

So my question: what should I do on my Windows PC to get two self-sufficient files of my app that may be copied to another Windows and macOS computers and simply run there (by an absolute beginner) without any rocket-science - just click-and-run?

Thank a lot in advance for your help.
 
Last edited:

udg

Expert
Licensed User
Longtime User
I expected that B4J will produce a single JAR file
Compiling in Release mode do just that. Look in your project/Object dir and you find the jar you're looking for.
As for running it "as is" on other PCs, provided that those PCs already have a running copy of JRE (should be at least 8.40) you can lauch the copied jar according to rules set by the OSes. What I mean is that on a PC with JRE 8.40+ you can double-click the jar (provided the JAVA path is correct), while on machines sporting JRE 11/14 you should set a batch or launch by manual calling the program.
There are a lot of examples on the Forum. Here I just gave you hints to confirm that a jar is produced and could be run on machines different from the developing one.
 
Upvote 0

androidsoftcoder

Member
Licensed User
Longtime User
Compiling in Release mode do just that. Look in your project/Object dir and you find the jar you're looking for.
... you can double-click the jar ...
when I "double-click the jar" I got nothing. executing "java -jar <myjarfilename>.jar" gave me the following error:
Java error message:
Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.
UnsupportedClassVersionError: ks/tablezator/main has been compiled by a more recent version of the Java Runtime (class file version 53.0),
this version of the Java Runtime only recognizes class file versions up to 52.0
I used these files to install java:
- jdk-11.0.1.zip
- jdk-8u251-windows-x64.exe
and have these ones in java:
- jdk-11.0.1 - using for compile (bcz jdk-14.0.1 can't compile)
- jdk-14.0.1 - default in system
- jre1.8.0_251 - default in system
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
No, it's not fiction. Erel suggests (and encourages) the use of B4JPackager11 because the resulting exe (for Windows) contains a copy of the Java runtime right for your app, (i.e the same version used to test and compile) so the machine running your code won't need to have an already installed copy of JRE (which could be a "wrong" version btw).
Same for older B4JPackager for version 8.
On the other hand, when you install JRE 8.251 on a PC (provided you compiled the app using that same Java version), you can just deploy your *.jar file on that PC and run it (double-clicking if the OS can find java.exe).

As an example, I have JDK 1.8.0. 201 on my dev machine and in Tools/Paths configuration the first box (javac.exe) points to it (C:\Program Files\Java\jdk1.8.0_201\bin\javac.exe). A *.jar compiled with that config could go (and I do it) to a Windows/Linux (Mac not tried) where JRE 1.8.0.201 is installed and ran w/o problems.

Once I'll upgrade to JDK11 I'll go the B4XPackager11 way to deploy even if I don't like the waste of space due to multiple copies of JREs
 
Last edited:
Upvote 0

androidsoftcoder

Member
Licensed User
Longtime User
Ok. Accepted!
And what about creating on Windows files that may be sent to the macOS user and he just runs them?
Development on Windows, target os - macOS...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I will write it more clearly. There are two options:

1. Assume that Oracle Java 8 is properly installed on the client computer.
Use Java 8. Run your app in release mode. Send the small compiled jar to the user. It will run on Linux, Mac or Windows. The user will be able to double click on the jar or run it with java -jar.

2. Don't assume anything.
Use Java 11+.
Windows: Build a package with the integrated packager tool. Use Inno Setup to build a setup file.
Mac / Linux: Use B4J-Bridge with B4JPackager11 to build a package. The package must be built on the target platform.
Distribute the package. On Mac you need to follow additional, quite complicated steps, to create a notarized package if you want it to run without warnings.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
So my question: what should I do on my Windows PC to get two self-sufficient files of my app that may be copied to another Windows and macOS computers and simply run there (by an absolute beginner) without any rocket-science - just click-and-run?
Me personally I use B4JPackaer11 for distributing on Windows, for MacOS I just send my clients the compiles .jar file. If need be I'll TeamViewer directly into their Macs and Install JRE for them. I've not had anyone complain for moaning about it, plus I tell clients that is what I'll do before hand.

I personally found manually packing up B4J projects for full distribution on MacOS a right pain, maybe I'll try it again sometime in the future...
 
Upvote 0

androidsoftcoder

Member
Licensed User
Longtime User
Thanks a lot for your attempts to help me. Unfortunately, my problem is in the different java versions. java 14 (the freshest) can't compile my sources, so I forced to use 11. However, when I'm trying to run obtained jar - the system is using the default 14th and can't jar, compiled under 11.
Is there any proper way, to use java 14 during compilation?
Compilation Error (use v14 for compile):
B4J Version: 8.31
Java Version: 14
Parsing code.    (0.02s)
Building folders structure.    (0.01s)
Compiling code.    (0.01s)
Compiling layouts code.    (0.01s)
Organizing libraries.    (0.00s)
Compiling generated Java code.    Error
error: module not found: javafx.swing
1 error
only showing the first 1 errors, of 6 total; use -Xmaxerrs if you would like to see more

javac 14.0.1
runtime error (run under v14 jar built by v11):
>java -jar Tablezator.jar
Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        ... 5 more
java.lang.UnsupportedClassVersionError: ks/tablezator/main has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        .......
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
If you are unable to compile using Java 14 due to javafx.swing not found, then you may not have used the Java 14 provided on B4J's home page. If you want simplistic running JavaFX apps, then just stick with Java 8. You don't have to use Oracle's version, there are other versions out there (that do include JavaFX) (Amazon's Corretto is one of them).
 
Upvote 0

androidsoftcoder

Member
Licensed User
Longtime User
Thanks. I'll try it tomorrow...
But what about compatibility? Let's assume I'll use Amazon's Corretto and successfully compile under v14.
I'm afraid that my (Amazon's Corretto v14) jar will NOT work on other PC where Oracle java v14 is installed. Am I right?

I wrote a handy tool and just want to share it with my friends. I want to be sure that the same jar will work on any other PC or mac with (almost) any installed java.
But my internal voice is whispering to me that it's impossible (
 
Last edited:
Upvote 0

androidsoftcoder

Member
Licensed User
Longtime User
After downloading and installing "Oracle JDK 8: download link (requires creating an account)", cofiguring path to C:\Program Files\Java\jdk1.8.0_251\bin\javac.exe
One more action may be required if you previously installed some other java runtimes and JDKs: you should verify that your system, by default, using "your" java. This reg-file shows the correct setting for my case.
Open jar by double clicking:
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\jarfile\shell\open\command]
@="\"c:\\Program Files\\Java\\jdk1.8.0_251\\bin\\javaw.exe\" -jar \"%1\" %*"
in my case here was a link to the v14 wich does not worked for generated by the B4J jar files.
 
Upvote 0
Top