I'm creating a game compatible with Discord's Rich Presence, and as Discord uses exe files to launch games, when I'm trying to launch my game from Discord, it starts the java.exe executable coming from the JRE instead of the jarfile of my game.
What I'm asking is, is there any way to copy jre (to bundle it with the game) and setup it to start automatically a specific jarfile ? If yes, how ?
If it's not possible, is there any way to launch a jar from an exe without launching java.exe separately ?
Thanks for your help.
Sorry for bad English, I'm french
Java 14 includes a packaging tool that allows exactly that. There are also tutorials available on how to do that, e.g. here. In short, something like this should work:
jpackage --input target/ \
--name MyApp \
--main-jar myApp.jar \
--main-class com.company.MyApp \
--type exe \
--java-options '--enable-preview'
If you are not yet using Java 14 but an older version, have a look at WinRun4J or Launch4J. Both allow creating exe-wrappers for Jar-Files, or creating an exe-file along with a JRE and a jar-File to run it independently. Those two only work for Windows, whereas the Java packaging tool will create executables for each platform (Windows, macOS, Linux) when run on the respective platform (it's not cross-platform capable, e.g. creating a macOS executable on Windows).
For macOS, there is appbundler which is able to create a .app with JRE and jar-File included, so users can just double click the application to start the java application.
Related
I've created my Application in JavaFX. Everything works well, so I use the Jpackage to make a installable version of my application in Window.
After I install the package, the window computer still ask to install JVM.
jpackage -t exe --name "FReport" --description "Time reporting" --app-version 1.0 --input "C:\myapp" --dest "C:\myapp\out" --main-jar "freport.jar" --module-path "C:\Program Files\Java\javafx-sdk-17.0.1\lib" --add-modules javafx.controls,javafx.fxml --win-shortcut --win-menu
My expectation, that I can use Jpackage to install my JavaFX application on any computer and it automatically setup the JVM ..etc. The user just have to click on the icon and run the application.
Any suggestion on the correct command in Jpackage to pack my JavaFX application jar file with all the require environment like JVM ..etc
You have not specified the correct modules for JavaFX - you have referenced the lib files not modules so jlink won't contain the JavaFX libraries because it does not read the modules such as javafx.base.jmod. Download jmods JDK and reference C:\somepath\javafx-jmods-VER instead of the lib release.
Additionally: turn on console by adding --win-console and report back what the actual error messages are when your run the installer and the application EXEs.
You have specified --input with nested --dest inside, so you will get your whole application copied into the second release
Test your application with the installed runtime image, or as separate image (see below). Simply reference the installer location of jlink runtime with your current application to see which modules are missing.
If you don't update -app-version each time then the new package is NOT installed by any of the subsequent attempts unless you un-install the previous installation. This is very frustrating, jpackage ought to warn on re-install same version rather than give up with no error message.
Note all of these other typical jpackage issues:
Enable console
update version number each run
minimise
use manual jlink to create JavaFX JRE, then you can test the JRE before running jpackage
I have tried the following:
in terminal it works
In Intellij it works
I have tried to launch it with javaw.exe but nothing changes
Are there any other options?
This looks like an OS problem that an application building one -- because you said that it works in your IDE and terminal.
Make sure that you installed Java properly in your machine.
In Windows/MacOs, after installing Java, the *.jar files are automatically associated with the java -jar command and makes it runnable via double-click.
In linux, this varies on the flavour or DE you are using. But there's probably a utility in your OS to open *.jar files using java -jar command.
For running a java code (that includes a JXInput code for a Joystick use), I only run this .bat:
jre1.8.0_66\bin\java.exe -Djava.library.path=d:\HG -jar HG11.9.jar
But, that works if I has PREVIOUSLY installed the VC redistributable.
As some people are not software specialists, they don't know how to install this vc_redist_.exe from https://www.microsoft.com/en-US/download/details.aspx?id=48145. I mention "" because it depends on the system they have (vc_redist.x64.exe or vc_redist.x86.exe).
I am sure that this problematics consisting in facilitating procedure of installation of a game and, more generally, a product has been already solved. So, what is the good practice to manually avoid to install the vc_redist?
In other words, is it possible to include this vc_redist in the .jar? Then, only running a .bat (like above) allowing to run the game.
If not, is it possible to run a single .bat or something else that:
- unzip the .zip file of my game (this file includes the game and the vc_redist)
- then automatically install the vc_redist, depending on the system (32 or 64 bits) - by what means?
Thanks a lot for your response
If you already have bat file for install best way to do this will be to include install of vc_redist in this *.bat before install. From what I know it is not posiible to include this in the *.jar. Better will be to pack all files into zip (together with this *.bat script).
To install vcredist in bat silently you will have to add these commands:
#echo OFF
reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /i "x86" > NUL && set OS=32BIT || set OS=64BIT
if %OS%==32BIT runas /user:Administrator %~dp0\vcredist_x86.exe /q /norestart
if %OS%==64BIT runas /user:Administrator %~dp0\vcredist_x64.exe /q /norestart
#rest of your script
Checking if system is 32bit of 64bit is taken from here (batch file to check 64bit or 32bit OS)
%~dp0 resolves to the full path of the folder in which the batch script resides.
Edit:
As the runas might not be the best solution to problem of running this as administrator I will direct you to these posts to read about running with administrator privilages in bat
How to code a BAT file to always run as admin mode?
How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.
javapackager and javafxpackager don't seem to be recognised on the command line for me. They don't show up in the terminal, even after I installed the latest 1.8 SDK. (Even 'echo $JAVA_HOME' seems to be drawing a blank, though java -version seems to work fine.)
If I look under /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/ I can see the javapackager and javafxpackager tools are present, but if I follow /usr/libexec/java_home back to it's origin in /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/ , there's no sign of them.
The only other discussions I could find on the subject were a blog post and mailing list from about a year and a half back:
https://devreboot.wordpress.com/2014/11/26/java-desktop-app-packaging-automation/
http://lists.apple.com/archives/java-dev/2015/Nov/msg00009.html
I realise OSX hasn't come with java 'by default' for some time, but that's kinda why I was hoping to release my application as a self-contained bundle with it's own VM. Would it be standard procedure to update my bash profile to point at the tool explicitly? Is there something screwy about my personal setup, or is there some gap in the tool support on OSX?
PS: I realise there's an older thread on this subject below:
What is the best way to deploy JavaFX application, create JAR and self-contained applications and native installers
However, that describes javapackager as a .jar file, and whatever I've got doesn't seem to be a .jar file. I'm legitimately confused about what I'm supposed to do with it.
EDIT: Thanks to everyone for the tips- I think I have enough info to go on for now.
You could try this gist for building, packaging and running a test install on Java client apps from the OS X command line. No guarantees it will work for you, it was just something I whipped up for personal development purposes a long time ago. But, the info in there may help in resolving packager tool locations from the command line and also in performing other packaging related functions.
The key part for locating (and using) the javapackager is:
# select java version
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
$JAVA_HOME/bin/java -version
...
# make an executable jar file
$JAVA_HOME/bin/javapackager -createjar -srcdir . -appclass start.HelloWorldSwing -srcfiles HelloWorldSwing.jar -outdir . -outfile HelloWorld.jar
# package the jar and java runtime as a native application with installer
$JAVA_HOME/bin/javapackager -deploy -srcdir . -srcfiles HelloWorld.jar -outdir . -outfile HelloWorld -appclass start.HelloWorldSwing -native -name HelloWorld
Note, the above is for packaging a Swing application. Packaging a JavaFX application will use slightly different command line options for the packager.
Doing things this way from command line scripts is decidedly old school, usually maven or gradle is used.
My personal preference would be to just use Ant, but I guess that's only slightly less old-school?
Yes, not that there is anything wrong with that. Documentation on using Ant to package java client applications is provided by Oracle.