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
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
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.
you need to create exe from the java program.
Creating executable jar files
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
you can use something like Launch4j. also look at JSMooth.
Hope it helps
Right click to your "project" in eclipse and select "export" then choose "Java->Runnable Jar File" select your project name and finish.
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
.
Step 4. Run your Java class by right-clicking it and selecting Run with Java
option.
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.