Java exe library path

Java.library.path setting programmatically

When messing around with JNI, one has to set the java.library.path accordingly. Unfortunately the only way is to add a system property before the application is started:

java -Djava.library.path=/path/to/libs 

Changing the system property later doesn’t have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java…

System.setProperty( "java.library.path", "/path/to/libs" ); Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" ); fieldSysPath.setAccessible( true ); fieldSysPath.set( null, null ); 

Explanation

At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically.

The Classloader has a static field ( sys_paths ) that contains the paths. If that field is set to null, it is initialized automatically. Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary() is called…

+1 — Very cool hack — I will try this as the last resort [this is very cool, but horrible for maintainability!].

Please quote the relevant parts of the article in your answer as links can go dead (which yours has).

Читайте также:  Php как создать константу

You should be really careful with this, not only is it implementation based, but it might also cause unexpected exceptions when a concurrent thread tries to load a native library and has just passed the null check for this field when you set it to null

@GuyTal did you mean your native library(.so or .dll) wrap in jar? then you will need to extract it somewhere outside, on file system first, see stackoverflow.com/questions/20389255/…

No you can’t. This property is a read only value. You can change it at JVM launchin time with:

-Djava.library.path=your_path 

If you want to load a library from a specific location, you can use System.load(libraryPath) instead with the full path to the library.

I wanted to accept both the answers, but as I can accept only one I will go with this,as the cool hack relies on implementation details.

I’m just quoting from the link provided by secmask (https://cedarsoft.com/blog.html), in case the link goes dead:

Changing the system property java.library.path later doesn’t have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java.

System.setProperty("java.library.path", "/path/to/libs"); Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible(true); fieldSysPath.set(null, null); 

Explanation:

At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically. The Classloader has a static field ( sys_paths ) that contains the paths. If that field is set to null , it is initialized automatically.Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary() is called.

Источник

Rob Bamforth’s Blog

Java, Objective-c, iOS, WATCHOS, SQL, T-SQL, MySQL, SQL Server, DBA, PHP, ODBC, API, Telecoms

JAVA – How To Bundle A Java App As An Windows Executable EXE (.jar to .exe)

This question comes up time and time again and there are two programs that I frequently use to build my compiled Java apps into a Windows Executable files. This, of course, limits the environments in which your app can run to Windows only devices, so if you plan to release a ‘Windows only’ version then this could be the solution for you.

It’s worth noting that it’s also possible to bundle a jar app into a Mac OSX specific .app file, which could be something to consider if you intend to release your app to Mac OS. The .app file extension provides Mac users with a familiar file name structure which can add a subtle level of authenticity to your app. I will cover building a jar file into a Mac OS .app file in a future post.

Option 1: JSmooth (Click here to download from SourceForge)

Smooth provides a basic and easy to use interface to build your jar file into an exe. JSmoth frequently uses relative file paths, so I tend to launch within the distribution file of my compiled Java app.

The app opens on the Welcome screen, which is pretty standard. You can navigate through the pages using the menu located at the left-hand side of the screen.

  • Executable Binary: Enter the location and file name for the exe file you wish to output. I’ve chosen to save my output file as MYAPP.exe.
  • Executable Icon: Select and icon image file to be used in your exe file. I’ve chosen to use WORLD.png located in the source of my app.
  • Current Directory:Here you can specify the working directory, your safe to leave this blank unless you implicitly wish to set it.

Head to the Application section

  • Main Class: Enter the name of the main class in your Java app, that is the main initial class called at runtime.
  • Application Arguments: You can optionally enter arguments to be passed to your app at runtime. You can enter multiple arguments separated by a comma.
  • Embedded Jar: Tick the option to Use an embedded jar and specify the name of your compiled Java .jar file. This tells JSmooth the location from which to load your .jar file.
  • Classpath: Here you must enter every external library (.jar file) included in your app. My compiled Java app has my libraries contained in a LIB folder in the same directory as my MYAPP.jar file.
    IMPORTANT
    ! The library paths are relative to your jar file AND your exe. In my example, MYAPP.exe MUST launch from a file location containing the LIB folder holding the libraries.

Head to JVM Selection

  • Minimum JVM Version: If your app requires a specific JVM version or higher, specify here.
  • Maximum JVM Version: If you wish to limit your app to run below a certain JVM, specify here.
  • Bundle JRE with your app – OPTIONAL You can optionally include a bundled Java Runtime Environment with your exe. This enables your app to run an a PC that doesn’t already have a Java installation. It also gives you control over which JVM version gets used to run your app. If the PC already has a JVM installed then your exe will use the bundled version instead of the PC version. Leave this blank to default to the installed JRE.
    Tick Use a JVM bundled with your applicationSpecify a file location, relative to your exe, which contains a full JVM.In my example, I have a folder named JRE_MASTER which contains a full JVM.
    NOTE: You may bundle a 32-BIT JVM with your exe for operation on 64-BIT OS. This could be useful if your app contains 32-BIT libraries.

Go ahead and hit the COG icon at the top of the screen. All being well, you should now have a .exe file bundled from your compile .jar file.

Launch4J is set out using a tabbed menu located at the top of the screen, opening on the Basic tab. I will describe some of the basic functions required to generate an exe and once familiar with the process you can explore the advanced functions.

  • Output File: Enter the location and file name for the exe file you wish to output. I’ve chosen to save my output file as MYAPP.exe.
  • Jar: Specify the name of your compiled Java .jar file. This tells Launch4J the location from which to load your .jar file.
  • Icon: Select and icon image file to be used in your exe file. I’ve chosen to use WORLD.ico.
  • Java Download URL: http://java.com/download

  • Bundle JRE with your app – OPTIONALYou can optionally include a bundled Java Runtime Environment with your exe. This enables your app to run an a PC that doesn’t already have a Java installation. It also gives you control over which JVM version gets used to run your app. If the PC already has a JVM installed then your exe will use the bundled version instead of the PC version. Leave this blank to default to the installed JRE.
  • Bundled JRE Path: Specify a file location, relative to your exe, which contains a full JRE.
  • Min JRE Version: If your app requires a specific JRE version or higher, specify here.
  • Max JRE Version: If you wish to limit your app to run below a certain JRE, specify here

  • Launch4j provides the ability to specify a splash screen, this feature is not available in JSmooth.
  • Tick Enable splash screen
  • Splash File:Specify as location of your launch image
  • Tick Wait for window
  • Timeout [s]: Enter a timeout period in which the splash screen will disappear

IMPORTANT!: The library paths are relative to your exe. In my example, MYAPP.exe MUST launch from a file location containing the LIB folder holding the libraries. You are not required to implicitly mention each library whilst configuring your exe, as you are with JSmooth.

You should now have a runnable exe file bundled from your compile .jar file. Take some time to explore the advanced functions within the configuration menus.

Источник

What is java.library.path?

send pies

posted 18 years ago

  • 2
  • Report post to moderator
  • I wonder what is java.library.path? where should i set this and how?

    send pies

    posted 18 years ago

  • Report post to moderator
  • Your application tries to load a native library called liblwjgl.so (Linux) or lwjgl.dll (Windows).
    Check to see if this file exists and what it’s file permissions are.
    This problem most often shows up because the file does not exist, has incorrect file permissions, or can not be located by the JVM.

    Under Linux set LD_LIBRARY_PATH.
    Under Windows set PATH.

    send pies

    posted 18 years ago

  • Report post to moderator
  • Thanks, I found the lwjgl.dll files, there are about 4 dll files. Where should i put them?

    How to find out that JVM is looking for a ‘.dll’ or ‘.so’ module from the exception thrown?

    author and iconoclast

    Chrome

    send pies

    posted 18 years ago

    • 1
  • Report post to moderator
  • The message says «java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
    ary.path». The «lwjgl» is interpreted in a platform-dependent way. On Windows, it means LWJGL.DLL . On Linux and many other UNIX variants, it means «liblwjgl.so». On other platforms, it may mean something else. You’re just supposed to know what it means on your platform.

    Since you’re on windows, just make sure the file(s) are in a directory named on your PATH environment variable; you could add an entry if need be.

    Источник

    How to set the java.library.path from Eclipse

    How can I set the java.library.path for a whole Eclipse Project? I’m using a Java library that relies on OS specific files and need to find a .dll/ .so/ .jnilib . But the Application always exits with an error message that those files are not found on the library path. I would like to configure this whole project to use the library path. I tried to add the path as a VM argument to some run configurations in eclipse but that didn’t work.

    16 Answers 16

    Don’t mess with the library path! Eclipse builds it itself!

    Instead, go into the library settings for your projects and, for each jar/etc that requires a native library, expand it in the Libraries tab. In the tree view there, each library has items for source/javadoc and native library locations.

    Specifically: select Project , right click -> Properties / Java Build Path / Libraries tab, select a .jar, expand it, select Native library location, click Edit, folder chooser dialog will appear)

    Messing with the library path on the command line should be your last ditch effort, because you might break something that is already properly set by eclipse.

    Native library location

    that’s a good question. Why do you have more than one folder of native libraries for one jar? I’m pretty sure there is a way where you can embed the native libraries themselves into the jar, so that might be the way to go?

    I’m working on a legacy project over which I have very little control. I certainly can’t change the project structure. My workaround for the moment is to put everything on my PATH — but that’s not so nice.

    depending on OS, you could create one directory that is all symbolic links to the other libraries? or at least for development purposes you could copy all the libs to one directory.

    I can’t make this stick. I can add the native library path, but when I click OK to go all the way back out then jump into project properties again, it says «None». I’m using eclipse Indigo SR2

    Источник

    Оцените статью