需求
再对海康摄像头进行二次开发时,需要将海康提供的.dll(window)文件或.so(linux)文件通过路径的方式进行配置,项目在window上开发.在linux上进行部署,另一方面在linux上是通过jar包部署,无法直接获取到jar中.dll与.so文件的路径,需做成自动获取
问题
1.获取jar包中的指定的文件
2.如何填写有效的文件路径
通过jar包启动
获取jar包的路径,通过JarFile解析jar包,获取到想要的数据.复制到对应的文件夹下,将路径返回
代码
//通过jar包启动
public static String getLibraryPath(String type, String name) {
String fileName = null;
URL url= Application.class.getProtectionDomain().getCodeSource().getLocation();//获取jar包的路径
try{
//转化为utf-8编码,支持中文
String path= URLDecoder.decode(url.getPath(),"utf-8");
if(path.startsWith("file:")) {
//判断是什么系统
if (SystemUtils.name().equals("windows")) {
path = path.replace("file:/", "");
} else if (SystemUtils.name().equals("linux")) {
path = path.replace("file:", "");
}
}
if(path.contains(".jar!/")) {
path = path.substring(0, path.indexOf(".jar!/")+4);//去掉后缀
}
// System.out.println(path);
File file = new File(new File(path).getParentFile() + File.separator + type);//当前jar下新增目录
if (!file.exists()) {
JarFile jarFile =