Easy way to make a runnable program out of a java project - java

Im doing a small Java project with some school kids to teach them programming and at the end of this project hopefully we will have a small Game.
I would like to give these kids some sort of "exe" file(on an USB-Stick) that should run if you double click it.
I can produce a .jar file, but that wont execute on double clicking it.
The first thing i can think of is a .bat file with java -jar [game.jar] as content but this would require an installed JRE.
Is there any better(but not too complex) way to generate a file(or directory with file in it) that could run anywhere(on Windows is enough) like a portable JRE?
The best solution would be a single File like a self-extracting zip.

You can use Launch4j, it makes an .exe which tries to run the program and if no JRE is found it leads them to the download page of the JRE. I'm just not sure if it can make an .exe with embedded JRE.
Their website: http://launch4j.sourceforge.net/

Because you are using Java, which is dependent on the JRE to run, you are not going to get around not having the JRE installed without significant legwork. Just package instructions with the flash drives you give the kids!
If you're committed though, look into http://www.excelsiorjet.com/ or http://www.duckware.com/jexepack/index.html.
As well as this SO question: How can I convert my Java program to an .exe file?

I can produce a .jar file, but that wont execute on double clicking it.
Why doesn't this work? Is it an issue with having a JRE present, and that these kids might not have it, or might not be able to install it on other's machines?
I suggest you go here if you want go the "exe" route.
http://www.excelsiorjet.com/free
if you click "free licenses" you can get apply for a free license of I believe the "standard edition."
This should be "Good enough" for your intended purposes, and you wont need a JRE present.
It's more work on your end, but it should work nicely for your needs.

Related

Deploying JARs with Java 9 and above (JDK11 in this case) (JLink confusion?)

I'm a Java veteran, but I've been using JDK8 for a long time. I've decided I finally want to upgrade, so I've been using Java11. I've been enjoying the new features, but I've gotten the point where I need to deploy my software.
In the past I would export a runnable JAR from Eclipse and bundle it with an appropriate JRE. Then I'd use both to run the software from an OS-specific program (e.g. an EXE file that fires up the bundled JRE with the given JAR). Of course, now this isn't really an option because JREs are a thing of the past. Supposedly the new system in its place is much lighter weight and straight forward, the only problem is I can't figure out how to actually use it.
I've been reading about how to deploy programs with JDK9 and above and have seen people mention JLink and link documentation to it, but I can't seem to find a straight answer on how to just simply export a runnable JAR. The thing is - I don't really need all of the module support and don't really want to have to configure it. Is there a tool for simply exporting something I can run? How is this done now?
Sorry if this is a dumb question, I'm just genuinely confused at how this all works and can't really find anything online that lays it out in a clear and concise way. There's a lot of documentation on JLink and what it does, but I haven't really found anything that explains the root purpose for all of it.
TL;DR; how do I export working Java programs with JDK9 and above using Eclipse (latest version)?
Thank you for your time!
...how to just simply export a runnable JAR.
Well, if you're not planning on using modules for your application classes, the JAR part should be the same as before. You basically just have to create your own JRE using jlink, e.g.:
jlink --add-modules java.se --output jre
That would create a jre folder with a runtime image that includes all the java.se modules.
You can then bundle that with your JAR like before.
The interesting part here is that you can pick and choose which modules go into this runtime image. java.se is an aggregator module that transitively includes a bunch of other modules. But you could also specify your own specific list of modules, leaving out some of the ones you don't need, making the final runtime image smaller.
I've spent literally months on trying to figure out how to create a single, clickable executable using Java 9+. I now conclude that it is impossible. Whether you use jlink directly yourself, or indirectly via Maven or Gradle, the result is that jlink produces a full directory structure that you are left to "distribute to your users" (somehow, I guess magically, since nothing in Java 9+ tells you anything about how you're supposed to do this). Then, once your user (somehow) has this directory on their machine, they are forced to invoke runtime-image-directory/bin/your-program-name. As though your user is a programmer who is happy to have to install a directory structure, and drill down to invoke a specific file name, which is buried in a directory with lots of other files, rather than being a user who USED to be able to simply double-click on a .jar file to run it. This makes me really wonder if whomever designed all this thought at all about the "user experience". The fact that there appears, as of Java 9+, to be NO WAY to simply deliver ONE file (such as a .jar) to users, together with the fact that just BUILDING this "runtime image" is fantastically complicated, means that developers are going to abandon Java in droves. It really seems to me that the brainiacs at Oracle simply didn't think this through. They have created a death knell to Java by making something onerous for developers to build and distribute, and onerous for users to invoke. I don't see how this situation can be allowed to remain without Java ultimately dying off. Somebody please correct me if I am missing something here, but I've spent months now trying to figure out how to create a runnable jar in Java 9+, USING MODULES, and it appears there is no way to do it.

running java installer with bundled jre

We currently ship a java(jar) installer of our application. Taking into acount the changes to jdk11 we want our users to have the same easy install experience. So, what we are looking for, is to have just one file that can be run even if there is no java installed, it should just start our old java installer.
Probably, this means that we jave to bundle the jre and have a script that runs the jar, but the problem is how to run the batch file natively ? We need this to work on Windows and Mac. Most tools we are looking into require java to run the wrapped jar.
You have to build 2 different scripts/installers, one for each platform. Even looking at very popular software like Chrome, the platform choice is still there (even though you're usually directed to the correct choice based on the information your browser provides them with)
Depending on how much time you can put in this task, the quickest (and dirtiest) solution would be having an archive for each platform that contains the script .bat for Windows and .sh for OS X together with the jre (also different per platform), ask the user to unarchive and run the script which will run your jar with the packed jre. Otherwise, you'll need to create an MSI/exe for windows and a dmg (or other installer type) for MacOs.
I have done the dmg before with a bundled jre and can try to look for the details if you need them (I no longer have access to the code but can probably find the details). It was a free solution but it did require an OS X computer to create the dmg.
One option that I used before and works very well is install4j but the price is not small.
LE: Self contained packaging - although I haven't used this before, it seems like the best current option for your problem.
And an open source option - packr.
that Self Contained packaging doesn't really help, same for packr, same for launch4j. Because all those just generate a application image with a lot of files and directories.
Problem is before that, as an installer you want 1 big exe (or dmg for mac) that does it all, single click
We are already at the stage like SCP or Packr. Because that is easy or current installer.exe and jre\ sub dir and a batch/sh file besides it. Problem is how to get from that directory structure to a single exe that runs.
So what we should have is something that can zip that in a self extracting archive, when clicking on that it should auto extract to the temp dir of the OS, then run directly a command on it (like a batch file or directly in that extracted dir: .\jre\bin\javaw.exe -jar installler.jar)
But nobody seems to have made such a thing, the closest that we have is eclipse with Oomph:
[1] https://git.eclipse.org/c/oomph/org.eclipse.oomph.git/tree/plugins/org.eclipse.oomph.extractor/src/extractor.c
[2] https://git.eclipse.org/c/oomph/org.eclipse.oomph.git/tree/plugins/org.eclipse.oomph.extractor.lib/src/org/eclipse/oomph/extractor/lib/BINExtractor.java
problem is a bit that is doing the extracting through java and still wants a vm first.

How can I make a program executable by double clicking? [duplicate]

This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 8 years ago.
I wrote a JAVA program for my wife to compare coupons stacked with sales on different item sizes to save her time on her shopping-trip-planning. Now I need to put it on her computer in such a way that she can double-click something, and have the program run. I'm not finding anything good on the interwebs (like a decent step by step).
It is a simple program, one class with a nested class, and various imports of swing, awt, and text.
Since it's my wife's computer, I can make sure that she is running the appropriate JRE. Thanks in advance for any help.
You can use Launch4j application. That convert executable jar file to windows native executable application.
You can also embed JRE with it so if JRE is not installed on machine the application will run.
you can also add some other mechanism to it also like loading image, icon, etc.
I assume you are using Eclipse. In this website they given it very clearly for eclipse.
Create Executable files in eclipse
Export it to (or create a) executable Jar.
Most IDE's will do this (I think you have to dig around Eclipse a little) or take a look at Packaging Programs in JAR Files and Setting an Application's Entry Point if you want to do it by hand
Most GUI OS's will run the Jar via a double click.
You could also investigate generating a native executable wrapper, which will make the program "look" more familiar to the user, including launch4j or exe4j or Packaging a Java App for Distribution on a Mac depending on your target platform
Use NetBeans and simply 'Build' the jar file. This will produce an executable file within the 'dist' folder of your project.
Eclipse is the same however slightly more hidden.

Deploy Jar With Java?

I was wondering how to include Java itself with a jar file so that people don't have to have Java installed already. Is it possible and if so, how do you do it?
To execute the jar in the first place you'd need to have java installed. So it would be best to include a JRE installer in a separate file if you'r including one. Also, you'd have to have a different installer for each target platform so this would be somewhat impractical for general distribution because of the inflated file size.
This is like asking "Can we include the chicken that lays the egg, in the egg?". Answer, no.
As to solving the bigger problem though, there is at least one strategy that might work well for applets, JWS apps. and (possibly) single Jars of desktop apps. that are launched from a link (I've never tried that, though). This approach uses JS to check for the right JRE before providing a link to the Jar.
In order to get a computer to do something, you need to have code that the operating system knows how to handle. Most modern operating systems do not know how to handle Java code unless you install a Java Runtime Engine - to them JAR files are just ZIP files.
Hence you need some code which can be executed directly (without Java) and the simplest is just to use a Java launcher. Many exist - see Java packaging tools - alternatives for jsmooth, launch4j, onejar - but e.g. launch4j is maintained and supports the <path> tag to specify a relative path to an included JRE. Those are unfortunately rather large, but you could provide two versions. One with the JRE, and one without (which then prompts the user to install a JRE).

How to generate an exe file from my java project ? Which tool should I use?

I was asked to make a program (in java) by some person but I was stacked as I didn't know how to generate and exe file from my jar file... It would be useless If I install for them the jdk environment as this person don't how to program... Does anyone knows ho to do this or what tool should I use ?
Regards from Córdoba Capital, Argentina...Thanks in advance !!!
You could use launch4J, it's pretty simple to use and to configure.
Read this post, it may help
http://viralpatel.net/blogs/2009/02/convert-jar-to-exe-executable-jar-file-to-exe-converting.html
Regards
Thomas
Here is a post from earlier that has some other options:
How to create a Java application which can be run by a click?
As Lucas mentions above, you will need to install the JRE on the user's machine to get a Java app to run properly. If it is a small program, you could consider making a .bat file (or .sh file on *nux) to allow the user to run the program. Your bat file could as simple as the one below. Just put in a .bat file and add as a short cut. I know there are more elegant solutions out there, but this is simple and should work.
setlocal
set CP=<PATH TO ANY DEPENDENT LIBRARIES OR JARS>
start javaw -cp %CP% <FULLY QUALIFIED MAIN METHOD> <PROGRAM PROPERTIES>
endlocal
They don't need to install the entire JDK to run the jar file, all they need is the significantly smaller JRE. This is standard practice when distributing java applications.
A big advantage of running your java program on the jvm as compared to generating a native binary is that it will, assuming you aren't using a platform-dependent library, be platform-independent straight away.
You can use Excelsior JET. It is not free, work quite slow, but you can create executable (very huge executable). I tested it on Windows and it worked. But executable it created was slower then .jar run using JRE.
There is also GCJ. I tried it, but it was too hard for me to compile my project.
I suggest installing JRE and working with .jar file.

Categories