如何创建可执行 Java 文件?
How to create an executable Java file?
虽然这个问题可能已经在 SO 中被问过很多次,但我无法创建可执行的 Java 文件 (*.jar)。
我已经严格按照 How do I create executable Java program? 上投票最高的解决方案中的说明进行了尝试,但是当我双击 jar 文件时,没有任何反应(没有弹出窗口 window 出现) .请告知这里可能有什么问题。
谢谢。
============更新===================
对于那些想知道的人,这是我使用的源代码:
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.mf
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.mf
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.8.0_172 (Oracle Corporation)
Main-class: start.SwingExample
这里发生了什么???
如果您的 jar 文件创建成功,请尝试使用一些提取器打开它以查看 class 文件。
Ubuntu 使用存档管理器,windows 使用 winRar。
正在命令提示符中创建 jar 文件
启动命令 Prompt.Navigate 到包含您的 class 文件的文件夹:
C:>cd \mywork
设置路径以包含 JDK 的垃圾箱。例如:
C:\mywork>路径c:\Program Files\Java\jdk1.7.0_25\bin;%path%
编译你的 class(es):
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 废话 *.class
测试你的罐子:
C:\mywork>Craps.jar
或
C:\mywork> java -jar Craps.jar
参考link:http://www.skylit.com/javamethods/faqs/createjar.html
好的,当我将 Manifest.mf 更改为阅读 Main-Class: start.HelloWorldSwing
时,问题似乎已解决。显然,OscarRyz 在 How do I create executable Java program? 的解决方案中也指出了这一点,但当我第一次尝试时,它似乎无法正常工作。
嗯...
============更新================
根据进一步的测试,下面这行代码似乎比 OscarRyz 建议的编译过程更好:
jar cvfm SwingExample.jar manifest.txt *.class
(参考:www.skylit.com/javamethods/faqs/createjar.html)
虽然这个问题可能已经在 SO 中被问过很多次,但我无法创建可执行的 Java 文件 (*.jar)。
我已经严格按照 How do I create executable Java program? 上投票最高的解决方案中的说明进行了尝试,但是当我双击 jar 文件时,没有任何反应(没有弹出窗口 window 出现) .请告知这里可能有什么问题。
谢谢。
============更新===================
对于那些想知道的人,这是我使用的源代码:
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.mf
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.mf
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.8.0_172 (Oracle Corporation)
Main-class: start.SwingExample
这里发生了什么???
如果您的 jar 文件创建成功,请尝试使用一些提取器打开它以查看 class 文件。 Ubuntu 使用存档管理器,windows 使用 winRar。
正在命令提示符中创建 jar 文件
启动命令 Prompt.Navigate 到包含您的 class 文件的文件夹:
C:>cd \mywork
设置路径以包含 JDK 的垃圾箱。例如:
C:\mywork>路径c:\Program Files\Java\jdk1.7.0_25\bin;%path%
编译你的 class(es):
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 废话 *.class
测试你的罐子:
C:\mywork>Craps.jar
或
C:\mywork> java -jar Craps.jar
参考link:http://www.skylit.com/javamethods/faqs/createjar.html
好的,当我将 Manifest.mf 更改为阅读 Main-Class: start.HelloWorldSwing
时,问题似乎已解决。显然,OscarRyz 在 How do I create executable Java program? 的解决方案中也指出了这一点,但当我第一次尝试时,它似乎无法正常工作。
嗯...
============更新================
根据进一步的测试,下面这行代码似乎比 OscarRyz 建议的编译过程更好:
jar cvfm SwingExample.jar manifest.txt *.class
(参考:www.skylit.com/javamethods/faqs/createjar.html)