4

I have written a Java program that uses Java swing library. Now I would like to execute this program by double clicking on the executable file on Windows just like any other program with a GUI. How do I do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
atakan
  • 133
  • 1
  • 1
  • 8
  • possible duplicate of [Compiling a java program into an exe](http://stackoverflow.com/questions/2011664/compiling-a-java-program-into-an-exe) – npinti Jul 04 '12 at 06:31
  • I suspect a lot of people are not sure whether your question is *"How do I make a `.exe`?"* or *"How do I make a desktop icon for my app.?"* I added a screen-shot to my answer indicating what **I** think you are asking (2nd option). Please clarify the question. – Andrew Thompson Jul 04 '12 at 07:06

12 Answers12

7

Since it is Java based and has a GUI, the obvious answer is to deploy it using Java Web Start.

Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, AWT, SWT) desktop applications directly from a network or internet link. It offers 'one click' installation for platforms that support Java.

JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..

By 'desktop integration' read desktop shortcuts and menu items on supported platforms.

Desktop Icons on Windows 7

The 2 icons on the right (JotPad & Star Zoom Animation) are both Java based apps., installed using Java Web Start. Since JotPad is sand-boxed, the user will be prompted as to whether to create the shortcut. That choice is not offered for apps. with higher permission levels, so it would make more sense to install/remove the shortcuts and menu items using the IntegrationService - which allows an app. (after prompting the user) to create/remove them at run-time.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

There are number of options:

  1. Create an executable jar of your project. for this jar to work you have to have javaw as default application to open it.
  2. Create an exe of your project.
  3. Create a bat file which runs your jar file.

Take a look at this: How can I convert my Java program to an .exe file?

Community
  • 1
  • 1
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
3

While the others mention excellent choices like creating a native executable, there is another useful method: creating a shortcut.

  • Right click your desktop, expand the "New" option, and click on "Shortcut".
  • Type "javaw.exe". Click next.
  • Name it whatever you want. Click done.
  • You'll notice the newly created shortcut on your desktop. Right click it and choose "Properties"
  • In the "Target" textfield, append "-jar path-to-your-jar.jar" where you replace "path-to-your-jar.jar" with the actual path to your jar
  • You can also now optionally change the icon to whatever icon you want

This shortcut can be pinned to the taskbar and be used from anywhere (considering you provided an absolute path to your JAR).

ra4king
  • 594
  • 3
  • 12
  • hey buddy nice solution but i'm in a bit of trouble, when I click on the icon it displays the message saying "Could not create the java virtual machine" – Saumil Dec 25 '13 at 12:44
  • This may work for a regular java program but it does not work for a GUI. I have tested it and it does not work in WIN 7. – MaxZoom Jan 19 '15 at 23:03
  • 1
    Works fine (whether "regular program" or GUI makes no difference, the shortcut can't tell) under Windows 7 and Windows 10. – Ti Strga Apr 09 '21 at 22:35
2

you need to create exe from the java program.

Creating executable jar files

  1. First, make sure you have installed Java 1.2 or above. This facility is not available in previous versions of Java.
  2. Next, create your working java system. In general, you will want to put it into a package. For this example, I created a trivial HelloWorld application that prints out "Hello World" plus the first command line argument, and placed it into the package "psae". Therefore, the HelloWorld files (HelloWorld.class, HelloWorld.java) were located in the directory psae. I tested the system to make sure it worked before going on to the next step.
  3. In the directory in which the psae is located, created a file called "mainClass". This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package specification. Here is the single line: Main-Class: psae.HelloWorld Note: make sure you type a carriage return after this line; some windows systems need it and will report a "Failed to load Main-Class manifest attribute" error.
  4. Next, I create a jar file called psae.jar using the "jar" command in Java2. I use the "m" command line argument to specify the manifest file mainClass, which adds information to the jar file on where the main class will be found. Here is the jar command: bertha:~ > jar cmf mainClass psae.jar psae
  5. Just for fun, and to check what's happened, I print the table of contents for the jar file I just created. Here's the command and its result: bertha:~ > jar tf psae.jar META-INF/ META-INF/MANIFEST.MF psae/ psae/HelloWorld.java psae/HelloWorld.class
  6. Having successfully created the jar file, I can now invoke java2 on it with the command line argument:

    bertha:~ > java -jar psae.jar Philip
    Hello World Philip
    
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
1

There are a few projects, like http://jsmooth.sourceforge.net/ and http://launch4j.sourceforge.net/

Yasei No Umi
  • 1,574
  • 9
  • 23
1

you can use something like Launch4j. also look at JSMooth.

Hope it helps

Parth Doshi
  • 4,200
  • 15
  • 79
  • 129
1

There are two ways. Both involve packaging your code in a .jar.

The first way is to build an actual .exe file using a tool like Launch4j. It will require you to set up things like tell it which class to execute, which icon to use, which JRE is OK, what JRE parameters to use, etc.

The second option is to make the .jar itself executable. You do this by adding a manifest to the .jar. The manifest is a small configuration file that describes the jar. One of the attributes is Main-Class which defines the entry point. In other words, it says which class has the main function that should be called when the user double-clicks the file. Here's a basic tutorial about manifests: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

The 2nd option is easier to get going, but users will know what to do with a .exe far more often.

Note that if either approach complains that it can't find the class, make sure to set the classpath manifest attribute to match your project.

Adam
  • 16,808
  • 7
  • 52
  • 98
  • *"but users will know what to do with a .exe far more often."* Why should the user ever see an EXE or a Jar? Use JWS and the Jar becomes 'invisible'. – Andrew Thompson Jul 04 '12 at 06:40
0

If you have an executable jar file, just shift-right click on your file and set it to be opened by javaw. The other option (in case you want to pass in parameters to your application) is to create a .bat file where you spin off your application via java or javaw

GETah
  • 20,922
  • 7
  • 61
  • 103
0

Right click to your "project" in eclipse and select "export" then choose "Java->Runnable Jar File" select your project name and finish.

MarsPeople
  • 1,772
  • 18
  • 30
0

Seems you want to deploy and run the standalone application of swings. Being a java developer you should understand the power of jar files. Those are executable in themselves {so no need to create .exe files :)} .

The below code will help you to create a jar file.

Creating a jar File in Command Prompt

Start Command Prompt.
Navigate to the folder that holds your class files:
C:\>cd \mywork
Set path to include JDK’s bin.  For example:
C:\mywork> path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Compile your class(es):
C:\mywork> javac *.java
Create a manifest file:
C:\mywork> echo Main-Class: NameOfProject >manifest.txt
Create a jar file:
C:\mywork> jar cvfm NameOfProject.jar manifest.txt *.class
Test your jar:
C:\mywork> DanceStudio.jar

After creating a jar just double click on it and you are done.

0

You have to create an executable jar file. For that you just simply add a META-INF folder to the jar, then add a MANIFEST.MF text file with two lines:

Manifest-Version: 1.0
Main-Class: your.package.YourMainClass
Eugenio Cuevas
  • 10,858
  • 3
  • 29
  • 51
0

Here's how to run a Java program by RIGHT-CLICKING on it (in other words, from the Windows Explorer context menu). This handy trick is great for beginners who need to test their simple programs on the fly. Works on both Win7 and XP rigs.

[ATTN: Depending on the situation, you may need to remove the package directive from the top of your Java file.]

Step 1. Create a batch file (e.g., RWJ.bat) inside a folder of your choice (say, in C:\Program Files\Java.)

Step 2. Fill RWJ.bat with the following commands (they will work just fine as is with simple classes but you can, of course, tweak them according to your particular needs by specifying compiler / interpreter switches, passing args, adding echo off, removing pause or whatever):

javac %1
java %~n1
pause

The first command passes the full name of your right-clicked file to the Java compiler; the second one strips the file extension and feeds JVM with the class name only.

Step 3. Add the following key to your Registry: HKEY_CLASSES_ROOT\*\shell\Run With Java and then create its command (default value): C:\Program Files\Java\RWJ.bat %1.

Hacking Registry

Step 4. Run your Java class by right-clicking it and selecting Run with Java option.

Right-clicking Java class

Java class up and running

That's all there is to it.

Another way to run Java programs by pointing and clicking is to use AOT compilers. For example, GCC has an entry point named GCJ, which can be used to compile the source code into both byte codes and standard executable file for your particular OS.

And finally, instead ot batch files one can run WSH, etc.

Igor Soudakevitch
  • 671
  • 10
  • 19