Skip to main content

Load Java class dengan URLClassLoader

Pernah berpikir untuk memanggil class yang tidak ada didalam classpath aplikasi yang kita buat? Atau mungkin ingin melakukan inisialisasi driver JDBC secara runtime? Mungkin bagi pengguna awal, URLClassLoader adalah class yang asing.
Sebenarnya, mekanisme ClassLoader merupakan mekanisme standar dalam java, namun umumnya ClassLoader berjalan ketika aplikasi akan di-compile atau dijalankan dengan cara membaca ClassPath baik melalui parameter -cp atau -classpath atau melalui MANIFEST.MF didalam jar atau melalui system environment variable CLASSPATH.
Untuk ClassLoader model ini, saya rasa tidak ada pertanyaan atau mungkin bukan menjadi fokus pembahasan pada post kali ini. Pembahasan pada post kali ini lebih menekankan bagaimana cara memindai (Load) suatu java class yang tidak terdapat pada classpath pada saat runtime.....
Penggunaan class URLClassLoader adalah solusinya. Class ini dapat memindai class-class yang berada pada alamat URL tertentu dan bahkan dalam bentuk JAR...
Berikut ini adalah contoh penggunaan URLClassLoader:
try{
URLClassLoader cl=new URLClassLoader(new URL[]{});
cl.addURL(new File("mysql-jdbc-driver.jar").toURI().toURL());
}catch(Exception e){}
Selanjutnya pembuatan instance dari class yang ada berdasarkan nama class yang diinginkan. Berikut caranya
try{
cl.loadClass("com.jdbc.mysql.Driver").newInstance();
}catch(Exception e){}
Selamat mencoba dan berkarya...

Comments

Popular posts from this blog

The Future Of Computing is Ubuntu Phone

As we are know, in the beginning of January 2013. Canonical announce their new project called Ubuntu Phone. From the beginning Ubuntu was just another Linux distro,  but now Canonical driving Ubuntu far beyond its beginnings as just another Linux distro into an Operating System that works on  television and even being  an android and iPhone competitor, Ubuntu is skating to where they puck is going to be. But, which such a bold move, there are significant obstacles to overcome. The soon to be released Ubuntu Phone is paving the way that all smart phones will eventually go. Back to the late  1943 where  computers were gigantic, filling rooms and requiring constant care and maintenance. Over time, the components required to build the computers become smaller and cheaper, till eventually it was possible to put one on your desk until the laptop computer, a smaller, more portable, but just as powerful machine was made and make it nearly obsolete. And then, com...

JSP/Servlet File Upload....

Wah dah lama juga ga posting... jadi gue sempet mikir kira-kira enaknya posting apa ya? nah kebetulan beberapa waktu yang lampau gue pernah dapet assignment dari kantor untuk buat aplikasi yang mengupload file zip lewat JSP/Servlet trus waktu selesai di upload file tadi langsung otomatis diextract... nah... pada posting kali ini gue pengen ngebahas gimana caranya buat Script upload file di JSP/Servlet... karena umumnya penanganan request yang bersifat multi-part agak berbeda pada server j2ee container dibandingkan dengan bahasa pemograman lainnya (eg. PHP). bagi yang belum ngerti atau ngga' bisa java tapi pingin belajar silahkan comment disini, nanti gue buat post tentang "java tutorial" berikut adalah beberapa API yang harus dimiliki terlebih dahulu. download API commons-fileupload disini download API commons-io disini setelah file telah didownload kita lanjutkan dengan coding... pertama kali yang harus dilakukan adalah memasukkan API library diatas kedalam class...

Ubuntu 9.04 x86_64 Run well in Acer Aspire 4530

Hey guys, after a while I didn't update this blog, right now I'm just feeling like to share my happiness. Just want to inform you all that ubuntu 9.04 64bit run well in my laptop... i'm just happy to say to my self welcome back to ubuntu after a while back to windows because ubuntu version lesser than 9.04 doesn't support my laptop. the installation process is simple, much more simple than before. to install ubuntu 9.04 please follow this steps: insert ubutu 9.04 live CD or the alternate version of it set your optical drive as your first boot device at BIOS or press F12 at boot sequence to select which device you want to boot from. select install ubuntu to harddrive wait until boot process done and setup your storage partition (at least 1 ext3/ext4 partition and 1 swap partition fill your name, user name and password. wait until installation progress finished. reboot you laptop now. VIOLA! your ubuntu now ready to use okay let's check so...