Java to windows executable

Compiling a java program into an executable [duplicate]

I’ve just made a simple program with Eclipse and I want to compile it into an executable, but simply can’t seem to find out how to do it.

7 Answers 7

You can convert .jar file to .exe on these ways:

(source: viralpatel.net)

1- JSmooth .exe wrapper:
JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself. When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a web site.

JSmooth provides a variety of wrappers for your java application, each of them having their own behaviour: Choose your flavour!

2- JarToExe 1.8
Jar2Exe is a tool to convert jar files into exe files. Following are the main features as describe in their website:

  • Can generate “Console”, “Windows GUI”, “Windows Service” three types of exe files.
  • Generated exe files can add program icons and version information.
  • Generated exe files can encrypt and protect java programs, no temporary files will be generated when program runs.
  • Generated exe files provide system tray icon support.
  • Generated exe files provide record system event log support.
  • Generated windows service exe files are able to install/uninstall itself, and support service pause/continue.
  • New release of x64 version, can create 64 bits executives. (May 18, 2008)
  • Both wizard mode and command line mode supported. (May 18, 2008)

3- Executor
Package your Java application as a jar, and Executor will turn the jar into a Windows exe file, indistinguishable from a native application. Simply double-clicking the exe file will invoke the Java Runtime Environment and launch your application.

How do I run a Java program from the command line on Windows?

I’m trying to execute a Java program from the command line in Windows. Here is my code:

I’m not sure how to execute the program — any help? Is this possible on Windows? Why is it different than another environment (I thought JVM was write once, run anywhere)?

12 Answers 12

Let’s say your file is in C:\mywork\

Run Command Prompt

This makes C:\mywork the current directory.

This displays the directory contents. You should see filenamehere.java among the files.

This tells the system where to find JDK programs.

This runs javac.exe, the compiler. You should see nothing but the next system prompt.

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!

To complete the answer :

Compile the Java File to a *.class file

  • This will create a TheJavaFile.class file

Execution of the Java File

Creation of an executable *.jar file

You’ve got two options here —

With an external manifest file :

Create the manifest file say — MANIFEST.mf

The MANIFEST file is nothing but an explicit entry of the Main Class

jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class

Executable by Entry Point:

To run the Jar File

In case your Java class is in some package. Suppose your Java class named ABC.java is present in com.hello.programs , then you need to run it with the package name.

Compile it in the usual way:

But to run it, you need to give the package name and then your java class name:

Complile a Java file to generate a class:

Execute the generated class:

Assuming the file is called «CopyFile.java», do the following:

The first line compiles the source code into executable byte code. The second line executes it, first adding the current directory to the class path (just in case).

Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.

This was an enhancement with JEP 330: https://openjdk.java.net/jeps/330

For the details of the usage and the limitations, see the manual of your Java implementation such as one provided by Oracle: https://docs.oracle.com/en/java/javase/11/tools/java.html

Читайте также:  Увидеть системные файлы windows 10

It is easy. If you have saved your file as A.text first thing you should do is save it as A.java. Now it is a Java file.

Now you need to open cmd and set path to you A.java file before compile it. you can refer this for that.

Then you can compile your file using command

Then run it using

So that is how you compile and run a java program in cmd. You can also go through these material that is Java in depth lessons. Lot of things you need to understand in Java is covered there for beginners.

You can compile any java source using javac in command line ; eg, javac CopyFile.java. To run : java CopyFile. You can also compile all java files using javac *.java as long as they’re in the same directory

If you’re having an issue resulting with «could not find or load main class» you may not have jre in your path. Have a look at this question: Could not find or load main class

On Windows 7 I had to do the following:

quick way

  1. Install JDK http://www.oracle.com/technetwork/java/javase/downloads
  2. in windows, browse into «C:\Program Files\Java\jdk1.8.0_91\bin» (or wherever the latest version of JDK is installed), hold down shift and right click on a blank area within the window and do «open command window here» and this will give you a command line and access to all the BIN tools. «javac» is not by default in the windows system PATH environment variable.
  3. Follow comments above about how to compile the file («javac MyFile.java» then «java MyFile») https://stackoverflow.com/a/33149828/194872

long way

  1. Install JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. After installing, in edits the Windows PATH environment variable and adds the following to the path C:\ProgramData\Oracle\Java\javapath. Within this folder are symbolic links to a handful of java executables but «javac» is NOT one of them so when trying to run «javac» from Windows command line it throws an error.
  3. I edited the path: Control Panel -> System -> Advanced tab -> «Environment Variables. » button -> scroll down to «Path», highlight and edit -> replaced the «C:\ProgramData\Oracle\Java\javapath» with a direct path to the java BIN folder «C:\Program Files\Java\jdk1.8.0_91\bin».

This likely breaks when you upgrade your JDK installation but you have access to all the command line tools now.

Embed a JRE in a Windows executable?

Suppose I want to distribute a Java application.

Suppose I want to distribute it as a single executable. I could easily build a .jar with both the application and all its external dependencies in a single file (with some Ant hacking).

Now suppose I want to distribute it as an .exe file on Windows. That’s easy enough, given the nice tools out there (such as Launch4j and the likes).

But suppose now that I also don’t want to depend on the end user having the right JRE (or any JRE at all for that matter) installed. I want to distribute a JRE with my app, and my app should run on this JRE. It’s easy enough to create a Windows installer executable, and embed a folder with all necessary JRE files in it. But then I’m distributing an installer and not a single-file app.

Is there a way to embed both the application, and a JRE, into an .exe file acting as the application launcher (and not as an installer)?

13 Answers 13

Try to use Avian and ProGuard toolkits. Avian allows to embed lightweight virtual machine in you app. Linux, MacOS, Windows and iOS are supported. And ProGuard allows you to shrink large jar file to prepare to embed.

There’s a new tool that does this: Packr
https://github.com/libgdx/packr

Quite a few options around, commercially this is very good:

There are two key open source options GCJ and VMKit

Other more prolonged paths would include IKVM.NET:

Which is a .NET JVM which can be statically compiled to an EXE

Is there a way to embed both the application, and a JRE, into an .exe file acting as the application launcher (and not as an installer)?

If a commercial tool is ok for you, install4j solves this problem very smoothly (see its «Features» page). It can generate both application launchers and installers.

I’d hate to repeat myself too much, so check e.g. this earlier answer where I plugged it (as installer builder, but it doesn’t make much difference here).

Summary / bottom line: install4j can create native .exe launchers that absolutely do not depend on a pre-installed JRE (or any other specific libs), and it offers flexible options for bundling (and detecting) JREs.

Netbeans allows a Java SE project to be packaged in a native application installer, embedding a JRE. It’s here :https://netbeans.org/kb/docs/java/native_pkg.html 🙂

After encountering the issue myself —

1)create a folder containing your application jar file

2)create a subfolder containing the jre

3)create a bat file that overwrites env variables for the duration of application usage and will launch your application:

the first option is avaliable here:

5)create an exe that contains the folder and runs the bat file when run

I know I’m a little late to this party, however it looks like ej-technologies (love their stuff) has a new solution called exe4j which will do exactly as the OP wanted without any funny business needed.

If you are working in an OpenSource project, they provide free licenses (you just need to contact them). If it’s a commercial project, then a license is required. but at $69 USD for a license, it’s darn cheap and worth while imho.

From their website:

I wrote a solution that has the following:

  • it compiles to just one small exe-file (30 kb) that only calls the jre
  • you compile the exe once from a standalone cpp file (you need to modify the cpp file, replacing «com.example.MyApplication» with your own class name)
  • choose an icon, it will be embedded in exe file
  • the finished exe file is now your java launcher, which catches any errors of java.exe, and shows an information dialog if launch fails
  • no console window

Since the exe file is so tiny, you can build it once and put it the repo. You won’t need to update it unless you rename the main Java class or want to change icon.

Your deployed application would look something like this:

JSmooth can do this according to the documentation. I have only tried it without an embedded JRE, but been very satisfied. It is scriptable with ant and we build on Linux.

You might take a look at: http://ulibgcj.sourceforge.net/uswt.html It has micro-swt library that make easy to build GUI using SWT and have a look at Eclipse GCJ Builder

Install4j is a great tool for this kink of purpose.Also you could check out JSmooth or launch4j-all though I have tried to bundle jres with JSmooth and was not able to..

I found Engima Projector x86 / x64 better than BoxedApp Packer

If you get soft miminizejre by packr than copy your application myapp.jar into launcher\jre\bin\

and copy from installed jre example C:\Program Files\Files\Java\1.8.0_xxx\bin\javaw.exe to launcher\jre\bin\

Open Engima Protector input -> javaw.exe from launcher\jre\bin\

And go Options if you want high compress and Find Miscellaneous -> Command line «-jar myapp.jar»

And click protect and wait for whole dlls and jar into exe

Make sure your myapp.jar should generate by Eclipse with «Package required libraries into generated JAR» because you don’t worry if you have imported many libraries example lwjgl or JavaFX than you must check if embedded javaw_protected.exe has included important files example assets.

But BoxedApp Packer is same too BoxedApp Packer is bit bigger than Engima Protector. Protector is almost best small embedding javaw.exe with assets / resources. Than they can’t crack jar files. I hope you are happy with own jar into exe as less 4 mb without lib directory — If you have problem with jvm.cfg error message It means you don’t copy embedded exe into root directory of jre or jdk

Please make sure embedded exe is here outside of jre or jdk root directory.

I hope you haven’t problem if you use Protector x86 than it need use i586, Protector x64 for jre x64

If you use Protector x64 with jre x86/i386 — It is okay no problem. It works fine.

UPDATED NEW VERSION EXCELSIOR JET + VIRTUAL BOX OR PROTECTOR are almost best «embedded applications»

JAR to EXE : Java program to Windows executable

Java is the best programming language available today that works across different platforms. I love Java and JavaFX. Once you finish writing your application, you may want to release it. The problem is, the native Java program executable, the good old .jar files always have a cup icon and sometimes a simple double click wont open them. The solution to this problem is to convert JARs to native executable, for windows .exe files.

In this article, I will explain how to make windows executable (.exe) from your Java / JavaFX program. Having exe file for windows has many advantages. We can set icons, properties, version information etc.

Step 1 : Make JAR file for your application

The first step is making a JAR file from your java code. This is pretty simple if you are using an IDE like NetBeans.

NetBeans : NetBeans provide option for making JAR file very easily. Just right click on your project from Projects window and then select “clean and build”. This will create a folder called “dist” in your project directory. Inside dist, there will be the final JAR. All the libraries required for running the program will be inside “/dist/lib” directory.

Maven : Maven uses convention over configuration, this means that you only need to tell Maven the things that are different from the defaults. You can use “mvn package” command to create JAR files.

Step 2 : Download Launch4J

Launch4J is so far the best wrapper available for converting JAR files to windows executable. It is written in JAVA and is open source. Download latest version of Launch4J from http://launch4j.sourceforge.net/. Once You install and open it, you will get the following screen.

Launch4J main screen

On the first look, the interface may look little confusing, but it is actually simple and every field will become too important once you get used to it.

Step 3 : Launch4J Basic Configurations

This tab contains the most basic configurations for the exe like jar file to be converted and the icon to be used.

Basic
  • Output File
    This is the location where the compiled EXE (executable) file will be created once the conversion is complete
  • Jar
    Location of the Jar file to be converted in to executable
  • Wrapper manifest
    You can specify manifest file here. This will be used for processing the application against User Account Control(UAC) in windows.
  • Icon
    Here you can specify the icon. It has to be in ico format.
  • Change dir
    Change current directory to an arbitrary path relative to the executable. If you omit this property or leave it blank it will have no effect. Setting it to . will change the current dir to the same directory as the executable. .. will change it to the parent directory, and so on.
  • Command line args
    Here you can specify the java arguments

In case if the system doesn’t have supported Java, the error message and URL to download can be specifed under the Java Download and Support Sections.

Classpath

Here you can specify classpath for your main class. Select the JAR file by selecting the directory icon from right and the default classpath corresponding to main class will be loaded. You can edut them from Edit Item text field.
You can also add or remove classpath for given main class from this section.

Single Instance

Here you can specify whether you need only once instance of the application running at a time. Give a mutext name and window title if single instance option needed.

This section allows configuring JRE. If you are bundling JRE with your product (Which is not a good idea at all), you can specify the path here. You can check the ‘Fallback option’ so that the bundled JRE will only be used when the JRE isntalled on the system does not matches with min and max version requirement.

Min JRE Version is a mandatory field and it has to be specified.You can also specify Heap parameters or any JVM options in the corresponding fields.

Splash

If you need a custom splash screen, you can check the “Enable Splash Screen option”. Then specify the file (Either bitmap image or JAR file). In the TimeOut field, time out can be specified.

Version Info

This is a good way to properly specify version information for your executable. This entries will then be shown in the properties section of executable.

Messages

Messages section allows to create custom messages when errors occur. You can enable this option by checking “Add custom messages”.

Step 4 : Compile and Make Executable

Once the entries are filled, you have to save the configuration. For saving, click on the Floppy DIsk icon from the menu. Your configuration entries will be saved as an xml file called config.xml. After saving click on the Setting like icon next to save button to start converting to executable file.

Launch4J Example Configuration

If it the wrapping was a success, you should see the following messages in Log

You can start your program from executable by clicking on the Play button from menu.

Watch video tutorial from Genuine Coder YouTube channel :-

Читайте также:  Не устанавливается драйвер автоматически windows
Оцените статью