How to copy files out of the currently running jar

后端 未结 3 2162
鱼传尺愫
鱼传尺愫 2020-12-01 22:38

I have a .jar that has two .dll files that it is dependent on. I would like to know if there is any way for me to copy these files from within the .jar to a users temp folde

3条回答
  •  遥遥无期
    2020-12-01 23:15

    Since your dlls are bundeled inside your jar file you could just try to acasses them as resources using ClassLoader#getResourceAsStream and write them as binary files any where you want on the hard drive.

    Here is some sample code:

    InputStream ddlStream = .class
        .getClassLoader().getResourceAsStream("some/pack/age/somelib.dll");
    
    try (FileOutputStream fos = new FileOutputStream("somelib.dll");){
        byte[] buf = new byte[2048];
        int r;
        while(-1 != (r = ddlStream.read(buf))) {
            fos.write(buf, 0, r);
        }
    }
    

    The code above will extract the dll located in the package some.pack.age to the current working directory.

提交回复
热议问题