我有一些代码可以通过命令行运行 java.exe 来启动游戏。由于我不会让玩家同时启动多个游戏程序实例,所以我需要检查游戏是否已经运行。
对于普通的exe文件,我可以从进程信息中获取exe路径。但对于java游戏,执行文件路径始终是java.exe
或javaw.exe
的路径。所以我想到了获取命令行信息来运行jar文件。但是获取不到命令行是有原因的:
Remember that from Win32's point of view, the command line is just a string that is copied into the address space of the new process. How the launching process and the new process interpret this string is governed not by rules but by convention.
所以,我需要另一种方法来做这样的事情,但我想不出任何方法来区分两个 java 进程。谁能给我一些提示?
最佳答案
您不必从启动器运行 java.exe
。相反,您的启动器可能会使用 Invocation API 创建进程内 JVM。 .通过这种方式,您可以检查可执行文件路径、检查命令行、创建命名互斥锁或执行任何您想做的事情,而无需依赖外部 Java 启动器。
另一种解决方案是使用Jvmstat Performance Counters 查找系统中其他Java 进程的主类、参数和VM 选项。 ,就像 jps
和 jcmd
实用程序一样。这个想法是每个 Java 进程(除非使用 -XX:+PerfDisableSharedMem
运行)将某些运行时信息导出为用户的 Temp\hsperfdata_user
文件夹中的内存映射文件。因此,您可以检查这些文件,看看它们是否属于游戏。
关于java - 如何在 Windows 上区分两个正在运行的 java 进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32476122/