创建一个独立的jar文件

创建一个独立的jar文件

本文介绍了创建一个独立的jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Netbeans构建了一个 jar 文件,它在我的系统中运行良好。但是,我想要使具有JRE能够运行的 jar 文件,即使是 classpath 不在该系统中设置。

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


推荐答案

以下帖子有一个简短的说明如何使用ANT创建:

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

这篇文章解释了任务如何帮助创建类路径清单条目(使可执行jar的构造更加稳健,更容易出错):

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

最后一个更复杂的例子演示了如何使用来管理您的项目在创建可执行jar时的第三方依赖关系:

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

这篇关于创建一个独立的jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 04:03