Kalyan Sriram
Kalyan Sriram

Reputation: 31

How to create an independent and executable jar file?

I have built a jar file using Netbeans, and it's working good in my system, but I want to make jar files that are capable of running in all systems that have JREs, and it should work correctly, even the classpath is not set in that systems.

package circle;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Circle {

    
    public static void main(String[] args) {
        
        
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Circle().createAndShowGUI(); 
            }
        });
    }
    
    private void createAndShowGUI() {
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
        
    }
    
    class MyPanel extends JPanel {
        
        public MyPanel() {

        setBorder(BorderFactory.createLineBorder(Color.black));

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
               startX=e.getX();
               startY=e.getY();
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
               X=e.getX();
               Y=e.getY();
               repaint();
            }
        });
        
    }
        
        
        
        public Dimension getPreferredSize() {
        return new Dimension(250,200);
    }
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);       
        //g.setColor(Color.RED);
        //g.fillRect(squareX,squareY,squareW,squareH);
        g.setColor(Color.BLACK);
        g.drawOval(startX,startY,X-startX,Y-startY);
        g.fillOval(startX,startY,X-startX,Y-startY);
    }  
        
        
    }
        
        private int startX,startY,X,Y;
}

Upvotes: 3

Views: 1922

Answers (3)

Mark O'Connor
Mark O'Connor

Reputation: 77971

The following posting has a brief explanation of how to create an executable jar using ANT:

This posting explains how the manifestclasspath task can assist with creating the classpath manifest entry (making the construction of executable jars more robust and less error prone):

Finally a more complex example demonstrating the use of ivy to manage your project's 3rd party dependencies when creating an executable jar:

Upvotes: 3

Roman C
Roman C

Reputation: 1

You can use an ant script to build the runnable JAR.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project SimpleApp with libraries in sub-folder">
    <!--ANT 1.7 is required -->
    <target name="create_run_jar">
        <jar destfile="C:/Workspaces/SimpleApp/SimpleApp.jar">
            <manifest>
                <attribute name="Main-Class" value="SimpleApp"/>
                <attribute name="Class-Path" value=". SimpleApp_lib/lib1.jar SimpleApp_lib/lib2.jar"/>
            </manifest>
            <fileset dir="C:/Workspaces/SimpleApp/bin"/>
        </jar>
        <delete dir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <mkdir dir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <copy file="C:/path/to/lib/lib1.jar" todir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <copy file="C:/path/to/lib/lib2.jar" todir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
    </target>
</project>

In this example, the project's SimpleApp depends on two libs: lib1.jar and lib2.jar, which are output to bin with a MANIFEST.MF having the attributes specified.

Upvotes: 2

Mehdi
Mehdi

Reputation: 4406

Simply You can create a JAR-file by executing following command:

jar -c excel.jar MANIFEST.MF *.class

The MANIFEST.MF-file should contain following line:

Main-Class: createExcel

But consider following tips too :

There are several ways:

  1. Create a jar file and put your classes (without dependencies) there. Use some tool (any IDE has it) to do this and specify class with main function. You can also do it manually from command-line. When user want to run it he should specify classpath and all dependencies should be in that classpath.

  2. Create the same jar and create .bat or .sh file in which set classpath and run your jar.

  3. Create cross-platform installer with some special tool (but good tools aren't free).

Remember that Netbeans can help you a lot ;)

Upvotes: 1

Related Questions