Creating Windows Executables from Java Applications

Creating Windows Executables from Java Applications

As a Product Manager, I am a huge fan of using the right tools for the right job – and sometimes this conflicts with beliefs I had (and maybe still have) as a developer. One of the beliefs that I have seen out there in developer communities is “if it isn’t in my programming language of choice, I am not going to use it.” In an ideal world, I would fully support this idea. However, we don’t live in an ideal world, and sometimes compromises must be made so we can get the best results out of our software. Our PDF Java Toolkit is an SDK written in Java that is used to create, manipulate, and process PDFs and contains some incredible functionality. It is entirely Java, not Java with C or C++ code underneath it. So, if Java isn’t your programming language of choice, how could you leverage our PDF Java Toolkit to support your business goals? Let’s take a look at a couple of options that turn Java code into executables that can be used from a programming language on top of .NET as an example.

Creating an executable JAR

When I was first introduced to Java, I was initially taught how to compile and run Java applications from the command line using the javac and java commands as outlined in the Oracle “Hello World!” tutorial. This only just begins to scratch the surface of working with Java, and you quickly learn that compiling Java code at the command line for larger projects is not fun. You can end up with some pretty gnarly looking javac and java commands if your project depends on additional libraries. Sure, using an IDE like Eclipse or NetBeans or IntelliJ helps with this because they manage the complex commands required to compile your projects for you. You could also go the route of using Ant or Maven or Gradle (some of the IDEs may even use these behind the scenes).

The piece that sometimes gets lost, though, is how you package your project up and turn it into something that you can give to others and have them run on their computer without needing to compile the source code on their own. One answer is to compile your project into a JAR file, then users can use something like the following to run it:

java -jar MyProject.jar

For those who are comfortable working from a command line, this looks perfectly reasonable. For others, though, they are used to “double clicking” on application icons and seeing something happen. Depending on how you create the JAR, it may not be executable, so “double clicking” does nothing useful. You have to take some additional steps to make them executable. Oracle has a nice walk through for doing this that can be read here. Eclipse (and I am sure other IDEs) also support creating executable JAR files, see the documentation for Eclipse here.

At this point, you would have an executable JAR file that could be called from any other programming language that supports executing command line applications.

Creating a Windows executable

What if we wanted to be a bit more Windows user/developer friendly and create a Windows executable? Lucky for us, a number of people who have come before us have solved this problem too! One solution is to use launch4j to turn your executable JAR into a native Windows executable. Launch4j allows you to wrap an executable JAR file in a native Windows executable with or without an installer, and with or without a bundled Java Runtime. Bundling a Java Runtime in the executable is a neat feature that helps you manage dependencies (and eventual support calls) by allowing you to specify which Java Runtime should be bundled, so that you do not have to direct users of your application to download and install the required version.

After turning launch4j loose on your executable JAR, you have a Windows executable that can be used just like any other Windows executable. It can even be called from other programming languages. Take a look at this example in C#.

Other options

As pointed out in this Stackoverflow question/answer, there are other potential options available for using Java. The decision about what technology to use and how to incorporate it into larger systems is entirely yours, and you must weigh the benefits and drawbacks of each approach and decide what is right for your needs. The one thing to keep in mind, though, is that there are multiple ways to skin a cat, some of them are easier than others. I challenge you to look at Java as an out of the box approach to solving your problems, and I encourage you to look at our PDF Java Toolkit. If, at the end of the day, it easily and effectively solves a problem you have, go with it!

Leave a Reply

Your email address will not be published. Required fields are marked *