How Java modifies Java.library.path and does not restart the JVM can also take effect

Source: Internet
Author: User

Let's talk about demand,

Currently in the Java program with JCEF implementation of the desktop version of the packaging, the source code needs to load compiled several DLL files, and the path of these files must be based on the path of the program installation changes, which need to run in the program, To dynamically modify the Java.library.path to implement DLL file loading

At the very beginning, I thought of System.setproperty (); But no matter how to try, there is no way to dynamically load in, and then Google a bit, found that this method needs to restart the JVM, the reason for the following code:

if (sys_paths = = null) {            usr_paths = Initializepath ("Java.library.path");            Sys_paths = Initializepath ("Sun.boot.library.path");        }

This code in the ClassLoader class, the LoadLibrary method, the meaning of this code is very well understood, sys_paths is null when the variable will be loaded once, that is, the JVM can not be loaded after startup, Since it is not possible to use System.setproperty directly (); That's just another way, I started looking for information on the Internet, not hard to find many blogs (original or reproduced) provides a method, the code is as follows:

private static void Addlibrarydir (String librarypath) throws Exception {Field field = ClassLoader.class.getDeclaredField ("Usr_paths"); Field.setaccessible (true); String[] paths = (string[]) field.get (null), for (int i = 0; i < paths.length; i++) {if (Librarypath.equals (Paths[i])) { return;}} string[] tmp = new String[paths.length + 1]; System.arraycopy (paths, 0, tmp, 0, paths.length); Tmp[paths.length] = Librarypath;field.set (NULL, TMP);}

I use this method to add my path, but in the actual load, still can't find, this is a bit embarrassing, I suspect that the code is wrong, or the location of the add path is not correct, but tried n long, the method is always unable to run through, perhaps the younger brother qualifications slightly worse, such as the lucky have the great God to see here, Still looking for directions

Since blocked, we can not be hanged in a tree is not, another to find his way, English crappy I do not implement will kill technology, Google began to search English materials, Google search "java modify Java.library.path at runtime" I didn't think the first search would solve my problem.!!!!!!

Changing-java-library-path-at-runtime

The first method of the blog, or the top two lines of code, or that location, the blogger's method is to set the sys_paths is null, since each time only when the null will be initialized, then why don't you set the path, reset to NULL, so that the next time he executes to initialize it? Young I still have a lot of way to go ah, so seemingly simple idea, incredibly never thought, ashamed ...

Using this method, the final code is as follows:

private static void Addlibrarydir (String librarypath) throws Exception {Field Userpathsfield = ClassLoader.class.getDeclaredField ("usr_paths"); Userpathsfield.setaccessible (true); String[] paths = (string[]) userpathsfield.get (null); StringBuilder sb = new StringBuilder (), for (int i = 0; i < paths.length; i++) {if (Librarypath.equals (Paths[i])) {Conti Nue;} Sb.append (Paths[i]). Append (';');} Sb.append (LibraryPath); System.setproperty ("Java.library.path", sb.tostring ()); final Field Syspathsfield = ClassLoader.class.getDeclaredField ("sys_paths"); Syspathsfield.setaccessible (true); Syspathsfield.set (null, NULL) ;}

Well, the problem of the perfect solution, the heart of a stone landing, noon can eat more than two pieces of meat, haha ~ ~

The article was recently published in CSDN blog, after the promotion, in the blog Park to publish a copy of the article for the original and translation, reproduced please indicate the source, thank you!

How Java modifies Java.library.path and does not restart the JVM can also take effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.