poidasmith / winrun4j

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

Prepend java.library.path.N to system PATH #64

Open pauldaustin opened 10 years ago

pauldaustin commented 10 years ago

Would it be possible to modify it so that if a java.library.path.N ini parameter is specified that the PATH environment variable is updated to that all the java.library.path entries appear before the system path.

The reasoning behind this is that java.library.path is used for the library specified in System.loadLibrary. However if that depends on another library then that is loaded from the PATH environment variable instead. If the PATH has a different version of that library then this fails.

I have an installation where I can't modify the PATH for the user so can't fix it that way.

poidasmith commented 10 years ago

Yup, that's a good idea. Have added to the list.

pauldaustin commented 10 years ago

Great, look forward to seeing it in the app.

I added the following to a copy of the code I have in JVM.cpp line 369. Pretty simple just prepend all the java.library.path values to the current PATH.

    // Look for java.library.path.N entries
    TCHAR *libPaths[MAX_PATH];
    UINT libPathsCount = 0;
    INI::GetNumberedKeysFromIni(ini, JAVA_LIBRARY_PATH, libPaths, libPathsCount);
    if(libPathsCount > 0) {
        TCHAR libPathArg[4096];
        libPathArg[0] = 0;
        TCHAR pathArg[4096];
        pathArg[0] = 0;
        strcat(libPathArg, "-Djava.library.path=");
        for(int i =0 ; i < libPathsCount; i++) {
            strcat(libPathArg, libPaths[i]);
            strcat(libPathArg, ";");
            strcat(pathArg, libPaths[i]);
            strcat(pathArg, ";");
        }
        strcat(pathArg, getenv("PATH"));
        SetEnvironmentVariable("PATH", strdup(pathArg));
        args[count++] = strdup(libPathArg);
    }
neandrake commented 10 years ago

We've run into this issue as well but we solved it through Java, something like this:

String path = System.getProperty("java.library.path");
path = "C:\software\path\" + File.pathSeparator + path;
System.setProperty(path);
// now System.loadLibrary() can be used and properly resolve path lookups