[英]How to create an executable Java file?
尽管在SO中可能已经多次询问过此问题,但是我无法创建可执行Java文件(* .jar)。
我已按照如何创建可执行Java程序中投票最高的解决方案中的说明进行了严格尝试。 ,但是当我双击jar文件时,没有任何反应(没有弹出窗口出现)。 请告知此处可能存在的问题。
谢谢。
============更新===================
对于那些想知道的人,这里是我使用的源代码:
HelloWorldSwing.java
package start;
import javax.swing.*;
public class HelloWorldSwing {
public static void main(String[] args) {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
frame.add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
class Dummy {
// just to have another thing to pack in the jar
}
清单文件
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.8.0_172 (Oracle Corporation)
Main-Class: HelloWorldSwing
=========================================
现在,我尝试了另一个* .java文件,同样的问题再次出现! 请查看下面的代码以获取详细信息:
码:
SwingExample.java
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
public class SwingExample implements Runnable {
@Override
public void run() {
// Create the window
JFrame f = new JFrame("Hello, !");
// Sets the behavior for when the window is closed
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Add a layout manager so that the button is not placed on top of the label
f.setLayout(new FlowLayout());
// Add a label and a button
f.add(new JLabel("Hello, world!"));
f.add(new JButton("Press me!"));
// Arrange the components inside the window
f.pack();
// By default, the window is not visible. Make it visible.
f.setVisible(true);
}
public static void main(String[] args) {
SwingExample se = new SwingExample();
// Schedules the application to be run at the correct time in the event queue.
SwingUtilities.invokeLater(se);
}
}
清单文件
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.8.0_172 (Oracle Corporation)
Main-class: start.SwingExample
WTH正在这里发生???
在命令提示符下创建jar文件
启动命令提示符,导航到保存类文件的文件夹:
C:> cd \\ mywork
设置路径以包含JDK的bin。 例如:
C:\\ mywork>路径c:\\ Program Files \\ Java \\ jdk1.7.0_25 \\ bin;%path%
编译您的课程:
C:\\ mywork> javac * .java
创建清单文件和您的jar文件:
C:\\ mywork> echo Main-Class:Craps> manifest.txt
C:\\ mywork> jar cvfm Craps.jar manifest.txt * .class
要么
C:\\ mywork> jar cvfe Craps.jar Craps * .class
测试你的罐子:
C:\\ mywork> Craps.jar
要么
C:\\ mywork> java -jar Craps.jar
参考链接: http : //www.skylit.com/javamethods/faqs/createjar.html
如果您的jar文件创建成功,则尝试使用一些提取器将其打开,以查看类文件。 对于Ubuntu使用存档管理器,对于Windows使用winRar。
好的,当我将Manifest.mf更改为Main-Class: start.HelloWorldSwing
时,似乎问题已解决。 显然,OscarRyz在其解决方案“ 如何创建可执行Java程序”中也指出了这一点。 ,但是当我第一次尝试时,它似乎无法正常工作。
嗯...
============更新===============
根据进一步的测试,似乎以下代码行比OscarRyz建议的编译过程更好:
jar cvfm SwingExample.jar manifest.txt *.class
(参考:www.skylit.com/javamethods/faqs/createjar.html)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.