Creating an independent jar file

心不动则不痛 提交于 2019-12-01 20:26:15

问题


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

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;
}

回答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.




回答2:


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

  • Including external jar-files in a new jar-file build with 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):

  • Ant - how to get all files' name in a specific folder

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

  • Error in classpath generation from default-template ant



回答3:


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 ;)



来源:https://stackoverflow.com/questions/12229300/creating-an-independent-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!