Saturday, September 1, 2012

About Class.newInstance()

Today, a little story about a problem that I encountered recently, problem related to java reflection.

The problem is the following : I had a couple of Java classes, that happen to be Java beans and that I wanted to manipulate by reflection.
Actually, it's not me but a third part library, typically JPA related, that manipulates my objects and especially construct them by reflection using :

SomeObject obj = SomeObject.class.newInstance();

Fair enough, expect that to do this I must ensure that all my objects have a public constructor with no argument. And it's something that I don't have in my case, my objects implement the factory design pattern, and the constructors of my objects are private or protected, and thus failed the previous code.

So I thought, let's just use reflection to change the visibility of the constructor by a well known:

SomeObject.class.getConstructor().setAccessible(true);

The problem is that, this method works if I instantiate the object from the constructor that I just set accessible, but it will not make the constructor visible by my library when it will call newInstance() method on the class.
Why ? For a simple reason, the setAccessible method does not actually change the modifier of the constructor, but just set some override attribute in the Constructor object, and the newInstance()  check this boolean and does not throw an Exception if set.

But in the case of the of a newInstance() called on the Class object, the private method privateGetDeclaredConstructors(boolean publicOnly) is called, and this method does not rely on the Constructor object itself, but either ask the JVM by reading the byte code of the class, or uses the publicConstructors object that caches the public constructors of this class.
So if I add my modified constructor to the publicConstructors, let's say by reflection, it will do the trick. But the problem is that the publicConstructors is a SoftReference to an array of constructors. So if I do so, the change might disappear if my program run out memory and my modified constructor, softly referenced, is cleaned up.

So the only choice that remains is to change the byte code itself.
For that purpose, I will simply use the famous javassist library.

CtClass someClass= ClassPool.getDefault().get("sandbox.SomeObject");
someClass.getDeclaredConstructor(null).setModifiers(Modifier.PUBLIC);
someClass.writeFile();
someClass.toClass();

Just a couple of things I need to be careful of, I mustn't load the SomeObject class before changing the bytecode (at least on the ClassLoader that I will use to override this class, in my case the SystemClassLoader).
So no SomeObject.class.getCanonicalName() to get the name of the class, if I don't follow this rule, the someClass.toClass() method, that forces the (in this case System) class loader to load the class will throw a

java.lang.LinkageError: loader (instance of  sun/misc/Launcher$AppClassLoader): attempted  duplicate class definition for name: "sandbox/SomeObject"

And we are done, the complete (and simple) code :

My dummy object :


package sandbox;

public class SomeObject {
    // private constructor
    private SomeObject() {    }
}

My main class :

package sandbox;

import java.lang.reflect.Modifier;
import javassist.ClassPool;
import javassist.CtClass;

public class Test {
 
    public static void main(String[] args) {
        try {
            // don't use SomeObject.class.getCanonicalName()
            CtClass classToEdit = ClassPool.getDefault().get("sandbox.SomeObject");
            classToEdit.getDeclaredConstructor(null).setModifiers(Modifier.PUBLIC);
            classToEdit.writeFile();
            classToEdit.toClass();

            // some stuffs
            ...
 
             // instantiate the object using the modified constructor
            SomeObject obj = SomeObject.class.newInstance();
            System.out.println(obj3);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Java Collections Performance

Performances of data structures, especially collections is a recurrent subject when coding.

If you have never heard about such a topic, here is the chance, otherwise it's the hundredth time you see such title, and you are probably thinking "Another article about this topic, I'll probably not learn anything new, but anyway I'm bored so I'm gonna read it ...". And you are probably 90% right, nothing really new here, but I promise you a couple of colorful and beautiful charts that we don't have the opportunity to see everyday (and the ability to create your own) .

The first time I started wondering about collection performances was when I started working with some > 100 000 elements collections. At that time, I heard some bad jokes such as "I just understood why the Java logo is a cup of coffee, because Java collections are so slow that when manipulating them, you have the time to go and grab some coffee before they do the job ... Just kidding' !".

At that time, I wanting to use am implementation of a java.util.List that would have good performances on all the common methods provided by the interface (let's say get(index), add(Object), remove(Object), remove(index), contains(Object), iterator(), add other methods that you like), without wondering about memory usage (I mean, even this List would take 4 times the size of a LinkedList it wouldn't be a big deal).

In other words, some List that would not be instantiated a million times in my application, but a couple of times, and each instance will have great performances. For example, the model of a GUI Table, or some other GUI component, which data will evolve frequently, usually in batch of operation (the user add 100 000 elements, remove 50 000, etc.) , and which performances will sometimes be critical.

First of all, what do I mean by good performances ? Usually good performances are equivalent to an average complexity of O(1). But this one is impossible to get all the time, so at first let's study current JDK List/Collection implementations and see what we have.

Well, we already know so basic information about Collection methods complexity, such as the fact that a HashSet has an average O(1) complexity for its contains(o) method, or that ArrayList is also in O(1) for its method get(int index), etc.
For more details, check this post of Lalit Pant which did a nice summary on this subject.

Let's do some basic benchmark on some collections to see concreatly what they are capable of ...

For that purpose, I wrote this very simple benchmark code, that is not very elegant I have to admit, but do the trick. Here is the core of the benchmark :

private void execute(BenchRunnable run, int loop, String taskName) {
    System.out.print(taskName + " ... ");
    // set default context
    collection.clear();
    collection.addAll(defaultCtx);
    // warmup
    warmUp();
    isTimeout = false;
    // timeout timer
    Timer timer = new Timer((int) timeout, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            isTimeout = true;
            // to raise a ConcurrentModificationException or a
            // NoSuchElementException to interrupt internal work in the List
            collection.clear();
        }
    });
    timer.setRepeats(false);
    timer.start();
    long startTime = System.nanoTime();
    int i;
    for (i = 0; i < loop && !isTimeout; i++) {
        try {
            run.run(i);
        } catch (Exception e) {
            // on purpose so ignore it
        }
    }
    timer.stop();
    long time = isTimeout ? timeout * 1000000 : System.nanoTime() - startTime;
    System.out.println((isTimeout ? "Timeout (>" + time + "ns) after " + i + " loop(s)" : time + "ns"));
    // restore default context
    // the collection instance might have been corrupted by the timeout,
    // create a new instance
    try {
        Constructor<? extends Collection> constructor = collection.getClass().getDeclaredConstructor(
                    (Class<?>[]) null);
            constructor.setAccessible(true);
            collection = constructor.newInstance();
        collection = collection.getClass().newInstance();
        // update the reference
        if (collection instanceof List) {
            list = (List<String>) collection;
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    // gc to clean all the stuff
    System.gc();
}

where the BenchRunnable is just like a Runnable, but which can use the current loop index :

private interface BenchRunnable {

    public void run(int loopIndex);

}

and the warmUp() method will manipulate a little the collection, to be sure that the internal structure is allocated.
A timer is used to timeout too long method benchmark, indeed my purpose is not to have a complete benchmark, but just a global idea of what implementations of Collection are efficient for a given method.

For example, I will call this method this way :

int nb = 1000000;

final List<String> list = new LinkedList<String>();

execute(new BenchRunnable() {

    @Override

    public void run(int i) {

        list.add(Integer.toString(i % 100));

    }

}, nb, "add " + nb + " distinct elements : ");

I will finally display the results in a JFreeChart component, because the console output is not very friendly to compare results.
The complete source of the Benchmark class can be found here, feel free to play with it ! (To those who will think "You should have used a proper tool to do this stuff", I would answer "Yes, you're totally right, but the fact is that when I started it, I didn't mean it to be so big in the end. Anyway, I like the press-button execution ...").

I first launched it on a couple of famous lists, from the JDK, from Javolution, from Apache Commons, I also added a HashSet, just for the records. Here is what I got (the colorful charts that I promised you) (Note that every bench is done with an instance of the given collection populated with 100 000 elements) :


We can see some interesting things here, we confirm what we already knew about some collections, the CopyOnWriteArray is significantly slow on data modification, etc, we also can see, between ArrayList and LinkedList some significant differences (OK, the benchmark does not cover all the different cases, and maybe we are in the particular case were ArrayList is better than LinkedList, anyway it gives us an interesting overall).

The point here is to talk about List and especially fast implementations, but just for the records, I post the results that I got for some Sets :


  • A CombinedList
So, after tuning a little bit the benchmark, studying my charts, I thought "How about creating my humble but own implementation of List ?" with a simple idea, not by implementing some cute-edge algorithm, but just by combining existing implementations. (If you're not very enthusiastic about this idea, you can go directly to the Memory usage of Collections section below).

By this I mean a CombinedList which will use internally a HashSet, an ArrayList and a TreeList, and in each of its method, will use the data structure the most efficient to do the job, and so my CombinedList will be almost as fast as the fastest collection for all of its methods.

By almost I mean their will be a little delay involved by the fact that we will sometimes need to synchronize the data in the internal collections. But it's not a big deal, because the clear() and addAll(collection) methods are quite fast, as we saw in our first charts, and they are definitely faster than some other O(n) or O(n x n) operations on some collections. So it's faster to recreate from scratch the all collection than applying a remove(Object) on an ArrayList (once again, here I don't care about Memory and GC).

So here we go, here are the attributes of my CombinedList

/** HashSet */
private HashSet<E> hashSet;

/** ArrayList */
private ArrayList<E> arrayList;

/** LinkedList */
private Tree linkedList;

/** If hashSet is up-to-date */
private boolean isHashSetUpToDate = true;

/** If arrayList is up-to-date */
private boolean isArrayListUpToDate = true;

/** If linkedList is up-to-date */
private boolean isLinkedListUpToDate = true;

After that, in each method I will use the fastest collection, for example, the contains(o) has an average O(1) in the HashSet implemetation, so :

/**
 * @see java.util.Collection#contains(java.lang.Object)
 */
@Override
public boolean contains(Object o) {
    // most efficient is HashSet
    updateHashSetIfOutOfDate();
    return hashSet.contains(o);
}

where the updateHashSetIfOutOfDate() will be coded like this :

/**
 * Update the hashSet if out of date
 */
private void updateHashSetIfOutOfDate() {
    // one of the two collection arrayList or linkedList is necessarily up-to-date
    if (!isHashSetUpToDate) {
        hashSet.clear();
        if (isArrayListUpToDate) {
            hashSet.addAll(arrayList);
        } else {
            hashSet.addAll(linkedList);
        }
        isHashSetUpToDate = true;
    }
}

Concerning data modification, we will do a similar trick :

/**
 * @see java.util.List#set(int, java.lang.Object)
 */
@Override
public E set(int index, E element) {
    modCount++;
    // most efficient is ArrayList
    updateArrayListIfOutOfDate();
    E old = arrayList.set(index, element);
    setOtherThanArrayListOutOfDate();
    return old;
}

where setOtherThanArrayListOutOfDate() will look like :

private void setOtherThanArrayListOutOfDate() {
    isHashSetUpToDate = false;
    isLinkedListUpToDate = false;
}

We'll extend <strong>java.util.AbstractList</strong>, and don't forget to increment modCount when changing internal data, so that the ConcurrentModificationException mechanism is inherited from it.

Finally, we'll do some custom optimization, on the retainAll(Collection) as mentioned on this Java bug.

And we are done, the complete source of the CombinedList class can be found here.

And here are the benchmark results :


We are quite good on contains(Object) and containsAll(Collection) thanks to the HashSet, and we should statistically be a better than ArrayList on remove(Object) and removeAll(Collection) thanks to LinkedList. We could eventually replace the LinkedList by a TreeList to be better on the remove (TreeList is in O(log n) for the remove). We could also parametrized the use of an internal collection with the size of the data, or the index of the data regarding the size of the list etc.

  • Memory usage of Collections
Ok, I said that I didn't care about memory, but let's take a look at what is the size of this CombinedList, and at the same time the size of other collections.
For that purpose, and as I still don't want to use Yourkit profiler today (which is an excellent tool by the way), I just added some simple method to measure our collection size :


gc();
long usedMemory = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
Constructor<? extends Collection> constructor = clazz.getDeclaredConstructor((Class<?>[]) null);
constructor.setAccessible(true);
// do the test on 100 objects, to be more accurate
for (int i = 0; i < 100; i++) {
    this.collection = (Collection<String>) constructor.newInstance();
    // polulate
    collection.addAll(defaultCtx);
    // do some operation to be sure that the inernal structure is allocated
    collection.remove(0);
    if (collection instanceof List) {
        ((List) collection).indexOf(((List) collection).get(collection.size() - 1));
    }
    collection.iterator();
    collection.toArray();
}
gc();

// measure size
long objectSize = (long) ((ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() - usedMemory) / 100f);
System.out.println(clazz.getCanonicalName() + " Object size : " + objectSize + " bytes");
collection.clear();
collection = null;

And display the results in the same JFreeChart component :



Ok, we have what we were expecting, that is to say our CombinedList size a LinkedList + an ArrayList + a HashSet.
Also, this means that this List will generate a lot of GC, and thus make it not suitable for massive random operations. But for my first purpose of a model for my JTable, it's a pretty good deal !





  • And what about JIT compilation, Heap allocation impact on the benchmarck ?
  • Just a final remark concerning the benchmark. Does the fact that all the benchmarks are done in the some JVM impact the results ? The answer (correct me if I'm wrong) is that, if I executed just one time each method, and if the methods were very fast, the JIT compilation time could have an impact on the performances measured on the first Collections tested. That's the reason why in my benchmark, I first test the CombinedList, to be sure to have the worst case performances.
    But the fact is that we execute several (thousand of ) times the same method, so I consider the JIT compilation time is negligible.
    Also, what about Heap allocation and status and its impact on the test ? For this purpose, I carefully free all objects at the end of each collection benchmark, and call a heavyGc() to be sure to minimize this matter.


    private void heavyGc() { 
      try { 
        System.gc(); 
        Thread.sleep(200); 
        System.runFinalization(); 
        Thread.sleep(200); 
        System.gc(); 
        Thread.sleep(200); 
        System.runFinalization(); 
        Thread.sleep(1000); 
        System.gc(); 
        Thread.sleep(200); 
        System.runFinalization(); 
        Thread.sleep(200); 
        System.gc(); 
      } catch (InterruptedException ex) { 
        ex.printStackTrace(); 
      } 
    }
    

    Tuesday, August 28, 2012

    JSON GSON

    JSON is a wide spread data description format used to exchange JavaScript Objects.
    It is pretty widely used for web API.
    Today, I will focus on the exploitation of this kind of data in Java, with the pretty well done GSon library.
    First, call the API (a simple GET with the arguments in the URL) that provide the data, this is done simply by this code :

    public static String call(String url) throws IOException {
        BufferedReader bis = null;
        InputStream is = null;
        try {
            URLConnection connection = new URL(url).openConnection();
            is = connection.getInputStream();
            // warning of UTF-8 data
            bis = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = null;
            StringBuffer result = new StringBuffer();
            while ((line = bis.readLine()) != null) {
                result.append(line);
            }
            return result.toString();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    Note : we use a InputStreamReader(is, "UTF-8") in order to manage correctly UTF-8 data that we'll retrieve in some Japanese, French or other exotic languages.

    Ok, the String result will be the something, for example like that :

    {
         "first-name": "John",
         "last-name": "Smith",
         "age": 25,
         "address":
         {
             "street-address": "21 2nd Street",
             "city": "New York",
             "state": "NY",
             "postal-code": "10021"
         },
         "phone-number":
         [
             {
               "type": "home",
               "number": "212 555-1234"
             },
             {
               "type": "fax",
               "number": "646 555-4567"
             }
         ]
     }

    To process this String, we'll use the magic GSon.

    The GSon library will try to parse the JSon formatted string and map the data to a given Java Person Object. GSon used reflection to do such a trick, so the attributes types and names of the target java Object will need to be matchable with the JSon string structure.
    In the previous example, the Person object will need to have a structure that match the JSon object, that is to say, will have 5 attributes named "first-name", "last-name", "age", "address", and "phone-number".

    First of all, what we see here, is that an attribute cannot be called first-name in Java, because the '-' character is not permitted in java attribute names. No problem, we'll declare a firstName attribute, and specify that it's JSon serialized name id "first-name" by using a @SerializedName annotation.

    We'll obtain an attribute declared like this :

    @SerializedName("first-name") private String firstName;
    

    Of course same trick is applied for the "last-name" attribute.
    The age is simply declared as a int, here we keep the same attribute name in JavaScript and in Java :

    private int age;
    

    Ok, now the address, which is a complex Object (we can see it with the {...} that represents a structure.
    So we'll map it with a Java Object Address, which following the same principle as above, which give us :

    private class Address {
    
        @SerializedName("street-address") private String streetAddress;
    
        private String city;
    
        private String state;
    
        @SerializedName("postal-code") private int postalCode;
    
    }
    

    Remain the phone numbers, here the [...] characters point out that we are dealing with a collections of objects.
    We'll then map it with a java.util.List of PhoneNumber Objects as below :

    @SerializedName("phone-number") private List&lt;PhoneNumber&gt; phoneNumber;
    

    Where a PhoneNumber is the given object :

    private class PhoneNumber {
    
        private String type;
        private String number;
    }
    

    OK, so now our Object Person is completed and looks like that :

    private class Person {
    
        @SerializedName("first-name") private String firstName;
    
        @SerializedName("last-name") private String lastName;
    
        private int age;
    
        private Address address;
    
        @SerializedName("phone-number") private List&lt;PhoneNumber&gt; phoneNumber;
    
    }
    

    We are done. A call to Gson will instantiate a Person Java Object and populate it with the flux.

    Person data = new Gson().fromJson(json, Person .class); 

     That give us :



    Tuesday, August 14, 2012

    Programmatically restart a Java application

    Today I'll talk about a famous problem : restarting a Java application. It is especially useful when changing the language of a GUI application, so that we need to restart it to reload the internationalized messages in the new language. Some look and feel also require to relaunch the application to be properly applied.

    A quick Google search give plenty answers using a simple :

    Runtime.getRuntime().exec("java -jar myApp.jar");
    System.exit(0);
    

    This indeed basically works, but this answer that does not convince me for several reasons :
    1) What about VM and program arguments ? (this one is secondary in fact, because can be solve it quite easily).
    2) What if the main is not a jar (which is usually the case when launching from an IDE) ?
    3) Most of all, what about the cleaning of the closing application ? For example if the application save some properties when closing, commit some stuffs etc.
    4) We need to change the command line in the code source every time we change a parameter, the name of the jar, etc.

    Overall, something that works fine for some test, sandbox use, but not a generic and elegant way in my humble opinion.

    Ok, so my purpose here is to implement a method :

    public static void restartApplication(Runnable runBeforeRestart) throws IOException {
    
    ...
    
    }
    

    that could be wrapped in some jar library, and could be called, without any code modification, by any Java program, and by solving the 4 points raised previously.

    Let's start by looking at each point and find a way to answer them in an elegant way (let's say the most elegant way that I found).

    1) How to get the program and VM arguments ? Pretty simple, by calling a :

    ManagementFactory.getRuntimeMXBean().getInputArguments();
    

    Concerning the program arguments, the Java property sun.java.command we'll give us both the main class (or jar) and the program arguments, and both will be useful.

    String[] mainCommand = System.getProperty("sun.java.command").split(" ");
    

    2) First retrieve the java bin executable given by the java.home property :

    String java = System.getProperty("java.home") + "/bin/java";
    

    The simple case is when the application is launched from a jar. The jar name is given by a mainCommand[0], and it is in the current path, so we just have to append the application parameters mainCommand[1..n] with a -jar to get the command to execute :

    String cmd = java + vmArgsOneLine + "-jar " + new File(mainCommand[0]).getPath() + mainCommand[1..n];
    

    We'll suppose here that the Manifest of the jar is well done, and we don't need to specify either the main nor the classpath.

    Second case : when the application is launched from a class. In this case, we'll specify the class path and the main class :

    String cmd = java + vmArgsOneLine + "-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0] + mainCommand[1..n];
    


    3) Third point, cleaning the old application before launching the new one.
    To do such a trick, we'll just execute the Runtime.getRuntime().exec(cmd) in a shutdown hook.
    This way, we'll be sure that everything will be properly clean up before creating the new application instance.

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            ...
        }
    });
    

    Run the runBeforeRestart that contains some custom code that we want to be executed before restarting the application :

    if(beforeRestart != null) {
        beforeRestart.run();
    }
    

    And finally, call the
    System.exit(0);
    

    And we're done. Here is our generic method :

    /**
     * Restart the current Java application
     * @param runBeforeRestart some custom code to be run before restarting
     * @throws IOException
     */
    public static void restartApplication(Runnable runBeforeRestart) throws IOException {
        try {
            // java binary
            String java = System.getProperty("java.home") + "/bin/java";
            // vm arguments
            List&lt;String&gt; vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
            StringBuffer vmArgsOneLine = new StringBuffer();
            for (String arg : vmArguments) {
                // if it's the agent argument : we ignore it otherwise the
                // address of the old application and the new one will be in conflict
                if (!arg.contains("-agentlib")) {
                    vmArgsOneLine.append(arg);
                    vmArgsOneLine.append(" ");
                }
            }
            // init the command to execute, add the vm args
            final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine);
            // program main and program arguments (be careful a sun property. might not be supported by all JVM) 
            String[] mainCommand = System.getProperty("sun.java.command").split(" ");
            // program main is a jar
            if (mainCommand[0].endsWith(".jar")) {
                // if it's a jar, add -jar mainJar
                cmd.append("-jar " + new File(mainCommand[0]).getPath());
            } else {
                // else it's a .class, add the classpath and mainClass
                cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
            }
            // finally add program arguments
            for (int i = 1; i < mainCommand.length; i++) {
                cmd.append(" ");
                cmd.append(mainCommand[i]);
            }
            // execute the command in a shutdown hook, to be sure that all the
            // resources have been disposed before restarting the application
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    try {
                        Runtime.getRuntime().exec(cmd.toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            // execute some custom code before restarting
            if (beforeRestart != null) {
                beforeRestart.run();
            }
            // exit
            System.exit(0);
        } catch (Exception e) {
            // something went wrong
            throw new IOException("Error while trying to restart the application", e);
        }
    }
    

    Sunday, August 12, 2012

    UnsatisfiedLinkError in a JavaWebStart application

    Yes ! Me too ! I have spent a tremendous amount of time dealing with trouble using JNI in a Java Web Start or an Applet environment. And by searching on my friend Google, I have found out that I'm not the only Java developer on the Java world to have dealt with this kind of trouble ...
    Sometimes, it works pretty fine, especially if you use Java Web Start to wrap your application (Swing/ Eclipse RCP or Applet), you just have to follow a quite simple procedure that can be automated with a simple Ant script (for more details about Java Web Start, check the Lopica web site that is pretty well done) :

    1) Jar the native lib :
     <jar destfile="lib/nativewin.jar" basedir="native/win/" includes="some.dll"/>  
    

    2) Sign the Jars your application :
     <target name="genkey">  
       <delete file="${keystore}"/>  
       <genkey alias="alias" keystore="${keystore}" storepass="storepass" validity="900">  
         <dname>  
           <param name="CN" value="CN"/>  
           <param name="OU" value="OU"/>  
           <param name="O" value="O"/>  
           <param name="C" value="C"/>  
         </dname>  
       </genkey>  
     </target>  
     <target name="signjar" depends="genkey">  
       <signjar alias="alias" keystore="${keystore}" storepass="storepass">  
         <fileset dir="${dest.dir}/">  
           <include name="**/*.jar" />  
         </fileset>  
       </signjar>  
     </target> 
    

    3) Add the good tag in your JNLP :
     <resources os="Windows" arch="x86">  
          <nativelib href="lib/nativewin.jar"/>  
     </resource> 
    
    And everything is fine. Yes sometimes everything is OK, but sometimes, for some dark reasons that I don't really understand, with some native libs and not with others, especially when you don't have time to spend on this kind of problem, and always on the production environment, because when testing on your development environment, you have forgotten to delete the native lib of your PATH ... So sometimes, the application crashes with a famous :

    java.lang.UnsatisfiedLinkError: no foobar in java.library.path

    Here we are. You check your JNLP : seems good. You check the jar that contains the native lib : hmm OK. It's 10pm and you're still at office, "It must be some little silly thing that I'm forgetting. Tomorrow, I'll recheck that I'll probably found the solution in 10 minutes".
    Naive of you ...

    The following day, you spend an entire morning checking all these little things, reboot 5 times your computer, but it's still here : the UnsatisfiedLinkErrorOfTheDeath ...

    The first workaround is of course to copy the native libs in a folder that you will previously had in your PATH. The copy can be done by some installer code of the program, for example by a code that will look like this one (the native is located in the Jar, so we need to access it as a Stream) :

    InputStream src = SomeClass.class.getResourceAsStream("/pathToTheLib/" + libName);
    
    File libDestFile = new File(installLibFilePath);
    
    FileOutputStream out = null;
    
    try {
           out = new FileOutputStream(libDestFile);
           byte[] buffer = new byte[256];
           int read = 0;
           while ((read = src.read(buffer)) > 0) {
                  out.write(buffer, 0, read);
                  read = src.read(buffer);
           }
    } catch (FileNotFoundException e) {
           e.printStackTrace();
    } catch (IOException e) {
           e.printStackTrace();
    } finally {
           // close
           try {
                  if (out != null) {
                         out.close();
                  }
           } catch (IOException e) {
                  e.printStackTrace();
           }
    }
    

    Here we have two possibilities : either our program is loading the native lib, in this case, we replace the instruction :
    java.lang.System.load(libDestFile.getAbsolutePath());
    
    By a :
    java.lang.System.load(libName);
    
    But if it is a library (another Jar) that is loading the native lib, we are stuck. In this case, we have to set some PATH env property manually so that the ClassLoader find the native lib, and your Java developer soul cry hard when thinking of doing such a disgraceful thing.
    Ok, so how do we do such a trick in Java ... Well, as it is said i the exception message, we need to add the library to the java.library.path property.
    What is this property ? It is used when the java.lang.ClassLoader will load the first native library to initialize its field usr_paths which is a String array. Indeed, in the loadLibrary method, we can see this code :
    if (sys_paths == null) {
           usr_paths = initializePath("java.library.path");
           sys_paths = initializePath("sun.boot.library.path");
    }
    
    So seeing this code, we understand that it is useless to override this property, by adding the folder where we will copy our library, after the JVM as been launch, because we'll be too late, the sys_paths field will be initialized, so the usr_paths will not be updated.
    And of course, this field cannot be accessed directly, so our last resort is to use reflection :
    Field usrPathFiled = ClassLoader.class.getDeclaredField("usr_paths");
    usrPathFiled.setAccessible(true);
    String[] usrPath = (String[]) usrPathFiled.get(null);
    String[] newUsrPath = new String[usrPath.length + 1];
    System.arraycopy(usrPath, 0, newUsrPath, 0, usrPath.length);
    newUsrPath[usrPath.length] = destFile.getParentFile().getAbsolutePath();
    usrPathFiled.set(null, newUsrPath);
    

    By executing this code before using the jar that will load the native library, we have a programmatic 100% Java workaround to our problem.

    It's definitely not the most beautiful way of coding in Java, some of you might say "If it's private in the JDK, there is a good reason, Son !"
    Anyway, shall this article avoid other people to waste their time on this problem.