Loading a DLL as a Java resource using NetBeans (6.1)

I wanted to package the DLL (dynamic link library) I was using via JNI together with my Java application overall instead of referring to it with a hard-coded, absolute path in my code. This can be accomplished by packaging the DLL as a “resource” of your application. The whole process becomes a bit tricky though, because you need to first restructure your NetBeans project to accommodate “resource files”, and you also have to deal with the fact that “resource” files CANNOT be loaded as local system libraries in Java. Let me repeat that, a DLL simply cannot be loaded from within a packaged JAR distributable, it needs to be loaded from a file on the system. Here is how I was able to accomplish all this:
1.    Create a location to put the DLL within NetBeans project structure
a.    Create a new “resources” folder in the “Files” view within NetBeans
i.    Create a sub-folder within this folder called “images” or “clibs”
1.    Place your DLL files in this directory
b.    Go to project properties within NetBeans, and go to “Sources”. Add the “resources” folder you just created and label it anything you want
c.    Clean and build your project. Make sure that within the “File” view, the JAR distributable within the “dist” directory contains your newly created “resources” folder.
2.    Add code to find the DLL resource within your program, and load the library. Remember, we need to find the DLL resource, copy its contents into a new file located on the local system, and then load that new file as a DLL

More information:
•    http://www.velocityreviews.com/forums/t143553-dll-in-jar.html
•    http://forums.sun.com/thread.jspa?messageID=10550643
•    http://weblogs.java.net/blog/kohsuke/archive/2007/04/how_to_convert.html


6 thoughts on “Loading a DLL as a Java resource using NetBeans (6.1)

  1. What is the “files” view within NetBeans? HOw does the code find the dll resource within your program? None of the “More information” pages are still available. I’ve been working the problem for 3 days. Can’t get Netbeans to find a dll.

  2. Having read this I thought it was rather informative. I appreciate you finding the time and energy to put
    this short article together. I once again find myself
    personally spending way too much time both reading and posting comments.
    But so what, it was still worth it!

  3. This is the right site for anyone who wishes to understand this topic.
    You know so much its almost tough to argue with you (not that I really will need to…HaHa).
    You certainly put a new spin on a topic that has been written about for many years.

    Excellent stuff, just great!

  4. An outstanding share! I’ve just forwarded this onto a friend who was doing a little homework
    on this. And he actually bought me dinner due to the fact that I found it for him…
    lol. So allow me to reword this…. Thanks
    for the meal!! But yeah, thanx for spending
    some time to talk about this matter here on your

  5. From this the sufferer might solely be roused by music, which excited an overwhelming want to bounce until the performer fell to the ground bathed in profuse perspiration, when the treatment, in any respect events for the time, was alleged to be effected. Furthermore., it’s the indirect technique of supplying water to just about each town and village in Seistan Proper, feeding because it does a group of minor canals, by which a system of profuse irrigation is pu t t in pressure.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s