1

Recently i had a task to read and update a specific cell in a csv for that i used chilkat csv libs. But what i dont understand is why we need a static block to load the chilkat library even after adding the jar file to Reference library in Eclipse project.

The below is the static block code used

static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
        System.err.println("Native code library failed to load.\n" + e);
        System.exit(1);
    }
}
Patil Prashanth
  • 1,901
  • 2
  • 13
  • 7

1 Answers1

0

If you want to do load the library when this particular class is initialized, then you use static blocks. This prevents lazy loading of the library (when it is first accessed). Loading it early (i.e, in a static block) increases program responisveness during runtime.

Note : Adding the jar file to Reference library doesn't loads the library lazily i.e, the dependencies will be loaded as and when needed after they are resolved during build time.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Is there any way this can be achieved without using the static block. Because when i remove this static block its throws the below error Exception in thread "main" java.lang.UnsatisfiedLinkError: com.chilkatsoft.chilkatJNI.new_CkCsv()J at com.chilkatsoft.chilkatJNI.new_CkCsv(Native Method) at com.chilkatsoft.CkCsv.(CkCsv.java:39) at CsvOperations.GetIMEI(CsvOperations.java:22) at CsvOperations.main(CsvOperations.java:17) – Patil Prashanth May 07 '15 at 07:06
  • @PatilPrashanth - You might want to google `UnsatisfiedLinkErrors` and see for yourself. Native libraries should be found and linked in your JVM – TheLostMind May 07 '15 at 07:16