poidasmith / winrun4j

WinRun4J Java Application Launcher
http://winrun4j.sourceforge.net
212 stars 63 forks source link

Provide an option to redefine an environment variable #24

Closed ikus060 closed 11 years ago

ikus060 commented 11 years ago

When loading native library for JNI, the DLL required to be in search path. Whould be nice to privode a way to override the PATH environment variable.

e.g.: PATH='../my_path/lib/;%PATH%'

Otherwise, a batch script is required.

poidasmith commented 11 years ago

Thanks for the suggestion.

I think you could solve this particular issue by using vm args, eg:

vmarg.1=-Djava.libary.path=../my_path/lib;%PATH%

ikus060 commented 11 years ago

The suggested solution doesn't work since the java.libary.path property only influence the java class loader -- i.e. System.loadLibrary() --but it doesn't influence the linking to a second dll.

I have a JNI library, named glpk_java.dll. This library will try to load glpk.dll. Modification of java.libary.path, will influence the loading of glpk_java.dll, but not glpk.dllé

ikus060 commented 11 years ago

I've found this very good explanation to my problem while googling. http://kalblogs.blogspot.ca/2009/01/java.html

poidasmith commented 11 years ago

Yes, that's a fair point. The only other option I can think for now is to use a little native binding and modify the path environment variable in your java code before calling loadLibrary, eg:

import org.boris.winrun4j.PInvoke;
import org.boris.winrun4j.PInvoke.DllImport;
import org.boris.winrun4j.PInvoke.UIntPtr;

public class EnvironmentVariables
{
    static {
        PInvoke.bind(EnvironmentVariables.class, "kernel32.dll");
    }

    @DllImport("kernel32.dll")
    public static native int GetEnvironmentVariable(String lpName, StringBuilder lpBuffer, UIntPtr nsize);

    @DllImport("kernel32.dll")
    public static native boolean SetEnvironmentVariable(String lpName, String lpValue);

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        UIntPtr uip = new UIntPtr(4096);
        GetEnvironmentVariable("PATH", sb, uip);
        String path = sb.toString();
        System.out.println(path);
        // Modify path now
        path += ";../mypath/etc";
        SetEnvironmentVariable("PATH", path);
        GetEnvironmentVariable("PATH", sb, uip);
        System.out.println(sb.toString());
    }
}
ikus060 commented 11 years ago

Thanks poidasmith, I will have a look into this. Since the same base code is used for linux and Windows, I will need to check the integration of the code provided.

On the long term, I may submit a patch to set environment variables from the ini file using the following syntax:

env.PATH=./lib;%PATH%