我尝试编写一个powershell脚本来运行我的Jar,这只是一个使用tomcat catalina servlet的简单的hello world spring web mvc应用程序。问题是执行我的Jar文件后,powershell实例被servlet占用。这意味着我无法在localhost:8080上运行连续命令-> open 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
运行代码,即以阻塞方式运行。因此,直到服务器关闭后才可以到达该呼叫,这显然不是目的。Start-Process
您可以通过两种基本选择来使调用异步:
Start-Process
在新窗口中运行命令(仅Windows):# Opens the server in a *new window* (Windows only)
Start-Process java '-jar .\helloworld-0.0.1-SNAPSHOT.jar'
Start-Job
或Start-ThreadJob
[v6 +]),该作业在不可见的后台进程或线程中运行该命令,您可以使用*-Job
cmdlet进行监视和管理:# 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
阻止它(并由此服务器)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句