简体   繁体   English

蝙蝠文件在退出时打开空白命令提示符

[英]Bat file opens blank command prompt on exit

I have a .jar file and a jre folder for that specific .jar, by doing this the user won't have to download Java or if they don't have it, it will still work. 我有一个.jar文件和该.jar的jre文件夹,通过这样做,用户无需下载Java,或者如果他们没有Java,它仍然可以工作。

To launch this .jar file I am using a .bat file like seen below. 要启动此.jar文件,请使用如下所示的.bat文件。

%~dp0jre\bin\java.exe -jar %~dp0myJar.jar

The %~dp0 part is used to find the path to the jre file and the .jar file. %~dp0部分用于查找jre文件和.jar文件的路径。

But the issue is the fact that whenever the .bat file has launched the .jar file, the command prompt is still open. 但是问题在于,只要.bat文件启动了.jar文件,命令提示符仍会打开。 I understand that this is because my program is a "window" app and therefor the command prompt would first close once the .jar program has finished. 我了解这是因为我的程序是一个“窗口”应用程序,因此.jar程序完成后,命令提示符将首先关闭。

To fix this I have tried to modify the .bat file with the following different methods. 为了解决这个问题,我尝试使用以下不同方法修改.bat文件。 (not all at once, one at a time) (不是一次全部一次)

start %~dp0jre\bin\java.exe -jar %~dp0myJar.jar

start "" "%~dp0jre\bin\java.exe" -jar %~dp0myJar.jar

start %~dp0jre\bin\java.exe -jar %~dp0myJar.jar 
exit

start "" "%~dp0jre\bin\java.exe" -jar %~dp0myJar.jar
exit

%~dp0jre\bin\java.exe -jar %~dp0myJar.jar 
exit

But... When using these methods, of which seem to work for other people, instead of closing the command prompt completely. 但是...使用这些方法似乎对其他人有用,而不是完全关闭命令提示符。 It exits the command prompt which launches the .jar file and then opens a blank command prompt. 它退出命令提示符,启动.jar文件,然后打开空白命令提示符。 When closing the .jar it then closes the new blank command prompt or if I close the new blank command prompt it also closes the .jar 当关闭.jar时,它将关闭新的空白命令提示符;或者,如果我关闭新的空白命令提示符,则还将关闭.jar

I am unable to figure out exactly why this is, and why it doesn't just exit the first command prompt which launches the .jar and then that is it. 我无法弄清楚到底是什么原因,以及为什么它不仅仅退出第一个启动.jar的命令提示符,然后就是它。

To try and figure out the issue, I have drawn inspiration from questions like this and many more. 要设法弄清楚这个问题,我从这样的问题得出的灵感等等。

Update 更新

Just tried to run the command straight from a command prompt, instead of using the actual .bat file. 只是尝试直接从命令提示符下运行命令,而不是使用实际的.bat文件。 But because I am using the method to get the full path of the jre and .jar in the Bat file, I had to instead use the full path. 但是,因为我使用的是在Bat文件中获取jre和.jar的完整路径的方法,所以我不得不使用完整路径。 Nevertheless it actually worked this time, by using this method. 不过,实际上这次使用这种方法确实有效。

my\long\path\jre\bin\java.exe -jar my\long\path\myJar.jar 
exit

But when using the same method within the .bat file it doesn't open the blank command prompt, but it never exits the first "original" command prompt. 但是,在.bat文件中使用相同的方法时,它不会打开空白的命令提示符,但是它永远不会退出第一个“原始”命令提示符。

After a couple of different tries, with all kinds of commands I have figured that for my case what worked is to use start /B in front of whatever command afterwords. 经过几次不同的尝试,使用各种命令,我发现对于我而言,可行的方法是在任何命令后缀之前使用start /B So in my case the command would look like below. 因此,在我的情况下,命令如下所示。

start /B %~dp0jre\bin\java.exe -jar %~dp0myJar.jar

The question/answer where I found the fix is the following 我找到修复程序的问题/答案如下

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

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