java 获取 jar 目录 生成 logs 目录

public static void main(String[] args) {
        String path = ClassName.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        try{
            path = java.net.URLDecoder.decode(path, "UTF-8");//转换处理中文及空格  
        }catch (java.io.UnsupportedEncodingException e){
            e.printStackTrace();
        }  
        File file = new File(path);
        if(file != null){
            String jarPath = file.getParent();
            System.out.println(jarPath);
            jarPath = jarPath.substring(0,jarPath.lastIndexOf(file.separator) + 1);
            System.out.println(jarPath+"logs"+file.separator+"test.log");
        }
        
    }
原文地址:https://www.cnblogs.com/feelgood/p/5945285.html