Srikanth Technologies

Creating Desktop Shortcut for Java Application

In this blog, I explain how to create a desktop shortcut to run a Java application. The need is to invoke a Java Swing application by double clicking on a shortcut on the desktop.

Assume we have a program (shown below) that displays the running digital clock at the center of the frame.  Place this code in DigitalClock.java and then compile this program to create DigitalClock.class.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;

public class DigitalClock extends JFrame implements ActionListener {
  JLabel  l1 = new JLabel();
  Timer  t;
  public  DigitalClock() {
     super("Digital Clock");
     l1.setFont( new Font("Verdana",Font.BOLD,30) );
     l1.setHorizontalAlignment( JLabel.CENTER);
     t = new Timer(1000,this);
     t.start();
     getContentPane().add(l1);
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     setSize(200,200);
     setVisible(true);
     // call actionPerformed to get Time at the startup
     actionPerformed(null);
  }
  public void actionPerformed(ActionEvent evt)  {
      l1.setText( new Date().toString().substring(11,19));
  }
  public static void main(String args[]) {
     new DigitalClock();
  }
} // end of class

Follow the steps given below to create a JAR (Java Archive) file associated with a MF (Manifest) file, which indicates the class that must be run when you run Jar file.

Create a manifest file called clock.mf as follows.

Manifest-Version: 1.0
Main-Class: DigitalClock

Create clock.jar file using JAR utility provide by JDK. Jar file is associated with manifest file clock.mf and program DigitalClock.class.

jar cfm clock.jar clock.mf DigitalClock.class

After clock.jar is created, you can invoke main class (DigitalClock.class) by running clock.jar as follows.

java -jar clock.jar

But our aim is to run this file from a shortcut on desktop. So follow the steps to create a desktop shortcut as follows:
  1. Right click on Desktop
  2. Select New->ShortCut option from the popup menu
  3. Using Browse button select clock.jar file
  4. Click on Next button
  5. Enter Clock as the name for shortcut.
  6. Click on Finish
Once shortcut is created on the desktop, just double click on the shortcut to run DigitalClock.class.

In case you want to give a better icon to shortcut, right click on shortcut and select Properties from popup menu and select Change Icon option.