gpt4 book ai didi

java - 创建一个独立的jar文件

转载 作者:搜寻专家 更新时间:2023-11-01 02:29:51 25 4
gpt4 key购买 nike

我已经使用 Netbeans 构建了一个 jar 文件,它在我的系统中运行良好。但我想制作能够在所有系统中运行的 jar 文件,它具有 JRE,即使 classpath 未在该系统中设置,它也应该正常工作。

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 脚本构建可运行的 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>

在这个例子中,项目的SimpleApp依赖两个库:lib1.jarlib2.jar,输出到bin 与具有指定属性的 MANIFEST.MF

关于java - 创建一个独立的jar文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12229300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com