บทนำ (Overview)
บทความนี้นำเสนอวิธีการสร้างไฟล์ “.jar” ที่สามารถ “Execute” ได้ โดยใช้โปรแกรม Netbeans สำหรับคนที่ใช้ “Eclipse” สามารถติดตามได้ที่บทความ How to create Executable jar file (.jar)
ขั้นตอน (Steps)
- ติดตั้ง “Server” และสร้าง “Project” (How to create the web application with Netbeans)
- จากนั้นคลิกขวาที่ “Project” แล้วเลือก “Properties”
- ไปที่ “Build > Packaging” แล้วตรวจสอบว่า มีเครื่องหมายถูกหน้า “Build Jar after compiling” หรือไม่ ถ้าไม่มีให้เลือก
- ส่วน Jar file “dist/…” คือส่วนที่จะสร้าง “.jar” ไฟล์เก็บเอาไว้ ถ้าเรียบร้อยแล้วให้คลิก “OK”
- ทดลองเขียนโปรแกรมง่าย ๆ ดังนี้
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package client; //สร้าง Form ด้วย JFrame ต้อง import Class ของ javax.swing.JFrame import javax.swing.JFrame; /** * * @author wsunpachit */ public class Client { /** * @param args the command line arguments */ public static void main(String[] args) { //สร้าง object โดยการ new JFrame JFrame frame = new JFrame("Hello Login");//ชื่อหน้าต่าง frame.setSize(500, 500);//ขนาดหน้าต่าง frame.setLocation(100,100);//ตำแหน่งของหน้าต่าง frame.setVisible(true);//ให้แสดงหน้าต่าง } }
- ทดสอบ “Run” โปรแกรม
- สุดท้ายเราจะสร้าง “.jar” ที่สามารถ “Execute” ได้ โดยคลิกที่ “Clean and Build Project”
- ให้กลับไปดูโฟลเดอร์ “<project>/dist/<.jar>” เราจะพบ “Jar” ไฟล์ลอง “Run” โดย “Double Click”