Welcome Message

"The difference between a successful person and others is not a lack of strength,
not a lack of knowledge, but rather a lack in will."
-Vince Lombardi

April 9, 2013

Executable Java Jar File and Windows Exe File Creation

How to create an executable java jar file? I want to just double click the jar file and the application should run, please help me. I want to convert my java application to a windows exe file, how can I do it? These are two frequently asked questions.

For you guys who know about these things already you may skip this article, as there is no surprise element embedded. For us who are interested in learning new things go ahead, anything new adds up.

How to Create an Executable JAR File?

JDK gives us a tool to create jar files.

Before creating a java jar file, we need to create a java manifest file.

Command to create an executable java jar file:

jar -cvfm BundleExample.jar manifes 
t.txt com/javapapers/corejava/*.class

How to create a java manifest file?

Generally we will have the manifest file located in,

  • META-INF/manifest.mf

This manifest file contains information about the jar file. Information like which is the main java class to be executed, who created the application, version of the application, etc.

It is a regular text file. Remember to press enter at the end of each line and importantly in the last line.

So with respect to creating an executable java jar file, we need to add the main class of the application using which it can be launched. Our example manifest file is like below,

Main-Class: com.javapapers.corejava.JavaBundleExample 

This is the only line I have and that is sufficient for our purpose.

Sample Application

We need a sample java application to enjoy this. Lets create a simple hello world type swing application.

package com.javapapers.corejava;

 

import java.awt.GridLayout;

 

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

 

public class JavaBundleExample {

 

  private static void createAndShowGUI() {

 

    JFrame.setDefaultLookAndFeelDecorated(true);

    JFrame frame = new JFrame("Bundle Example");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel pane = new JPanel(new GridLayout(0, 1));

    JButton button = new JButton("Dummy Button!");

    pane.add(button);

    JLabel label = new JLabel("Example for Bundling JRE with Java Class");

    pane.add(label);

    pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));

    frame.getContentPane().add(pane);

    frame.pack();

    frame.setVisible(true);

  }

 

  public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {

      public void run() {

        createAndShowGUI();

      }

    });

  }

}

Output:

Just execute the following line (command) to create the jar file, you should have added JDK to PATH.

jar -cvfm BundleExample.jar manifes 
t.txt com/javapapers/corejava/*.class

Now we can double click the jar file and the application will launch and run.

How to Create a Windows Exe file?

If we create a windows native exe file, then we loose the platform independent-ness.

Let us use a tool called Launch4J. It’s a nice tool, works good and easy to use.

Its core features are,

  • creating lightweight Windows native EXE
  • JRE version control
  • app icon
  • splash screen
  • process name
  • Java download page

Download Launch4J and use the GUI to create the windows/Java exe file. Launc4J creates an XML file to save the configuration.

  • I have specified the output file, this is the name of the exe file to be created.
  • Jar is the input file which we want to bundle as an exe file. Lets use the Java JAR which we created in the previous step.
  • In JRE tab, we can specify the Min JRE version required. So this will detect the JRE available in the system and verify if the version is sufficient.
  • Click the cog wheel icon from the menu to generate the exe file.

Source: javapapers.com

April 4, 2013

Ant Colony Optimization in Java

Long back I introduced Wordle word clouds as part of Java gallery and then completely forgot about the gallery category. Today I was reading about ant colony optimization and came across a nice implementation of it in Java. Thought of sharing this Java application to you as part of Java gallery.

Ant colony optimization is an awesome algorithm inspired by ant’s natural intelligence. Like cockroaches, ants are extremely successful insects surviving for millions of years. Ants live in colonies and they have hierarchies among them. Physical castes are, like worker ants have responsibilities divided based on their size.

Ants communicate within themselves effectively. Their form of communication is efficient enough to help them survive for millions of years. Apart from sound, touch they use a secreted chemical called pheromone to communicate. Ants go out in search of food and once it finds a food source, on its return back to home ants spit pheromone on the trail. If it comes across obstacles during its way back, the group gets dispersed to find a shortest route.

Ants use pheromones to find the shortest path between home and food source. Pheromones evaporate quickly. Assume that there are two path trails formed by ants between its home and food source. When an ant walks out looking for food, it will choose the path where the pheromone is denser. Since the shortest path will have denser pheromone.

Christian Borgelt has created a nice implementation of ant colony optimization in Java. It is worth having a look at it. He has used Java Swing, Awt, for UI using which the traversal for shortest path is shown.

=========

Source: javapapers.com