Powershell JAR 可执行文件占用实例

Powershell JAR executable occupies instance

我尝试为 运行 我的 Jar 编写一个 powershell 脚本,它只是一个使用 tomcat catalina servlet 的简单 hello world spring web mvc 应用程序。问题是在我执行我的 Jar 文件后,powershell 实例被 servlet 占用了。这意味着我不能 运行 一个连续的命令 -> 在 localhost:8080.

上打开 chrome

我当前的 powershell 脚本:

java -jar .\helloworld-0.0.1-SNAPSHOT.jar

# doesnt get executed due to the servlet occupation of the ps instance
Start-Process "chrome.exe" "http://localhost:8080" 

有没有办法在同一个 powershell 实例中 运行 第二个命令,或者我是否需要打开另一个 ps 实例?如果,我该怎么做?

正在执行 Java 代码 java.exe 运行 代码 同步 ,即在 阻塞 [=36] 中=]时尚。因此,Start-Process 调用直到服务器关闭后才会到达,这显然不是本意。

您有两个基本选择来进行调用异步:

  • 使用 Start-Process 到 运行 new window 中的命令(仅限 Windows):
# Opens the server in a *new window* (Windows only)
Start-Process java '-jar .\helloworld-0.0.1-SNAPSHOT.jar'
  • 使用后台或线程作业 (Start-Job or Start-ThreadJob [v6+]),运行这是一个不可见的后台进程或线程中的命令,您可以使用 *-Job 命令:
# Start the server in a background process.
# In PowerShell [Core] 6+ you can use the lighter-weight Start-ThreadJob cmdlet.
# Monitor it with the *-Job cmdlets, such as Receive-Job.
$jb = Start-Job { java -jar .\helloworld-0.0.1-SNAPSHOT.jar }

# Syntactically simpler PowerShell [Core] v6+ alternative:
# (A postpositional & is the same as using Start-Job.
# As of PowerShell 7.0, there is no operator for Start-ThreadJob.)
$jb = java -jar .\helloworld-0.0.1-SNAPSHOT.jar &

使用 Receive-Job $jb 获取作业的输出并使用 Stop-Job $jb 停止它(从而停止服务器)。