Introduction
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. We use Java extensively to create, manipulate, and process PDFs and to achieve some incredible functionality. It is entirely Java, not Java with C or C++ code underneath it. However, if Java isn’t your programming language of choice, here’s 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 to support your business goals.
Background
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 this Oracle "Hello World!" tutorial: https://docs.oracle.com/javase/tutorial/getStarted/cupojava/unix.html#unix-2. 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: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html. Eclipse (and I am sure other IDEs) also support creating executable JAR files. 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.
Using the code
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 (http://launch4j.sourceforge.net/index.html) 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 (http://stackoverflow.com/questions/9679375/run-an-exe-from-c-sharp-code) in C#:
using System.Diagnostics;
class Program
{
static void Main()
{
Process.Start("C:\\");
}
}
If your application needs cmd arguments, use something like this:
using System.Diagnostics;
class Program
{
static void Main()
{
LaunchCommandLineApp();
}
static void LaunchCommandLineApp()
{
const string ex1 = "C:\\";
const string ex2 = "C:\\Dir";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
}
}
}
Points of Interest
Other options
As pointed out in this Stackoverflow question/answer: http://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file, 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. If, at the end of the day you find the right tool that easily and effectively solves a problem you have, go with it!