You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Java或Spring编程获取已执行的.jar文件名称?

当然可以!不管是用纯Java还是Spring(尤其是Spring Boot),都能轻松实现获取启动Jar文件名的需求,下面给你分场景的具体实现方案:

纯Java原生实现方案

如果你的项目没有用到Spring框架,用原生Java就能搞定,这里有两种靠谱的方法:

方法一:通过系统属性java.class.path获取

当你用java -jar xxx.jar命令启动应用时,JVM的java.class.path系统属性值就是这个Jar文件的完整路径,直接提取文件名即可:

import java.io.File;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class JarNameUtils {
    public static String getJarFileName() {
        String classPath = System.getProperty("java.class.path");
        // 处理路径中的URL编码(比如空格会被转成%20)
        try {
            classPath = URLDecoder.decode(classPath, StandardCharsets.UTF_8.name());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new File(classPath).getName();
    }

    public static void main(String[] args) {
        System.out.println("当前启动的Jar文件名:" + getJarFileName());
    }
}

⚠️ 注意:这个方法只适用于java -jar单Jar启动的场景,如果是多个Jar在classpath下的启动方式,结果可能不符合预期。

方法二:通过类的保护域获取(更通用)

这种方法不受classpath设置影响,只要当前类是从Jar文件中加载的,就能准确获取对应的Jar文件名,适用性更广:

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.CodeSource;
import java.security.ProtectionDomain;

public class JarNameUtils {
    public static String getJarFileName() {
        // 获取当前类的保护域信息
        ProtectionDomain domain = JarNameUtils.class.getProtectionDomain();
        CodeSource codeSource = domain.getCodeSource();
        if (codeSource == null) {
            return "非Jar包启动";
        }

        URL jarUrl = codeSource.getLocation();
        String jarPath = jarUrl.getPath();
        // 解码URL编码的路径
        try {
            jarPath = URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new File(jarPath).getName();
    }

    public static void main(String[] args) {
        System.out.println("当前启动的Jar文件名:" + getJarFileName());
    }
}
Spring Boot实现方案

如果你的项目是Spring Boot应用,那有更便捷的方式——Spring框架提供的ApplicationHome类,专门用来处理应用的主Jar或目录信息:

首先创建一个工具类或者服务组件:

import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Component;
import java.io.File;

@Component
public class JarNameService {
    public String getCurrentJarName() {
        ApplicationHome home = new ApplicationHome(getClass());
        File jarFile = home.getSource();
        if (jarFile == null) {
            return "非Jar包启动(比如IDE运行)";
        }
        return jarFile.getName();
    }
}

之后就可以在需要的地方注入这个服务来获取Jar文件名,比如在控制器中:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JarInfoController {
    private final JarNameService jarNameService;

    // 构造方法注入(Spring 4.3+支持)
    public JarInfoController(JarNameService jarNameService) {
        this.jarNameService = jarNameService;
    }

    @GetMapping("/jar-name")
    public String getJarName() {
        return "当前启动的Jar文件名:" + jarNameService.getCurrentJarName();
    }
}

这个方法的好处是自动适配场景:如果是java -jar启动,返回Jar文件名;如果是在IDE中运行,会返回项目的类目录路径,非常灵活。

内容的提问来源于stack exchange,提问作者Felix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:29:25