0

I want to create a window through CMD / K start, start the jar package in this new window and get the PID of the jar package.I've tried some methods, but none of them can.

pid = Popen(['cmd', '/k', 'start', 'java', '-jar', 'project-0.0.1-SNAPSHOT.jar'], shell=False).pid

pid = Popen(['java', '-jar', 'project-0.0.1-SNAPSHOT.jar', 'cmd', '/k', 'start'], shell=False).pid

Is there any way?

  • Well, the first line results in starting `cmd.exe` with an automatically assigned process identifier which starts as separate process `java.exe` also with an automatically assigned process identifier and `cmd.exe` keeps running while `java.exe` runs and perhaps closes itself finally. The `cmd.exe` process started with option `/k` continues running until a user enters `exit` or closes the console window if there is one opened at all which it looks like is not the case. – Mofi Mar 14 '22 at 15:21
  • The second line is simply wrong because of the arguments `cmd`, `/k` and `start` are neither for `java.exe` nor (most likely) for `project-0.0.1-SNAPSHOT.jar` to which these three argument strings are finally passed to. – Mofi Mar 14 '22 at 15:22
  • I recommend to read the Python documentation for the [subprocess module](https://docs.python.org/3/library/subprocess.html) carefully and completely from top to bottom. There can be used `subprocess.Popen` with `startupinfo` to call on Windows the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) with the structure [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) to start `java.exe` in a visible console window. – Mofi Mar 14 '22 at 15:29
  • It is most likely better to read first the Microsoft documentation pages for the function `CreateProcess` and the structure `STARTUPINFO` to better understand how to specify in Python code all the function parameters and use `startupinfo` for full control on how `java.exe` is started without using `cmd.exe` at all which is definitely not required here and which also uses `CreateProcess` to run any executable. `java.exe` must be specified with full path in Python script because of `CreateProcess` is not searching for an exe as `cmd.exe` does using the environment variables `PATHEXT` and `PATH`. – Mofi Mar 14 '22 at 15:32
  • So the Python script itself has to search for `java.exe` using the folder paths of environment variable `PATH` or the environment variable `JAVA_HOME` if that environment variable is defined at all. That can be done with Python code quite easily as Python has all the necessary functions to get value of the environment variable `JAVA_HOME` on existing at all or of environment variable `PATH`, split up the `PATH` value to folder paths like a CSV row using `;` as separator and check for existence of `java.exe` in current working directory and each directory of `PATH`. – Mofi Mar 14 '22 at 15:35
  • `Popen(['java', '-jar', 'text.jar'],shell=False,creationflags=subprocess.CREATE_NEW_CONSOLE)` – lllllllliuxiaohan Mar 15 '22 at 01:31

0 Answers0