-1

I think this stackoverflow question already has my answer: IntelliJ runnable jar doesn`t work on other computers The answer on this question states that most IDEs can autoinclude the needed java libraries to make the jar runnable on pc's without jdk. My question is, where in IntelliJ can I make sure that these libraries are included?

In my project java 15 jdk already shows as external library: link to screenshot

This is what my artifact settings look like: link maybe I need to change something here?

Edit: My question is this: The jar file IntelliJ produces works fine on my computer. On my friends computer excecuting the file gives an JNI error. He only has JRE on his computer and not JDK. I am assuming the problem has something to do with this. How can I make sure the jar file runs on computers without JDK and with JRE?

  • 1
    No. You can package _your application's dependencies_ into a single JAR file with your application, but that still requires a JRE on the target computer. That said, you can use `jlink` to create a self-contained, custom run-time image and/or `jpackage` to create a native installer or executable. Both options embed the JRE with the application. – Slaw Jan 18 '21 at 20:09
  • @Slaw The target computer has JRE in my case. How can I package my application's dependencies into the jar file? My application dependencies already contain java version 15 it seems in the dependencies settings. – Borek Bandell Jan 18 '21 at 21:18
  • When I say dependencies I mean third-party dependencies. You can't package the JRE itself into a JAR file. – Slaw Jan 19 '21 at 05:38
  • @Slaw so there is no way to make my jar executable on another computer with only JRE? They need to have JDK installed? – Borek Bandell Jan 19 '21 at 12:26
  • That's not what I said. I'm beginning to think I don't understand what problem you're actually having. Please create a [mre] and then [edit] your question to add it. Also include the exact error you're getting. – Slaw Jan 19 '21 at 12:41
  • @Slaw I think my problem is not caused by the code but by my IntelliJ settings. I wrote a small gui program. When I compile it to a jar it works fine on my computer. When I send it to a friend he gets a JNI error. I am assuming this is because he does not have JDK installed but only JRE. My question is how I can make sure the code runs on a computer with only JRE. – Borek Bandell Jan 19 '21 at 14:09
  • I don't know what the solution is because I don't know what the problem is. Tell your friend to try launching the JAR file from the command line. What error do they get? – Slaw Jan 19 '21 at 14:26
  • @Slaw I told him to do that and it showed the following error: java.lang.UnsupportedClassVersionError: app has been compiled by a more recent version of the Java Runtime (class file version 58.0), this version of the Java Runtime only recognizes class file versions up to 52.0. I looked it up and it seems like he has java jre 8 while the code is compiled with jdk 15. JRE 15 does not seem to exist. How can I make it work if JRE 15 does not exist? – Borek Bandell Jan 20 '21 at 15:57
  • Well, as noted in my first comment and AlBlue's answer, you can use `jlink`. You can also use `jpackage`. The former [supports "cross-targeting"](https://stackoverflow.com/questions/47593409/create-java-runtime-image-on-one-platform-for-another-using-jlink) whereas the latter [does not](https://docs.oracle.com/en/java/javase/15/jpackage/packaging-overview.html#GUID-786E15C0-2CE7-4BDF-9B2F-AC1C57249134). Also note that the former requires all code to be modular whereas the latter can package class-path based applications. – Slaw Jan 21 '21 at 15:47
  • Otherwise, your friend has at least two options. (1) Download the JDK and just deal with having development tools alongside the run-time environment; remember that the JDK is a superset of the JRE, meaning the JDK is just the JRE with development tools included. (2) Use a different vendor than Oracle to get a JRE-only download; I believe vendors such as Zulu Community, BellSoft Liberica, and AdoptOpenJDK all provide JRE-only downloads. – Slaw Jan 21 '21 at 15:50
  • Be careful with distributing your application using Oracle's distribution of the JDK/JRE. With Java 9 Oracle has changed it's licensing. I believe it's okay for developmental and personal uses, but I'm unsure how it applies to noncommercial sharing (I'm not a lawyer). – Slaw Jan 21 '21 at 15:55
  • @Slaw thanks a lot for taking the time to help me with this problem. I fixed it by compiling my code with java 8 instead of 15. The latest JRE is 8 so it now works on computers with the latest JRE installed. – Borek Bandell Jan 23 '21 at 13:17
  • Just to clarify, the latest JRE is Java 15. It's simply that Oracle—specifically—does not distribute one separately anymore. The latest JRE _distributed by Oracle_ is Java 8. But again, you can get a JRE from other vendors (see [here](https://www.azul.com/downloads/zulu-community/?package=jre) for example). However, if your friend can't upgrade then yes, you have to target Java 8. – Slaw Jan 23 '21 at 21:07
  • @Slaw Thanks for telling that I didn't know that. Most common pc users only download the official Java from Oracle I suppose. That's the audience I am targeting so I will have to stay with Java 8. – Borek Bandell Jan 24 '21 at 21:29
  • If you're creating _desktop_ applications then you should really consider using `jpackage` (Java 14+). The philosophy of Java has changed in recent years. Now, instead of expecting end users to have Java installed separately, you can just bundle the JRE with your application. – Slaw Jan 24 '21 at 22:14
  • Thanks that seems like what I am looking for. I will look into it. – Borek Bandell Jan 25 '21 at 11:34
  • Does this answer your question? [Is there any way to use jlink in IntelliJ IDEA to create a custom modular run-time image?](https://stackoverflow.com/questions/53230489/is-there-any-way-to-use-jlink-in-intellij-idea-to-create-a-custom-modular-run-ti) – tevemadar Mar 10 '23 at 19:26

1 Answers1

1

Your program can run without the jdk but it will need a jre -- the Java Virtual Machine is needed.

You can create this on Java 11 and above using jlink which creates a custom cut-down Java runtime, along with your code, that can be executed on a different computer. However, it will have to be the same architecture/operating system; you can't run the Windows JVM on macOS or vice-versa, for example. Your application code, in the .jar, will be the same on both though.

AlBlue
  • 23,254
  • 14
  • 71
  • 91