java 如何获取启动进程的命令行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2541627/
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
How do I get the commandline that started the process
提问by aksamit
From Java, is it possible to get the complete commandline with all arguments that started the application?
从 Java 中,是否可以获得包含启动应用程序的所有参数的完整命令行?
System.getEnv()and System.getProperties()do not appear to contain the values.
System.getEnv()并且System.getProperties()似乎不包含这些值。
采纳答案by Stephen Denne
Some of it is available from the RuntimeMXBean, obtained by calling ManagementFactory.getRuntimeMXBean()
其中一些可从 RuntimeMXBean 获得,通过调用获得 ManagementFactory.getRuntimeMXBean()
You can then, for example call getInputArguments()
然后你可以,例如调用 getInputArguments()
The javadocs for which say:
其 javadocs 说:
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 虚拟机。因此,返回的输入参数可能不包括所有命令行选项。
回答by Johannes Weiss
In Linux/UNIX that should be possible when you get the output of that command (run in a shell)
在 Linux/UNIX 中,当您获得该命令的输出(在 shell 中运行)时,这应该是可能的
cat /proc/$PPID/cmdline
But that is not portable at all and should therefore not be used in Java...
但这根本不是可移植的,因此不应在 Java 中使用...
回答by Leniel Maccaferri
The following links may help you get there:
以下链接可以帮助您到达那里:
How to get command line arguments for a running process
get command-line of running processes
How to get a list of current open windows/process with Java?
Just as a note:
就像一个注释:
In Windows you have Process Explorer by Sysinternalsthat shows you the command line used to open the process. Right click the process and select Properties... You'll see Command Line in the window that is opened.
在 Windows 中,您有Sysinternals 的 Process Explorer,它向您显示用于打开进程的命令行。右键单击进程并选择属性...您将在打开的窗口中看到命令行。
回答by Asgeir S. Nilsen
You might want to look into how jpsdoes this. 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.
你可能想看看jps这是如何做到的。它是一个 Java 程序,能够获取所有 Java 进程的完整命令行,包括主类的完整类名和 JVM 选项。
回答by ggonsalv
There is a environment variable %~dp0 which returns the complete path
有一个环境变量 %~dp0 返回完整路径
回答by SamWest
Have a look at YAJSW (Yet Another Java Service Wrapper) - it has JNA-based implementations for various OSes (including win32 and linux) that do exactly this so it can grab the commandline for a running process and create a config that wraps it in a service. A bit more info here.
看看 YAJSW(Yet Another Java Service Wrapper)——它为各种操作系统(包括 win32 和 linux)提供了基于 JNA 的实现,这些实现正是这样做的,因此它可以获取正在运行的进程的命令行并创建一个将其包装在其中的配置一项服务。这里有更多信息。
回答by Keshav
If you are using solaris as the OS, take a look at "pargs" utility. Prints all the info required.
如果您使用 solaris 作为操作系统,请查看“pargs”实用程序。打印所需的所有信息。
回答by Raymond Kroeker
One option I've used in the past to maintain the cross-platform-shine is to set the command line as an environment variable prior to issuing the command.
我过去用来维护跨平台闪耀的一种选择是在发出命令之前将命令行设置为环境变量。

