How to create jar file with java command line | วิธีการสร้าง jar ไฟล์ด้วย command line


1 สร้าง java file ขึ้นมาเพื่อทดสอบ
public class Hw {
public static void main(String[] args) {
System.out.println("hello world");
}
}
view raw Hw.java hosted with ❤ by GitHub
2. ใช้คำสั่ง compiled source code ให้ java กลายเป็น byte code ด้วยคำสั่ง
// Create directory bin.
sudo mkdir bin
// Compiled command with javac.
sudo javac -d bin Hw.java
3.หลังจากนั้นไฟล์​ Hw.class  จะถูกสร้างขึ้นใน directoy bin
4.สร้าง file mainifest.mf เพื่อเป็นตัวสร้างไฟล์ jar ขึ้นมา ซึ่งไฟล์นี้อยู่ใน directory เดียวกับไฟล์​ Hw.java ไฟล์ที่เราสร้างขึ้นมาตอนแรก
Manifest-Version: 1.0
Created-By: Samark
Main-Class: Hw
view raw MAINIFEST.MF hosted with ❤ by GitHub
5.เข้าไปที่ directory bin [cd bin] แล้วใช้ำสั่งสร้าง jar file ด้วยคำสั่ง
sudo jar cfm Hw.jar ../MAINIFEST.MF .
//#################################
คำสั่ง ประกอบด้วย
jar [คำสั่งสร้างไฟล์ jar]
cfm [อ๊อฟชั่นในการสร้างไฟล์สามารถอ่านเพิ่มด้วยคำสั่ง jar -help ]
Hw.jar [ชื่อไฟล์ที่ต้องการสร้าง]
../MINIFEST.MF [ที่อยู่ของ mainifest ของเราที่สร้างขึ้นมา]
. [ให้เอาทุกไฟล์ที่เป็นภายใน directory นี้ ]
view raw create-jar-file hosted with ❤ by GitHub
6.วิธีการรัน jar file สามารถทำได้ด้วยคำสั่ง java -jara Hw.jar เป็นต้น
Share on Google Plus

About maxcom

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 ความคิดเห็น:

แสดงความคิดเห็น