
ในบทความนี้จะยกตัวอย่างการเรียกใช้งานไฟล์ .jar บนโปรแกรม Eclipse

ตัวนี้คือ Path ของโปรเจคปัจจุบัน

ในการนำไฟล์ .jar มาใช้ นิยมจดัเก็บไว้ในโฟเดอร์ lib/ ให้เราสร้างโฟเดอร์นี้ไว้ใน Project

จากนั้นให้ทำการ Copy ไฟล์ .jar มาใว้ในโฟเดอร์ (ตัวอย่างนี้เป็นไฟล์ .jar ชื่อว่า opencsv-2.3.jar ไว้สำหรับจัดการไฟล์ CSV)

จากนั้นกลับมาที่โปรเจคบนโปรแกรม Eclipse ให้คลิกขวาที่ Project เลือก Refresh

แสดงโฟเดอร์ไฟล์ lib/opencsv-2.3.jar
การเพิ่มไฟล์ .jar เข้ามาใน Project

คลิกขวาที่ Project เลือก Properties

ในส่วนของ Java Build Path เลือก Add JARs..

เลือกไฟล์ .jar ที่ต้องการ

ไฟล์ .jar ถูก Add เข้ามาใน Libraries เรียบร้อยแล้ว ซึ่งตอนนี้เราสามารถเรียกใช้งาน Class ต่าง ๆ ที่อยู่ใน .jar ได้ทันที

ตัวอย่างการ Import ละการเรียกใช้งาน ซึ่งขึ้นอยู่กับ การใช้งาของแต่ล่ะ Class
ทางเลือกการเรียกใช้งานไฟล์ .jar บน Eclipse

ถ้าง่าย ๆ ก็คลิกขวาที่ Project -> Build Path -> Add External Archives.. หรือ Add Libraries

เลือกไฟล์ .jar ที่ต้องการ

แสดง Package ของไฟล์ .jar ที่เราจะสามารถเรียกใช้งานได้
บทความนี้เป็นเพียงตัวอย่างการ Add ไฟล์ .jar และตัวย่างการเรียกใช้งานบน Project ซึ่งในบทความอื่น ๆ ถัดไป บางตัวจะมีการเรียกใช้งาน Library อยู่หลายตัว อาทิเช่น การติดต่อกับ Database รุ่นต่าง ๆ การสร้างไฟล์ CSV การสร้างไฟล์ Excel , Word ร่วมทั้งพวก JSON หรือ Web Services ก็อาจะต้องหาพวก .jar ที่เป็น Library มาใช้งาน
ไม่มีความคิดเห็น:
แสดงความคิดเห็น