no sqljdbc_auth in java.library.path

后端 未结 6 646
再見小時候
再見小時候 2020-12-03 00:38

I have a Java EE Web Application which connects to a SQL Server 2008 instance. I don\'t have any problem connecting and retrieving to all my tables, except for one of them.

6条回答
  •  悲哀的现实
    2020-12-03 01:25

    I've just encountered the same problem but within my own application. I didn't like the solution with copying the dll since it's not very convenient so I did some research and came up with the following programmatic solution.

    Basically, before doing any connections to SQL server, you have to add the sqljdbc_auth.dll to path.. which is easy to say:

    PathHelper.appendToPath("C:\\sqljdbc_6.2\\enu\\auth\\x64");

    once you know how to do it:

    import java.lang.reflect.Field;
    
    public class PathHelper {
        public static void appendToPath(String dir){
    
            String path = System.getProperty("java.library.path");
            path = dir + ";" + path;
            System.setProperty("java.library.path", path);
    
            try {
    
                final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
                sysPathsField.setAccessible(true);
                sysPathsField.set(null, null);
    
            }
            catch (Exception ex){
                throw new RuntimeException(ex);
            }
    
        }
    
    }
    

    Now integration authentication works like a charm :).

    Credits to https://stackoverflow.com/a/21730111/1734640 for letting me figure this out.

提交回复
热议问题