简体   繁体   English

如何获取启动进程的命令行

[英]How do I get the commandline that started the process

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()似乎不包含这些值。

Some of it is available from the RuntimeMXBean, obtained by calling ManagementFactory.getRuntimeMXBean() 通过调用ManagementFactory.getRuntimeMXBean()获得的部分RuntimeMXBean可用。

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. 返回传递给Java虚拟机的输入参数,该参数不包含main方法的参数。 This method returns an empty list if there is no input argument to the Java virtual machine. 如果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虚拟机的应用程序传递的参数,例如“ java”命令,环境变量,配置文件等。

Typically, not all command-line options to the 'java' command are passed to the Java virtual machine. 通常,并非将“ java”命令的所有命令行选项都传递给Java虚拟机。 Thus, the returned input arguments may not include all command-line options. 因此,返回的输入参数可能不包括所有命令行选项。

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中使用...

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? 如何使用Java获取当前打开的窗口/进程的列表?

Just as a note: 请注意:

In Windows you have Process Explorer by Sysinternals that shows you the command line used to open the process. 在Windows中,您可以使用Sysinternals的Process Explorer,它向您显示用于打开进程的命令行。 Right click the process and select Properties... You'll see Command Line in the window that is opened. 右键单击该进程,然后选择“属性...”。您将在打开的窗口中看到“命令行”。

You might want to look into how jps does this. 您可能想研究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选项。

有一个环境变量%〜dp0返回完整路径

我过去用来维护跨平台光泽的一个选项是在发出命令之前将命令行设置为环境变量。

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. 看看YAJSW(又一个Java服务包装程序)-它具有针对各种OS(包括win32和linux)的基于JNA的实现,可以完全做到这一点,因此它可以抓取正在运行的进程的命令行并创建将其包装的配置。服务。 A bit more info here . 这里有更多信息。

Java 9 开始,您可以使用ProcessHandle来获取进程的命令行:

ProcessHandle.current().info().commandLine()

If you are using solaris as the OS, take a look at "pargs" utility. 如果您将solaris用作操作系统,请查看“ pargs”实用程序。 Prints all the info required. 打印所有必需的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM