Java dynamic loading dll file

I do not know the specific principles, however, when loading dll file with the path or change the name of the dll files are being given. Although the library records successful, but Virgo think it's unacceptable. So with this solution.

Create a soft link in the root directory for the library, and then use system.loadLibrary ( "libname") to load. It turns out that when it considered soft connection.

On the code:

import com.seapine.surroundscm.api.*;
import java.lang.UnsupportedOperationException;
import java.lang.SecurityException;
import java.io.IOException;
import java.nio.file.*;

public class SurroundSCMAPIExample {
      static {
        String arch = System.getProperty("sun.arch.data.model");

        Path target;
        
        if(arch.equals("32"))
        {
            target = FileSystems.getDefault().getPath("lib","sscmapi.dll");   
        }
        else
        {
            target = FileSystems.getDefault().getPath("lib64","sscmapi.dll");
        }

        Path link = FileSystems.getDefault().getPath("sscmapi.dll");

        try {
            Files.deleteIfExists(link);
            Files.createSymbolicLink(link, target);   
        } catch (IOException | UnsupportedOperationException | SecurityException e) {   
            if (e instanceof SecurityException) {   
                System.err.println("Permission denied!");   
            }   
            if (e instanceof UnsupportedOperationException) {   
                System.err.println("An unsupported operation was detected!");   
            }   
            if (e instanceof IOException) {   
                System.err.println("An I/O error occurred!");   
            }   
            System.err.println(e);
        }

        System.loadLibrary("sscmapi");
      }
      
    public static void main(String[] args)
    {
       SSCMContext context = new SSCMContext();
       SSCMResult ret =  SSCMAPI.connectRSA("localhost",
               4900,
               "",
               "admin",
               "",
               context);

       if(SSCMAPI.SSCM_API_OK == ret.result)
          System.out.println("Connection Succeeded");
       else
          System.out.println("Connection Failed: " +
                  SSCMAPI.getLastError(ret.result));

       SSCMAPI.disconnect(context);
    }
}

Properly properly, no longer see any error.

Guess you like

Origin www.cnblogs.com/pied/p/11133413.html