Search code examples
javaruntime-environment

How do I make a java .exe to allow user to interact as I would in the eclipse command window?


I wrote a simple java code that would take simple inputs from the user in the command window (of eclipse for me) using nextInt() and nextLine(). However, I realized that others need JRE (I believe?) on their computer to run the executable jar file made. So I was wondering if there is a way to get around that by making the app produce a window that is like the command window to have the same interaction as the command window in eclipse.

So, if I were to run the .jar or .exe then a simple window would pop up that acts like the console of eclipse displaying lines from System.out.println() and etc.


Solution

  • For programs written in Java, they are compiled as a jar file, like you mentioned, and how these compiled versions of your source code differs from many other programming languages is that they do not contain the assembly/machine code like for example a compiled C program would have. They are instead compiled as bytecode. Which is special code for execution by a Java Virtual Machine. Here is a good Wikipedia reference: link

    To answer your question, yes, others need a JRE (Java Runtime Environment) and this can be either:

    1. Installed by themselves (this is what you mentioned)

    2. Packaged together with your java app, to provide a download-and-click experience.

    For option 1, assuming they already have it installed, they can simply run it by executing the jar file with javaw, more information on that is in this previously answered SO question

    For option 2, the process is fairly lengthy and I'll point you to the official docs to refer to: self contained executables and Deploying java apps

    If you have a more complex project with third party libraries and what not, look at this SO question

    In the past, I've also found launch4j, a cross-platform wrapper to be very useful, it automates the process of going from jar to an executable (made a simple game that using Swing, simple and ugly thing it was), but the user still needs a JRE, nonetheless. :)

    Hope this helps!