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

如何查看运行中Jar文件的命令行启动参数及类路径信息

获取运行中Java程序的完整启动参数

方法一:在目标进程内部直接获取

如果能修改目标Java程序代码,或通过Agent注入代码,可直接调用JMX相关API获取所有启动信息:

  • 获取JVM启动参数(含-Xmx、-D系统属性等)与类路径:
    import java.lang.management.ManagementFactory;
    import java.lang.management.RuntimeMXBean;
    import java.util.List;
    
    public class StartupArgsPrinter {
        public static void printStartupInfo() {
            RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
            
            // 输出JVM启动参数(不含main方法的args)
            System.out.println("JVM启动参数:");
            runtimeMXBean.getInputArguments().forEach(System.out::println);
    
            // 输出类路径
            System.out.println("\n类路径:");
            System.out.println(runtimeMXBean.getClassPath());
    
            // Java 9+ 可直接获取完整启动命令行
            if (Runtime.version().feature() >= 9) {
                String fullCommand = ProcessHandle.current().info().commandLine().orElse("无法获取完整命令");
                System.out.println("\n完整启动命令行:");
                System.out.println(fullCommand);
            }
        }
    }
    
    将这段代码嵌入目标程序,或做成Java Agent动态注入,即可输出所需信息。

方法二:用Java Attach API从外部进程获取

若无法修改目标程序,可通过Attach API连接到运行中的VM,远程获取RuntimeMXBean数据:

import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.List;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class RemoteArgsFetcher {
    public static void main(String[] args) throws Exception {
        // 遍历所有运行中的VM,匹配目标进程(可通过进程ID/名称过滤)
        List<VirtualMachineDescriptor> vms = VirtualMachine.list();
        for (VirtualMachineDescriptor vmd : vms) {
            if (vmd.displayName().contains("目标Jar名称")) { // 替换为目标程序标识
                VirtualMachine vm = VirtualMachine.attach(vmd.id());
                try {
                    // 获取或初始化JMX连接地址
                    String connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
                    if (connectorAddress == null) {
                        String agentPath = vm.getSystemProperties().getProperty("java.home") +
                                "/lib/management-agent.jar";
                        vm.loadAgent(agentPath);
                        connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
                    }
                    // 建立JMX连接并获取远程RuntimeMXBean
                    JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(connectorAddress));
                    MBeanServerConnection mbs = connector.getMBeanServerConnection();
                    RuntimeMXBean remoteRuntime = ManagementFactory.newPlatformMXBeanProxy(
                            mbs, ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);

                    // 输出目标进程信息
                    System.out.println("目标进程ID: " + vmd.id());
                    System.out.println("\nJVM启动参数:");
                    remoteRuntime.getInputArguments().forEach(System.out::println);
                    System.out.println("\n类路径:");
                    System.out.println(remoteRuntime.getClassPath());
                    if (Runtime.version().feature() >= 9) {
                        System.out.println("\n完整启动命令行:");
                        System.out.println(remoteRuntime.getCommandLine());
                    }
                } finally {
                    vm.detach();
                }
                break;
            }
        }
    }
}

注意:Java 8及以下运行时需依赖JDK的tools.jar,Java 9+需确保引入jdk.attach模块。

方法三:用系统命令直接查看

无需编写代码时,可借助系统自带工具:

  • Linux/macOS:使用JDK自带的jps工具或ps命令:
    # 显示所有Java进程的完整Jar路径、进程ID与JVM参数
    jps -lv
    # 或用ps查看完整命令行
    ps -ef | grep java
    
  • Windows:使用wmic命令获取完整启动命令:
    wmic process where "name='java.exe'" get commandline, processid
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 18:52:58