问题描述
我已经在 JCreator 中编写了一个 Java 程序,一切都完成了,但是我想从中创建一个可执行文件,即我不想通过加载 Java 类并编译然后执行来运行程序,而是有它作为一个独立的可执行文件.
最快的方法是什么?
您可以使用 .
一旦您熟悉了这种.jar"方法,我认为您可以继续使用此线程中发布的其他链接.
您也可以使用 jnlp(Java 网络启动器协议).
I have programmed a Java Program in JCreator, everything is done, but I want to create an executable file from it, ie I dont want to have to run the program by loading the java classes and compiling then executing, but instead have it as a stand alone executable file.
What the quickest way to do this?
You can use the jar tool bundled with the SDK and create an executable version of the program.
This is how it's done.
I'm posting the results from my command prompt because it's easier, but the same should apply when using JCreator.
First create your program:
$cat 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
}
Very simple, just displays a window with "Hello World"
Then compile it:
$javac -d . HelloWorldSwing.java
Two files were created inside the "start" folder Dummy.class and HelloWorldSwing.class.
$ls start/
Dummy.class HelloWorldSwing.class
Next step, create the jar file. Each jar file have a manifest file, where attributes related to the executable file are.
This is the content of my manifest file.
$cat manifest.mf
Main-class: start.HelloWorldSwing
Just describe what the main class is ( the one with the public static void main method )
Once the manifest is ready, the jar executable is invoked.
It has many options, here I'm using -c -m -f ( -c to create jar, -m to specify the manifest file , -f = the file should be named.. ) and the folder I want to jar.
$jar -cmf manifest.mf hello.jar start
This creates the .jar file on the system
You can later just double click on that file and it will run as expected.
To create the .jar file in JCreator you just have to use "Tools" menu, create jar, but I'm not sure how the manifest goes there.
Here's a video I've found about: Create a Jar File in Jcreator.
I think you may proceed with the other links posted in this thread once you're familiar with this ".jar" approach.
You can also use jnlp ( Java Network Launcher Protocol ) too.
这篇关于如何创建可执行的 Java 程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!