Wrapping an executable jar file with an icon - java

Background: I created a simple GUI in java and can run that GUI by clicking its executable jar file. I created the GUI in java so that it could be ran on more than one OS.
Question: Is there a way to make some sort of "wrapper icon" for the jar file so that when I click it (the icon) the GUI opens up?
To be clear I am not talking about changing the .jar file icon, that is determined by the OS. I successfully converted the jar file to a .exe, .app and .sh file with the intent of running a cmd file that would determine the OS and run the appropriate file. But then I started feeling like I was backtracking. I know I am being picky but I just thought I would be useful for my Java programs to have different icons. Any ideas?

I created a simple GUI .. Is there a way to make some sort of "wrapper icon" for the.. (GUI)
Sure. Launch the app. using Java Web Start. It can install desktop icons and menu shortcuts with icons on supported platforms. For further branding, add a splash.

Windows: Launch4J creates an EXE from you JAR and lets you choose its icon. If you want to keep whatever tool you are currently using for building the EXE and then go and change the icon of that EXE, try Resource Hacker:
http://download.cnet.com/Resource-Hacker/3000-2352_4-10178587.html
Mac:
For changing the Mac app bundle icon, add the following in your info.plist
<key>CFBundleIconFile</key>
<string>maciconfile</string>
having put a maciconfile.icns file in your Resources directory.
Xcode Developer Tools which you can get for free if you own a Mac have the tools you need. If you do not have a Mac, there are tools on other platfroms that allow you to build bundles for Mac (such as the OS X Application Bundle Plugin for Maven).

Related

How do I run a java application like I would a typical program, using a shortcut?

I'm currently using an IDE for writing a java application and testing it. In the IDE, I can run the application and see how it works. However, how would I run the application using a shortcut, or a jar file?
For example, in order to run my WAMP server, I run the wamp.exe file in the WAMP directory. So, I'm running a single file which launches the entire program. How do I achieve this sort of thing with a java application? I've heard about using a jar file, but I'm unsure about whether that would be the proper way to do this or not.
It depends on the IDE you are using. With eclipse for example, you open up the file tab, select export, open java in the tree, and select runnable jar file. Then fill the interface out and your good to go.
If you are using Eclipse, then you can export your application as a single jar file and run it directly by double clicking
In the Package Explorer view:
Right click on your project
Go to "Export"
Click on "Export as Runnable Jar"
And you are done
And if you are using Netbeans, then follow these steps:
Right click on your project
Click on "Clean and Build"
Now got the directory where your netbeans projects are created( usually it should be "C:\Users\your_user_name\Documents\NetBeansProjects"
Open the directory of your project (directory with the name of your project name)
Open "dist" folder and you'll find the jar file of your application/project there.
Java Web Start is the easiest way to add shortcuts for a desktop app.
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..
See also
This answer - the 2 icons on the right "JotPad" & "Star Zoom Animation" are icons installed by JWS.
You can also, just for fun, create a launcher in C++ that launches your executable(the way Limewire did it). A simple console mode C program is given below.
#include <stdlib.h>
int main(int argc, char *argv[]) {
system("java YourProgramClassFileNameHere");
return 0;
}
An advanced methodology would require CreateProcess() in win32API or in JNI for your platform.
You can convert it to an executable Jar file(See Abu's answer)
You can also, create a bat file which can run your program: (Windows only)
#echo off
start java YourProgramClassFileNameHere
Or a shell script(BASH)
Also, see here run a executable jar from c++ code

Convert Java Project to .Exe

I've created a game project in NetBeans 6.9, and I want to run the project on another computer. The problem is if NetBeans is not installed on the other computer then surely I cannot run the project.
Therefore I have to convert my project into a .exe that can be run on other computers without having to install NetBeans.
Is there a free application that can convert .java to .exe?
Please help me to convert my project into a .exe file. Thanks.
Check out these official netbeans article for Packaging and Deploying Desktop Java Applications and Developing General Java Applications.
From the article
Running the Application Inside of the IDE
When developing applications in the IDE, typically you will need to test and refine them before distributing them. You can easily test an application that you are working on by running the application from the IDE.
To run the AnotherGrep project in the IDE, right-clicking the project's node (AnotherGrep) in the Projects window and choose Run Project.
The xGrep window should open. You can click the Browse button to choose a file in which to search for a text pattern. In the Search Pattern field, type text or a regular expression pattern that you would like to match, and click Search. The results of each match will appear in the xGrep window's Output area.
Information on regular expressions that you can use in this application are available here and in many other places on the World Wide Web.
Running the Application Outside of the IDE
Once you have finished developing the application and before you distribute it, you will probably want to make sure that the application also works outside of the IDE.
You can run the application outside of the IDE by following these steps:
In your system's file manager (for example, in the My Computer window on Windows XP systems), navigate to PROJECT_HOME/dist and double-click the AnotherGrep.jar file.
You will know that the application has started successfully when the xGrep window opens.
If the xGrep window does not open, your system probably does not have a file association between JAR files and the Java Runtime Environment. See Troubleshooting JAR File Associations.
Distributing the Application to Other Users
Now that you have verified that the application works outside of the IDE, you are ready to distribute it.
You can distribute the application by following these steps:
1: Create a zip file that contains the application JAR file (AnotherGrep.jar) and the accompanying lib folder that contains swing-layout-1.0.jar.
2: Send the file to the people who will use the application. Instruct them to unpack the zip file, making sure that the AnotherGrep.jar file and the lib folder are in the same folder.
The users of your application should be able to run it by double-clicking the JAR file. If this does not work for them, show them the information in the Troubleshooting JAR File Associations section.
Also some third party tools like Launch4j may do the trick. For more information see this article on java-to-exe
See Launch4j. I dislike starting java apps with a batch file. This wraps anything from the root jar all the way up to a complete Java environment in an exe. You get your own icon in the system tray rather than the generic coffee cup. I've used it successfully on a project that has about 300,000 downloads, and it's never been the source of a reported bug. NB It does take some careful reading to pick the right options for your project. But you can set it up with the Ant build script in Netbeans to make the .exe automatically. Very nice and clean. My project includes native libraries. They work fine as well.
I think the NSIS will help you . Most of the applications using NSIS. Because it is open source.
Also there is a Eclipse plugin for NSIS to make the process easy.
http://hmne.sourceforge.net/

File to open terminal and start .jar in OSX?

I currently have a small text game I've written in Java that utilizes System.out.print(); to output text and a Scanner to receive input from the user.
I've compiled the runnable .jar and used IExpress to create a .exe that runs the batch command java -jar "foo.jar" upon execution. It's not the best way to distribute the game, but it's a quick way to distribute it to friends for play testing in Windows.
I'm wondering if there is an equivalent for this for OSX? I know I can write a shell script to open the terminal and execute the .jar file, but will it work if the shell script and .jar are distributed to other computers? Does each person have to give the .sh file "execute" permission? Some of these people aren't really tech savvy and even mentioning opening terminal would turn them off to helping me play test.
So is there a way to give them something to double-click on that will open terminal and run my .jar?
Thanks in advance!
Here are two possible options (the first of which will probably not open the terminal, but I'm leaving here in case you find a way to do so with the below mentioned JarBundler)
1 - Building a native java application bundle and installer
The answer is somewhat two-fold. OSX applications have a specific directory structure which the operating system expects to be in place, they are called application bundles (google for "osx application bundle programmers guide" for the messy intricate details). Jar Bundler is an Ant task which can build a bundle for you without you needing to understand the bundle format itself.
That alone is enough to allow you to drag the application onto the OSX dock. However, what is more common is an installer. To build an installer, you need to build a DMG file. Some good documentation on how to create this with ant can be found here.
The gist of it is to create a disk image, attach to it, copy your application into it, detach from it, and compress it. A typical OSX installer is a window with a copy of your application and a symbolic link to the /Applications directory. When the installer window opens up, it displays both of these items and you normally drag the application to the /Applications icon, "installing it" (in reality, just copying it to /Applications).
The following the relevant Ant build files from my project for building an application bundle as well as building a DMG file (both are google-code links). They do not require any of the GUI tools.
Both of those files are pretty heavily commented.
However, I have a feeling this will not open the terminal window as you desire (you may be able to work with the stubfile option in JarBunder to get it to do what you want).
2 - Create an application bundle from your shell script
This link shows you how to create an application bundle out of a shell script.
You can then create a DMG file out of your application bundle to simplify installation for your users. The DMG file will preserve the execution file attributes so they will not need to manually chmod +x your script.

Creating a stand alone Java application

Is it possible to create a stand alone application in Java (using Swing) and package it into an exe file?
My Idea is, we should be able to double click it and run the application.
If you export a runnable .jar file you will be able to double click and run, but you'll still need the JVM in order to interpret the bytecode.
The best way to deploy a Swing based app. to a client's desktop is using Java Web Start.
JWS offers desktop integration - making a desktop shortcut and menu item, each using an icon if specified. The end use can double-click the desktop shortcut cut to launch the app. ( or single-click the menu item to do the same ;).
Best of all, JWS works for all platforms for which the J2SE is supplied. Windows, Ubuntu, Fedora, Solaris, Mac. OS..
For ensuring the user has the correct minimum JRE needed to run the app., use deployJava.js to write the link to the JNLP launch file.
If you export your project in a .jar file, you will be able to run it when you click on it.
I suppose that you know that you must have the virtual machine installed in the computer that runs the program.(The virtual machine is the thing that allows java to be operative system independent)
And also you must know that if you transform that .jar to an .exe file using some kind of gadget... Then java will not be java anymore, because will not be able to run it in non-windows operative systems.
Remember java slogan
'Write once run everywhere"
See this image here i will explain you how to create a .jar using the eclipse IDE:
1-Select your project in the package explorer
2-Select File>Export from the menu
3-Pick Runnable JAR file from the dialog and
4-Click next and follow the rest of the wizard
You can package your application to JAR file. Application in jar file runs on duoble click.
Here is tutorial. http://download.oracle.com/javase/tutorial/deployment/jar/
You can already do this with a normal .jar file if the user has configured the extension to be executed with the JRE. But you can always use something like Launch4J. Please Note this simply wraps the Jar in a EXE file. The file still needs your Dependencies (.jars) and the JRE enviroment.

Run Java applications By double click

why i can`t run my java compiled applications with double click?
my applicatins just run in cmd (java -jar: my app.jar)
plz help me
You can make a batch file to run your command. This way it will work without the need to make associations to .jar files. If it is a windows app then start it with javaw.exe.
You need to associate the .jar extension with javaw.exe, then no console window will be shown, when you double click the jar file
This is usually done by the JDK/JRE installer, so either you didn't install the JDK properly, or you manually changed that associaten.
I think all the answer here are valid. But if you want a true executable file in Windows then you need to create a launcher. In windows, jar files are not executable. But exe files are. There are many tools out there than wrap all your java code around some native library. I recommend http://launch4j.sourceforge.net/ I think this is what you are looking for. You can easily create executable with icons and many more features.
You can create a Windows shortcut to your .jar file.
The following line could be the target of the Windows shortcut.
%windir%\system32\javaw.exe -Xms1m -Xmx1m -jar "C:\Program Files\Jar Directory\file.jar"
This has the advantage of allowing you to specify the javaw parameters that you want, as well as the Windows parameters (normal window, maximized, start in directory) that you want.
Just assign .jar files to be executed by javaw.exe. You can do that using the Open with dialog on a .jar file.
BTW, doesn't the Java installer do that for you?

Categories