如何将库类路径添加到工作的dist / lib目录?

时间:2013-12-13 15:13:46

标签: java path

我不想通过属性提供java.library.path。如何加载when .jar文件开始在它所在的位置运行。库已经位于dist / lib / something.dll中。

2 个答案:

答案 0 :(得分:1)

这就是我在制作中的表现。如您所见,我利用反射来修改类加载器中的其他私有数据成员usr_paths

 /* 
 * Adds the supplied path into java.library.path.
 * This is benign if the path is already present.
 */    
public static synchronized void addLibraryPath(java.nio.file.Path path) throws myexception
{
    if (path == null){
        return;
    }
    String newPath = path.toString();
    try {
        /*We are using reflection here to circumvent encapsulation; usr_paths is not public*/
        final java.lang.reflect.Field field = ClassLoader.class.getDeclaredField("usr_paths");
        field.setAccessible(true);

        /*Benign if the path is already present*/
        final String[] oldPaths = (String[])field.get(null);
        for (String it : oldPaths){
            if (it.equals(newPath)){
                return;
            }
        }

        /*Add the new path*/
        final String[] newPaths = java.util.Arrays.copyOf(oldPaths, oldPaths.length + 1);
        newPaths[newPaths.length - 1] = newPath;
        field.set(null, newPaths);
    } catch (final java.lang.IllegalAccessException | NoSuchFieldException e){
        throw new myexception(e.getMessage());
    }
}

答案 1 :(得分:-1)

我使用了另一种方法来为JVM加载库。第一步将您的dll文件添加到包中。添加以下代码作为主类的主要方法。

 public static void main(String args[]) {
        /* Set the Nimbus look and feel */
       /* Create and display the form */
        String startupPath=System.getProperty("java.library.path");
        System.out.println![enter image description here][1]("LibraryPath -1:"+startupPath);
        String[] trim=startupPath.split(";");
        if(trim.length > 0){
            String firstPath=trim[0];
            System.out.println("firstPath :"+firstPath);
            String destPath=firstPath+"\\rxtxSerial.dll";
            File destinationFile=new File(destPath);
            if(!destinationFile.exists()){
                try {
                InputStream dllStream=Base.class.getClassLoader().getResourceAsStream("dllPack/rxtxSerial.dll");
                    FileOutputStream fos = null;
                    try{
                    fos = new FileOutputStream(destPath);
                    byte[] buf = new byte[2048];
                    int r = dllStream.read(buf);
                    while(r != -1) {
                        fos.write(buf, 0, r);
                        r = dllStream.read(buf);
                    }
                    }finally{
                        if(fos!=null){
                           fos.close();
                        }
                    }
                 System.out.println("Complated...");
                } catch (IOException ex) {
                    Logger.getLogger(Base.class.getName()).log(Level.SEVERE, null, ex);
                    System.out.println("Problem occured closing...");
                    System.exit(0);
                }
            }

        }