我想知道是否可以从代码中获取用于启动 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 以获得最完整(便携)的响应.对于其余的,所有人都赞成:)
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"));
}
的 javadoc获取输入参数
返回传递给 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 应用程序启动器 链接到:
You can find a good summary of various JVM tools, including Java Application Launcher links to :
ManagementFactory.getRuntimeMXBean(
) - 返回 Java 虚拟机运行时系统的托管 bean.getInputArguments()
javadoc这篇关于是否可以获得用于在 java 中启动 jvm 的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!