是否有可能获得用于在java中启动jvm的命令? [英] Is it possible to get the command used to launch the jvm in java?

查看:93
本文介绍了是否有可能获得用于在java中启动jvm的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从代码中获取用于启动java程序的命令。

I would like to know if it is possible to get from code the command used to launch a java program.

例如。如果我启动一个java程序:

E.g. if I launch a java program with:

 java -cp lib1:lib2:... -jar mylib.jar com.foo.Bar

我想得到确切的字符串(包括jvm参数)。

I would like to get the exact string (jvm parameters included).

有可能吗?

评论赏金和问题

感谢大家的回复。不幸的是,我没有得到我最初寻找的答案。我希望有一些可移植的解决方案从程序本身(包括类路径等)中获取完整的java命令。因为似乎没有可移植的解决方案,因为我使用Linux,我使用 agodinhost Luigi R. Viggiano 的响应来解决我的问题。但是,我将赏金给予 rahulroc 以获得最完整(便携)的响应。对于其余的一个upvote for all:)

Thank you all for your responses. Unfortunately, I did not get the answer I was initally looking for. I was hoping there was some portable solution to get the complete java command from within the program itself (including classpath etc.). As it seems there are no portable solution and since I am using Linux I am using the responses of agodinhost and Luigi R. Viggiano to solve my problem. However I give the bounty to rahulroc for the most complete (portable) response. For the rest an upvote for all :)

推荐答案

下面提到的代码应该显示所有JVM参数,传递给main方法的参数以及主类名。

The below mentioned code should show all JVM parameters, arguments passed to the main method as well as the main class name.

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

import java.util.List;

public static void main(String[] args) {
  RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
  List<String> jvmArgs = bean.getInputArguments();

  for (int i = 0; i < jvmArgs.size(); i++) {
    System.out.println( jvmArgs.get( i ) );
  }
  System.out.println(" -classpath " + System.getProperty("java.class.path"));
  // print the non-JVM command line arguments
  // print name of the main class with its arguments, like org.ClassName param1 param2
  System.out.println(" " + System.getProperty("sun.java.command"));
}

getInputArguments


返回传递给Java虚拟机的输入参数,
不包含main方法的参数。如果Java虚拟
机器没有输入参数,则此方法返回
空列表。

Returns the input arguments passed to the Java virtual machine which does not include the arguments to the main method. This method returns an empty list if there is no input argument to the Java virtual machine.

某些Java虚拟机实现可能需要输入参数来自多个不同来源的
:例如,从
传递的参数是启动Java虚拟机的应用程序,例如
'java'命令,环境变量,配置文件等。

Some Java virtual machine implementations may take input arguments from multiple different sources: for examples, arguments passed from the application that launches the Java virtual machine such as the 'java' command, environment variables, configuration files, etc.

通常,并非'java'命令的所有命令行选项都是传递给Java虚拟机的
。因此,返回的输入参数
可能不包括所有命令行选项。

Typically, not all command-line options to the 'java' command are passed to the Java virtual machine. Thus, the returned input arguments may not include all command-line options.

您还可以查看: jps


这是一个Java程序,它能够获取所有
Java进程的完整命令行,包括主类的完整类名和JVM
选项。

It's a Java program that is able to get the full command line for all Java processes, including full class name of main class and JVM options.

你可以找到各种各样的好摘要 JVM工具,包括
Java Application Launcher 链接到:

You can find a good summary of various JVM tools, including Java Application Launcher links to :

  • ManagementFactory.getRuntimeMXBean() - Returns the managed bean for the runtime system of the Java virtual machine.
  • getInputArguments() javadoc
  • determine if JVM is running in debug mode

这篇关于是否有可能获得用于在java中启动jvm的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆