ProcessBuilder - java program to solve the problem of LD_LIBRARY_PATH (dynamic decompress the file so then add the path)

solved problem:

In linux, the DLL search path needs to specify the use of LD_LIBRARY_PATH. E.g

export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/your_lib_path

If you are lazy, so jar file in the package, but do not want to enter other commands before running jar package, write another script.

But when the package runs jar, so copy the file to the tmp folder, and dynamically load the library.

However, this error will be reported can not find the library.

Because the java program is already running, and then export LD_LIBRARY_PATH added tmp has no use.

Solutions:

By ProcessBuilder first set up LD_LIBRARY_PATH, and then create a new Process, dynamic library search path of this process contains the tmp up.

For example in the jar package, App1 is the main class, App2 so compressed library file package from solution jar and loaded, then the error will not (but why when the first performance will still error, the second on the can be found.).

public class App1 {
    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder();
        pb.inheritIO();
        String tmpStr = System.getProperty("java.io.tmpdir");
        String sep = File.separator;
        pb.environment().put("LD_LIBRARY_PATH", tmpStr + sep);
        pb.command("java", "App2");
        Process p;
        try {
            p = pb.start();
            p.waitFor();
            System.out.println("Bye!");
            p.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }            
    }
}

Guess you like

Origin www.cnblogs.com/zhsmtp/p/12070357.html