Đẻ đóng gói một ứng dụng thành dạng jar, ta có thể dùng tool jar sẵn có của bộ JDK. Các công việc sẽ từng bước như sau:
Bước 1. Nhớ đặt sẵn tùy biến môi trường PATH đến thư mục %JAVA_HOME%\bin và .; , trong đó %JAVA_HOME% là thư mục cài đặt JDK
Bước2. Tạo file manifest dạng như sau:Manifest-Version: 1.0
Main-Class:tên_lớp_có chứa_hàm_main
Bước 3: thực thi cú pháp trong DOS
Cú pháp: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files …
Trong đó (tuỳ chọn):
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-e specify application entry point for stand-alone application bundled into an executable jar file
-0 store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file
If
any file is a directory then it is processed recursively. The manifest
file name, the archive file name and the entry point name are specified
in the same order as the ‘m’, ‘f’ and ‘e’ flags.
VD 1: đóng gói 2 class riêng biệt thành classes.jar:
jar cvf classes.jar Foo.class Bar.class
Example 2: sử dụng lại
files in the foo/ directory into ‘classes.jar’:
jar cvfm classes.jar mymanifest -C foo/ .
Ví dụ cụ thể:
Giả sử chúng ta có 1 ứng dụng đơn giản gồm 2 lớp MyJFame.java và Running.java , chúng được lưu trong thư mục TyEx. Nội dung của chúng như sau:
Lớp MyJFame.java:
--------------------------------------------------------------------------------
package exjar;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyJFame extends JFrame{
public MyJFame() {
super(”Hello World!”);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,200);
JLabel lbl=new JLabel(”CIAO MONDO!”,JLabel.CENTER);
lbl.setFont(new Font(”Arial”,Font.BOLD,40));
lbl.setForeground(Color.red);
this.add(lbl);
}
}
-------------------------------------------------------
Lớp Running:
----------------------------------------
package exjar;
public class Running {
public static void main(String[] args) {
new MyJFame().setVisible(true);
}
}
-----------------------------------------
Ở đây lớp chứa hàm main là lớp Running.
Biên dịch các tập tin java bằng lệnh sau hoặc copy lệnh sau vào tập tin có tên build.bat rồi thực thi
Trong thư mục
TyEx ta sẽ có thư mục
exjar trong đó chứa 2 tập tin
MyJFame.class và
Running.classTiếp theo ta tạo tập tin manifest có tên
Ty.mf trong thư mục
TyEx với nội dung sau:
Manifest-Version: 1.0 Main-Class: exjar.Running |
Cuối
cùng, ta sẽ đóng gói chúng thành tập tin jar tự chạy (executable jar
file) bằng lệnh sau hoặc bạn cũng có thể tạo tập tin CreateJARFile.bat
trong thư mục TyEx với nội dung sau rồi thực thi nó để tạo jar fiel
jar cvfm MyApp.jar Ty.mf *.java exjar/*.* pause |
Giải thích:
c: (create) tạo file jar mới,
f: chỉ định tên file jar (của chúng ta có tên MyApp.jar,
v: (verbose output) in thông tin đóng gói ra màn hình,
m: chỉ định file manifest.
*.java: đưa tất cả source file ở thư mục hiện tại vào file jar,
exjar/*.*: đưa tất cả các file của thư mục exjar vào jar file.
Thực thi file này ta sẽ được tập tin
MyApp.jar. Thực thi file này bằng cách double-click hoặc enter kết quả như sau:
OK, như vậy bạn đã tạo được 1 file jar tự chạy (executable jar file) được rồi đó.
Chúc thành công!
Nguồn : sưu tầm