是否可以获取用于在 Java 中启动 jvm 的命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13958318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:34:00  来源:igfitidea点击:

Is it possible to get the command used to launch the jvm in java?

javajvm

提问by Hyman

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 程序:

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

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

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

Is it possible?

是否可以?



Comment on the bounty and the question

评论赏金和问题

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 agodinhostand Luigi R. Viggianoto solve my problem. However I give the bounty to rahulrocfor the most complete (portable) response. For the rest an upvote for all :)

谢谢大家的回复。不幸的是,我没有得到我最初想要的答案。我希望有一些便携式解决方案可以从程序本身(包括类路径等)中获取完整的 java 命令。似乎没有可移植的解决方案,因为我使用的是 Linux,所以我使用agodinhostLuigi R. Viggiano的响应来解决我的问题。但是,我为rahulroc提供了最完整(便携)的响应。其余的都给大家一个赞:)

回答by Rahul

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

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

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"));
}

javadoc for getInputArguments

用于getInputArguments 的javadoc

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.

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.

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.

返回传递给 Java 虚拟机的输入参数,其中不包括 main 方法的参数。如果 Java 虚拟机没有输入参数,则此方法将返回一个空列表。

一些 Java 虚拟机实现可能会从多个不同的来源获取输入参数:例如,从启动 Java 虚拟机的应用程序传递的参数,例如“java”命令、环境变量、配置文件等。

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

You can also take a look at : jps

你也可以看看:jps

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.

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

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

您可以找到各种JVM 工具的良好摘要,包括 指向以下内容的Java Application Launcher链接:

回答by assylias

You can use thisto retrieve the VM parameters :

您可以使用来检索 VM 参数:

public static void main(String args[]) {
    List<String> inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
    System.out.println("input arguments = " + inputArguments);
}

However it won't give you all the command line (only gives the JVM arguments, no main class nor parameters). Sample output:

但是,它不会为您提供所有命令行(仅提供 JVM 参数,没有主类或参数)。示例输出:

input arguments = [-Dfile.encoding=UTF-8, -XX:-UseTLAB, -Xms2000m, -Xmx2000m, -XX:+PrintCompilation, -XX:+PrintGC]

输入参数 = [-Dfile.encoding=UTF-8, -XX:-UseTLAB, -Xms2000m, -Xmx2000m, -XX:+PrintCompilation, -XX:+PrintGC]

回答by Luigi R. Viggiano

It only works on  Sun  Oracle JVM: System.getProperty("sun.java.command")

它仅适用于 Sun  Oracle JVM:System.getProperty("sun.java.command")

Additionally, you can have a look at JavaSysMon, it can report command line of active processes. To check which is the current JVM Process check here: How can a Java program get its own process ID?

此外,您可以查看JavaSysMon,它可以报告活动进程的命令行。要检查哪个是当前的 JVM 进程,请在此处检查:Java 程序如何获得自己的进程 ID?

回答by agodinhost

in a linux machine would be easier to run:

在 linux 机器上运行会更容易:

ps -ef | grep java

this command will list all java programs running with it's used parameters.

此命令将列出所有使用它使用的参数运行的 Java 程序。

Not sure about what can be used in a windows environment.

不确定可以在 windows 环境中使用什么。

回答by djangofan

In the task manager on Win2003 you can enable the display of a column that displays the command like it does on linux. Or, you can do it from the command line like so:

在 Win2003 上的任务管理器中,您可以像在 linux 上一样启用显示命令的列的显示。或者,您可以像这样从命令行执行此操作:

wmic.exe PROCESS where "name like '%java%'" get Processid,Caption,Commandline