How to ensure that JavaFX is installed (espacially on Ubuntu)? - java

I want to write my first JavaFX 8 application (doing my first experiments with e(fx)clipse, etc.) and have a few concerns about platform independence.
JavaFX 8 is a part of the Oracle JRE, which is the most used JRE on Windows. Therefore, I do not really bother here, since JavaFX will be installed on nearly every Windows machine running Java 8.
But on Ubuntu and other *nix, OpenJDK is often used as Java distribution. Since openjfx is separated from open-jdk-8-*, many will not have JavaFX installed by default.
I thought of:
Writing an install script that calls sudo apt-get install openjfx (which I would like to avoid, since I would like the executable .jar to be runable on its own, without script)
Shipping a JavaFX distribution (i.e., its .jars) with my applications (but I read that this should not be done)
Both ideas do not seem very promising...
Since I hope that there are some JavaFX pros out there, i hope that someone can help me with "What are the best possibilities to ensure that my JavaFX application will run fine, both on Windows and Ubuntu?" What are state-of-the-art techniques on how to tackle this problem?

This page (http://www.sj-vs.net/creating-a-simple-debian-deb-package-based-on-a-directory-structure/) describes very well how to build a simple .deb package based on any directory structure (e.g., with precompiled stuff).
You can than add your dependencies, e.g., to javafx packages, to the Depends section of the control file. (https://www.debian.org/doc/debian-policy/ch-relationships.html). This forces any installer like apt or dpkg to install javafx together with your package and makes them fail if this package is not available. You can also add a postinst script that will be executed after the installation in which you can set classpaths etc if necessary.

Related

Can I create software for Microsoft Windows in visual studio using Java [duplicate]

If I have a Java source file (*.java) or a class file (*.class), how can I convert it to a .exe file?
I also need an installer for my program.
javapackager
The Java Packager tool compiles, packages, and prepares Java and JavaFX applications for distribution. The javapackager command is the command-line version.
– Oracle's documentation
The javapackager utility ships with the JDK. It can generate .exe files with the -native exe flag, among many other things.
WinRun4J
WinRun4j is a java launcher for windows. It is an alternative to javaw.exe and provides the following benefits:
Uses an INI file for specifying classpath, main class, vm args, program args.
Custom executable name that appears in task manager.
Additional JVM args for more flexible memory use.
Built-in icon replacer for custom icon.
[more bullet points follow]
– WinRun4J's webpage
WinRun4J is an open source utility. It has many features.
packr
Packages your JAR, assets and a JVM for distribution on Windows, Linux and Mac OS X, adding a native executable file to make it appear like a native app. Packr is most suitable for GUI applications.
– packr README
packr is another open source tool.
JSmooth
JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself.
– JSmooth's website
JSmooth is open source and has features, but it is very old. The last release was in 2007.
JexePack
JexePack is a command line tool (great for automated scripting) that allows you to package your Java application (class files), optionally along with its resources (like GIF/JPG/TXT/etc), into a single compressed 32-bit Windows EXE, which runs using Sun's Java Runtime Environment. Both console and windowed applications are supported.
– JexePack's website
JexePack is trialware. Payment is required for production use, and exe files created with this tool will display "reminders" without payment. Also, the last release was in 2013.
InstallAnywhere
InstallAnywhere makes it easy for developers to create professional installation software for any platform. With InstallAnywhere, you’ll adapt to industry changes quickly, get to market faster and deliver an engaging customer experience. And know the vulnerability of your project’s OSS components before you ship.
– InstallAnywhere's website
InstallAnywhere is a commercial/enterprise package that generates installers for Java-based programs. It's probably capable of creating .exe files.
Executable JAR files
As an alternative to .exe files, you can create a JAR file that automatically runs when double-clicked, by adding an entry point to the JAR manifest.
For more information
An excellent source of information on this topic is Excelsior's article "Convert Java to EXE – Why, When, When Not and How".
See also the companion article "Best JAR to EXE Conversion Tools, Free and Commercial".
Launch4j
Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executables. The executable can be configured to search for a certain JRE version or use a bundled one, and it's possible to set runtime options, like the initial/max heap size. The wrapper also provides better user experience through an application icon, a native pre-JRE splash screen, a custom process name, and a Java download page in case the appropriate JRE cannot be found.
– Launch4j's website
UPDATE: GCJ is dead. It was officially removed from the GCC project in 2016. Even before that, it was practically abandoned for seven years, and in any case it was never sufficiently complete to serve as a viable alternative Java implementation.
Go find another Java AOT compiler.
GCJ: The GNU Compiler for Java can compile Java source code into native machine code, including Windows executables.
Although not everything in Java is supported under GCJ, especially the GUI components (see
What Java API's are supported? How complete is the support? question from the FAQ). I haven't used GCJ much, but from the limited testing I've done with console applications, it seems fine.
One downside of using GCJ to create an standalone executable is that the size of the resulting EXE can be quite large. One time I compiled a trivial console application in GCJ and the result was an executable about 1 MB. (There may be ways around this that I am not aware of. Another option would be executable compression programs.)
In terms of open-source installers, the Nullsoft Scriptable Install System is a scriptable installer. If you're curious, there are user contributed examples on how to detect the presence of a JRE and install it automatically if the required JRE is not installed. (Just to let you know, I haven't used NSIS before.)
For more information on using NSIS for installing Java applications, please take a look at my response for the question "What's the best way to distribute Java applications?"
You could make a batch file with the following code:
start javaw -jar JarFile.jar
and convert the .bat to an .exe using any .bat to .exe converter.
We're using Install4J to build installers for windows or unix environments.
It's easily customizable up to the point where you want to write scripts for special actions that cannot be done with standard dialogues. But even though we're setting up windows services with it, we're only using standard components.
installer + launcher
windows or unix
scriptable in Java
ant task
lots of customizable standard panels and actions
optionally includes or downloads a JRE
can also launch windows services
multiple languages
I think Launch4J is from the same company (just the launcher - no installer).
PS: sadly i'm not getting paid for this endorsement. I just like that tool.
The latest Java Web Start has been enhanced to allow good offline operation as well as allowing "local installation". It is worth looking into.
EDIT 2018: Java Web Start is no longer bundled with the newest JDK's. Oracle is pushing towards a "deploy your app locally with an enclosed JRE" model instead.
IMHO JSmooth seems to do a pretty good job.
If you need to convert your entire application to native code, i.e. an EXE plus DLLs, there is ExcelsiorJET. I found it works well and provided an alternative to bundling a JRE.
EDIT: This was posted in 2010 - the product is no longer available.
I would say launch4j is the best tool for converting a java source code(.java) to .exe file
You can even bundle a jre with it for distribution and the exe can even be iconified.
Although the size of application increases, it makes sure that the application will work perfectly even if the user does not have a jre installed. It also makes sure that you are able to provide the specific jre required for your app without the user having to install it separately.
But unfortunately, java loses its importance. Its multi platform support is totally ignored and the final app is only supported for windows. But that is not a big deal, if you are catering only to windows users.
As of JDK14, jpackage replaces javapackager mentioned in #Jay answer. The Windows version requires Wix 3.0 and it is fairly straightforward to take a java application and build an installer which provides EXE launcher.
It can also be used with jlink to build a cut-down Java runtime image which is bundled with the installer and only contains the set of modules needed to support your application. The jlink step will also be run implicitly by jpackage if no runtime is specified, but I prefer to make the JRE image separately as it will only change when you update JDK or add new module dependencies to your project.
Example main for Java class:
package exe;
public class Main {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("args["+i+"]="+args[i]);
}
}
}
Here are example steps to build on Windows - obviously you'd set up your local build environment (Maven / ant / etc) to re-produce this:
mkdir jpackage.input\jars tmp
javac -d tmp src\exe\Main.java
pushd tmp && jar cvf ..\jpackage.input\jars\myapp.jar . && popd
Check it runs:
java -cp jpackage.input\jars\myapp.jar exe.Main X Y Z
Create a runtime image with jlink for the set of modules use by your application:
set jlink.modules=java.base
jlink --add-modules %jlink.modules% --strip-debug --no-man-pages --no-header-files --compress=1 --output jpackage.jre
In case there are missing modules above, you should check the jlink JRE runtime image can run your app:
jpackage.jre\bin\java -cp jpackage.input\jars\myapp.jar exe.Main X Y Z
Use jpackage to generate installer, with app version based on date+hour (this saves on need to un-install every re-install) and to print out all system properties - remove the parameter "-XshowSettings:properties" after testing:
set appver=%date:~6,2%.%date:~3,2%.%date:~0,2%%time:~0,2%
jpackage --win-console --input jpackage.input --runtime-image jpackage.jre --app-version %appver% --type exe --name "MyApp" --dest jpackage.dest --java-options "-XshowSettings:properties" --main-jar jars\myapp.jar --main-class exe.Main
Run the installer:
jpackage.dest\MyApp-%appver%.exe
Test the application:
"C:\Program Files\MyApp\MyApp.exe" ONE 2 THREE
... Prints system properties ...
args[0]=ONE
args[1]=2
args[2]=THREE
You can use Janel. This last works as an application launcher or service launcher (available from 4.x).
Alternatively, you can use some java-to-c translator (e.g., JCGO) and compile the generated C files to a native binary (.exe) file for the target platform.
I can be forgiven for being against converting a java program to a .exe Application and I have My reasons. the Major one being that a java program can be compiled to a jar file from A lot of IDE's. When the program is in .jar format, it can run in Multiple Platforms as opposed to .exe which would run Only in very limited Environment. I am for the Idea that Java Programs shoudl not be converted to Exe unless it is very neccesary. One can always write .bat files that runs the Java program while it is a jar file.
if it is really neccesary to convert it to exe, Jar2Exe converter silently does that and one can also attach Libraries that are compiled together with the Main Application.
You can convert jar to exe using jar2exe. However you need to purchase the software. If you need a open source software i would suggest JSmooth.
Cautionary note: Much has changed with packaging and deployment since this question was first asked. Most of the answers given are not even current with JIGSAW (Java 9+).
If the goal is to create OS specific packages, information is provided in Oracle Docs Java 17 Packaging Tool User Guide. This guide includes documentation for the jpackage tool, which allows one to create platform-specific packages for Linux, macOS and Windows. I assume the Windows-specific instructions should include arriving at an .exe file, since that remains the most familiar way for Windows users to install and run applications.
My own personal experience creating an exe (for sale on itch.io) was with the Java Platform, Standard Edition Deployment Guide, which included making use of the tool Inno Setup 5. This documentation is older, is for Java 9. The section directly pertaining to .exe packaging is located here As a first step, I used jlink to make a self-contained package. At the time I was first wrangling with this, I was unable to figure out how to get jpackage to work with my modular program. But now that Jigsaw has been around for several years, jpackage is now likely much easier to use, and would be my first choice for the next Java app I might publish for Windows users.
Java projects are exported as Jar executables. When you wanna do a .exe file of a java project, what you can do is 'convert' the JAR to EXE (i remark that i putted between quotes convert because isn't exactly this).
From intelij you gonna be able to generate only the jar
Try following the next example : https://www.genuinecoder.com/convert-java-jar-to-exe/

What packaging tool should I use for a Mac/Windows Java app?

I have a Java desktop app that runs on both the MacOS and Windows.
I understand that I cannot have one distribution for each, which is not a requirement.
I need to know what tool or tools is best to use when delivering a Java app for each.
The tool should install prerequisites (in this case, Java and some JARs) and look native to the respective operating system.
As for OS X's java situation:
Currently, JDK 6 is bundled in the OS.
Presumably, the next version of the OS will still include JDK 6.
It's publicly stated that the OS will have a well-defined place to install multiple copies of Java runtimes, a public interface choosing which of the Java version, etc. See here.
Apple started contributing back its own code to the open JDK community, so JDK 7 should be available as a separate download, see here. 
So, you're not expected to include Java runtime itself into your Java app even then. You're not supposed to install Java in a ramdom place on a filesystem, for example.
As for how you should deploy java apps on OS X:
Double-clicking jar just works.
However, that won't be pretty, because you would only have a generic Java icon in the Dock. You don't want that.
You should use Jar Bundler to make it an honest OS X app. On Mac, it comes with XCode. See the documentation here. You can do that on a non-Mac machine too, using this open-source project.
On Windows, I would recommend either JSmooth or WinRun4J.
On a Mac, the situation is a bit more complex (as the comments point out), but just distributing an executable JAR is probably good enough for now.

How to do a professional setup for java app to avoid "javaw" cannot be found?

I just installed http://yacy.net/
It advertises as easy install except that when I run it says cannot find javaw which spoils nearly everything.
If I were to distribute a java app myself I'd like not to have user knows anything about downloading and installing java, so any article, tool which explains how to package a java app professionally like one can do with a windows app easily ?
There are several approaches to this, all of which have their own set of drawbacks. Here are some of the most popular:
Bundle a private JRE with your application. See the README file that comes with your JRE for the details.
Use a launcher (such as launch4j) that can download a JRE at runtime if a suitable one is not found on the target machine.
Compile Java to native, then distribute as a regular Win32/Linux executable. You can do this with Excelsior JET.
Since you are concerned with deployment, you'll probably also want to create an installer for your application. There are dozens of options for this, such as NSIS.

What is the best way of packaging Java software for running on Windows?

What's the best way to package Java software for running on Windows? Is there a standard for writing .BAT files which can discover the latest installed JRE on the machine? Are there any Maven plugins for this? What's the deal with executable Jar files?
I would without any doubt recommand you to use Java Web Start, as it allows you easy control over version used (and by far the easier to use update mechanism available nowadays).
Depends on what you need.
We've found that using the One-JAR + JSmooth gives a very good user experience as it allows us to distribute a single EXE-file which can be put anywhere on the users system, as opposed to having installation binaries, which need to be installed and uninstalled etc. etc. etc. JSMooth checks for the existance of the appropriate Java runtime, and redirects to the official download site if none is found.
The reason for One-jar is that you generally need to use library jars which is best to have separately but JSmooth only allows for a single jar file.
Also the One-Jar SDK and JSmooth is scriptable without being tied to Windows, so we can build new versions on our Hudson engine running on Linux.
In the past I've used a java installer package like launchForJ. However, a lot of these packages use Java so you may need to a batch or simple executable written in native windows code that does a quick check to see if Java is already installed then run the installer.
Since Java uses system variables during install you should be able to check if the version (or higher) that you require exists in this way.
The standard way would be to use Java Web Start.
Using Java Web Start technology, standalone Java software applications can be deployed with a single click over the network. Java Web Start ensures the most current version of the application will be deployed, as well as the correct version of the Java Runtime Environment (JRE)
Maven has a Webstart Maven Plugin to help building application bundles that can be deployed via Web Start.
An alternative solution would be to use a cross-platform installer generator to generate, well, an installer. I personally like IzPack (I think that JSmooth is very similar but I have more experience with IzPack) and, if you are specifically targeting the Windows platform, you could use IzPack to Build [a] Native Windows Installers with IzPack Native Launcher:
A problem still arise with IzPack when
the target system does not have a JRE
installed. The user needs to first
install it and then run the IzPack
installer. This is not a problem with
Linux or BSD users since they are
generally skilled and they know how to
do that by hand or with a packaging
system (RPM, DEB, ebuilds, ports,
...). Similarly, a Mac OS X user will
not have much problems since it is
available with the operating system.
However the situation is more
complicated on the Windows platform as
a JRE must be installed. Worse,
several instances can be installed in
a sometimes messy situation.
The IzPack Native Launcher tries to
solve this problem. It is a C++ native
application that is available under
the very permissive MIT License and
that uses wxWidgets for the GUI. It is
thus cross-platform (for the
historians: it was first developed on
a FreeBSD box), but most people will
need it on Windows. It uses a simple
configuration file and will first
check for a JRE. If none can be found,
it will pop-up a dialog to let the
user choose between the following
options:
manually specify a JRE location
download one from the Internet
install one that is provided by the packager (if available).
IzPack also offers a Maven Plugin. Worth the check.

JAR installer that auto-detects if Java is there and autostarts the application

I need to build an installer that does the following:
Installs my jar on the client.
Auto-detects if JRE is installed (in which case it does not re-install it), otherwise installs it without the user clicking on another button.
Auto-starts the application when the client is booted.
Works across Windows, Mac and Linux.
Any idea how to go about it?
If a commercial tool is ok, I can recommend install4j. 1, 2 and 4 are easy to do with it, but about 3 I'm not sure. Edit: You could probably achieve that by installing your app as a "service", which install4j supports for all platforms. Quoting from its Features page:
install4j offers full support for
generating and installing services
(daemons). For services, install4j
generates service executables on
Windows, startup items on Mac OS X and
start/stop scripts on Unix/Linux
platforms.
Edit: Regarding #2, JRE detection: install4 has pretty nice, flexible options for JRE bundling/detecting. You can include a JRE (statically or dynamically, shared or not) or prefer to use one already present, with the possibility to download a JRE from within your installer as a fallback. Check these docs for more: How Installers Find a JRE and JRE Bundles.
(More about my experiences with install4j.)
For other tools, you could check some of the earlier Java installer questions on SO:
What’s the best way to distribute Java applications?
Java Application Installers
What is the best installation tool for java?
Commercial tools like InstallAnywhere can do that. After installing the JAR (and links and such), installers can check the registry if Java is already installed and proceed or not. As long as the installer supports all needed plattforms, you are fine.
You could use launch4j , it does not automatically install java however. But maybe there's a way to make that work by packaging the JRE for example.
You may also want to take a look at BitRock InstallBuilder, it provides built-in functionality to perform what you want. We will be happy to build a sample installer for you, just drop us a line.
I recommend downloading the source code for the build scripts of popular Java programs like limewire and follow their example. The end.
...Or you can do like I did:
I have done the JRE detection and
download/install of redistributable if required with
NSIS on Windows. Maybe an .msi is better than a installer.exe which NSIS creates. I believe msi are more like packages on mac so they allow proper updates. Maybe NSIS creates MSIs... not sure.
Mac is simply an Java App
bundle and you specify the JRE and other Java info in the
info.plist and you can optionally create a .pkg
file to give the user an installer and bundle up in a dmg using the command line dmg programs.
I haven't done Linux and don't plan to... sorry :)
All this was a fair chunk of work. I first did the Windows stuff then a couple years later did the Mac stuff.
You can integrate all of this in NSIS build scripts and Ant. I can't remember why I didn't use install4j. Probably I was too cheap :)
Haven't heard of anything that works across all those platforms, but JSmooth might help (fits your needs on Windows). I've used it about 2 years ago and it worked fine.

Categories