JAR (Java Archive)
Home  Contents KMArticlesMembersSponsorsAbout us

ปรับปรุง : 2556-06-20 (ปรับ template)
ขอบซ้ายขอบบน
JAR (Java Archive)
jar (Java ARchive) ?
The JavaTM Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file contains the class files and auxiliary resources associated with applets and applications.
Short Sample

DOS>javac x.java
DOS>java x
DOS>dir x.class
DOS>edit manifest.mf
DOS>jar cfm x.jar manifest.mf x.class
DOS>java -jar x.jar


? ควรศึกษาเรื่อง package ควบคู่กันไป
ประโยชน์ของแฟ้ม JAR
- มีความปลอดภัย
- ลดเวลาดาวน์โหลดแฟ้ม
- ลดขนาดแฟ้ม
- รวมเป็นห่อเพื่อขยับขยายภายหลัง
- กำหนดรุ่น และตีตราชัดเจน
- พกพาสะดวก
Java Archieve = ที่เก็บเอกสาร

+ http://www.cs.princeton.edu
+ http://java.sun.com
+ http://www.particle.kth.se
+ http://java.sun.com
1. สร้าง manifest.mf (Manifest คือ บัญชีแสดง)
- Warning :  The text file from which you are creating the manifest must end with a new line or carriage return. 
The last line will not be parsed properly if it does not end with a new line or carriage return.
- When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest 
file in an archive, and it always has the pathname META-INF/MANIFEST.MF
พบ 2 บรรทัดนี้ใน MANIFEST.MF
Manifest-Version: 1.0
Main-Class: x 2. ตัวอย่างการสร้าง .jar
DOS>jar cfm x.jar manifest.mf x.class
DOS>jar cfm x.jar manifest.mf x.class y.class z.class
DOS>jar cfm x.jar manifest.mf v1/*.class 3. ดูแฟ้มใน .jar
DOS>jar tf x.jar
META-INF/
META-INF/MANIFEST.MF
x.class 4. แตกแฟ้ม .jar
DOS>jar xf x.jar 5. ปรับปรุงหรือแทนที่แฟ้มเดิม
DOS>jar uf x.jar x.class 6. ประมวลผล application
DOS>java -jar x.jar 7. ประมวลผล applet ในแฟ้ม x.htm
<applet code=x.class archive=x.jar width=120 height=120>
</applet>
กรณีตัวอย่าง checker01.jar
พบเกมหมากฮอสใน http://www.thaibg.com พัฒนาโดย อ.ทศพล ธนะทิพานนท์

จึงเป็นแนวคิดในการเขียนตาราง เพื่อใช้สอนนักศึกษาใช้ applet และ .jar 
Download : checker01.zip (Only .jar Executed)
Download : checker02.zip (Simulation : test)
DOS>jar cfm checker01.jar manifest.mf checker01.class checker01b.gif checker01w.gif // http://www.particle.kth.se import java.awt.*; import java.applet.*; import java.net.*; // Class URL public class checker01 extends Applet { Image imgb,imgw; URL url; int w = 42; public void init() { setBackground(new Color(192,192,192)); url = this.getClass().getResource("checker01w.gif"); imgw = Toolkit.getDefaultToolkit().getImage(url); url = this.getClass().getResource("checker01b.gif"); imgb = Toolkit.getDefaultToolkit().getImage(url); // imgw = getImage(getCodeBase(),"x.gif"); outside .jar } public void paint(Graphics g) { g.setColor(Color.white); for (int i=0;i<4;i++) for (int j=0;j<4;j++) { g.fillRect(i*w*2,j*w*2,w,w); g.fillRect(i*w*2 + w,j*w*2 + w,w,w); } for (int i=0;i<4;i++) { g.drawImage(imgw,i*w*2 + w,0,this); g.drawImage(imgw,i*w*2,w,this); g.drawImage(imgb,i*w*2 + w,w * 6,this); g.drawImage(imgb,i*w*2,w * 7,this); } } }

ต.ย.ใน appletviewer

checker01
- ทำงานได้ในแฟ้ม .jar แฟ้มเดียว
- วาดตารางด้วย fillRect
- load ภาพมา drawImage checker02
- ยังไม่ทดสอบใน .jar ให้ฝึกสร้างเอง
- แสดงการเดินหมาก ใช้ข้อมูลจากอาร์เรย์
- ถ้าอ่านหมากจากแฟ้มจะยืดหยุ่นกว่านี้
คำสั่งเรียก Applet ใน checker01.htm
<applet code=checker01.class
archive=checker01.jar width=336 height=336>
</applet>


แฟ้ม checker01.jar ทำหน้าที่เก็บทั้ง .class และ image หากเรียก .jar เข้า archive ก็จะเรียนสิ่งที่อยู่ในนั้นได้
"Imagination is more important than knowledge" - Albert Einstein
Home
Thaiabc.com
Thainame.net
Lampang.net
Nation university
PHP
MySQL
Visual basic.NET
TabletPC
Linux
Online quiz
Download
Search engine
Web ranking
Add website
Blog : Education
Blog : ACLA
Blog : Lampang
Facebook.com
Twitter.com
About us
My dream
Site map
Sponsor
http://goo.gl/72BPC