Loading libraries and natives from path

From Slick2D Wiki
Revision as of 11:25, 9 May 2018 by Decerez (Talk | contribs) (Undo revision 561 by Abc123456 (talk))

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction[edit]

Some of you may want to load the libraries and natives needed for slick from a custom path or directory. This is useful when you don't want your users to type in additional parameters for starting your program. Working with additional parameters can - more often than not - bring about errors and/or reports of bugs.

To avoid these problems, you can follow this tutorial.

Filestructure[edit]

In this section you will see the structure which we will use for this example.

<YourDirectory>
   ├── libs
   │   ├── lwjgl.jar
   │   ├── slick.jar
   │   └── natives
   │       ├── libjinput-linux.so
   │       ├── libjinput-linux64.so
   │       ├── liblwjgl.so
   │       ├── liblwjgl64.so
   │       ├── libopenal.so
   │       └── libopenal64.so
   └── YourProgram.jar

For this example the libraries used are the minimum for a Slick programm which are:

  • lwjgl.jar
  • slick.jar


The natives libraries which are used are the natives for linux:

  • libjinput-linux.so
  • libjinput-linux64.so
  • liblwjgl.so
  • liblwjgl64.so
  • libopenal.so
  • libopenal64.so


The native libraries with the 64 at the end of the file-name are those needed for 64 Bit Linux and those without are for 32 Bit systems.

You may also put the natives for Windows (.dll) and Mac (.dylib / .jnilib) to be platform independent.

Adding the code[edit]

It is very important to add the following lines of code at the beginning of your "public static void main(String[] args)"-method because the libraries need to be loaded before you use them!


The first line of code you have to add is the one setting your "java.library.path". This is the path where your libraries are. In our scenario the path of the libraries is "libs". So our code will look like this:

System.setProperty("java.library.path", "libs");

This will tell java to use the relative path to the directory libs for loading the libraries. You may also use absolute paths here.


The second and last line you have to add is the one saying lwjgl where the native libraries are.

//Extracted from Distributing Your LWJGL Application
System.setProperty("org.lwjgl.librarypath", new File("libs/natives").getAbsolutePath());

This line of code tells lwjgl that the native libraries are located in the directory with the relative path "libs/natives". The "File" used here is only for getting the absolute path to our natives directory because the parameter "org.lwjgl.librarypath" does only support absolute paths.

Finished sourcecode[edit]

The following code shows how your source code should look like when you're done with this tutorial:

public static void main(String[] args) {
       System.setProperty("java.library.path", "libs");

       //Extracted from Distributing Your LWJGL Application
       System.setProperty("org.lwjgl.librarypath", new File("libs/natives").getAbsolutePath());

       //Your sourcecode for starting the display
}