0

I'm trying to package a java jar file on a Windows operating system such that the user doesn't have to worry about installing jre on a Mac OS. Can I just simply place a copy of the jre in the folder where the jar file resides and point the java_home environment variable to that jre so that when I double click the jar file, it knows where the jre is located if there isn't one installed on the target (Mac OS) system?

I've been looking at the following link:

https://docs.oracle.com/javase/7/docs/technotes/guides/jweb/packagingAppsForMac.html

But it seems to have more to do with deploying apps through the Mac App Store. I just basically want the user to be able to click and run the java jar file without having to install JRE.

I've also looked at some links that say to use Launch4j or jPortable, but I'd rather not have to depend on 3rd party software.

How to run jar without JRE installed manually?

How do I run a JAR file in Ubuntu that was compiled on windows?

thokuest
  • 5,800
  • 24
  • 36
macdays
  • 63
  • 6
  • 1
    Have a look at [jpackage](https://www.baeldung.com/java14-jpackage) which is part of Java 14+. – cello Nov 03 '20 at 18:34
  • IntelliJ creates independent executable's for all operating systems – Joe Nov 03 '20 at 18:39
  • @cello you answered my question. jpackage is exactly what I was looking for. Please post this as an answer. – macdays Nov 05 '20 at 02:16
  • @macdays: thanks! I wasn't sure if Java 14+ covered your requirements, as you did not specify any java version in your question. – cello Nov 05 '20 at 08:25

1 Answers1

4

Java 14 and newer include a new tool called jpackage that creates native executables on each platform. There are tutorials available on how to use it, 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 dmg \
  --java-options '--enable-preview'
cello
  • 5,356
  • 3
  • 23
  • 28
  • I tried out jpackage and it didn't work for me. At this point, I'm willing to try even third party tools. I've tried install4j with no success. Another website I found said to use install4j with Inno Setup Compiler. This was more complicated and didn't work either. I thought maybe WiX Toolset might work. I don't really care how, I just want to create a windows installer for my jar file. I don't want to have to install jre/jdk on the target computer I want to deploy to. – macdays Nov 28 '20 at 05:58
  • please explain why it didn't work for you. Did you get an error message? Did you expect different behaviour? Without more details, people will not be able to help you. – cello Nov 28 '20 at 10:45
  • Nevermind, it was my fault it wasn't working. I had a bunch of extra files in the directory where the jar file was, so I just created a new directory and only put NtbnsProj.jar in it. I then ran the following command: ```jpackage --name Test --input . --main-jar NtbnsProj.jar``` For details see my related post: https://stackoverflow.com/questions/65047035/how-do-i-create-a-windows-installer-for-a-java-jar-file – macdays Dec 02 '20 at 23:59