2

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.

To launch this .jar file I am using a .bat file like seen below.

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

The %~dp0 part is used to find the path to the jre file and the .jar file.

But the issue is the fact that whenever the .bat file has launched the .jar file, the command prompt is still open. 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.

To fix this I have tried to modify the .bat file with the following different methods. (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. 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

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.

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. 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. 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.

DevLiv
  • 573
  • 7
  • 19
  • When running a batch file, I thought that the window has to stay open bcz a batch file must be launched from a command prompt and therefore it is tied to the window – Rabbit Guy Aug 07 '17 at 21:02
  • @rabbitguy Yes but because it is only used to launch a .jar file, it should open a command prompt for the bat file. But once the bat function has finished and the .jar file has been opened, there is no more need for the .bat file and therefor it can be closed. But you are right whenever using a .bat file it has to open a command prompt, but only until the task it has to do, has been finished. – DevLiv Aug 07 '17 at 21:11
  • It sorta sounds like it's not the batch file doing this.... You said it closes the console but then when the jar opens it launches a new blank cmd. Does it do this if you run the start command from cmd manually? – Mark W Aug 07 '17 at 21:14
  • @MarkW I appreciate the input, it doesn't open the new blank command prompt after the .jar. It first opens the original command prompt, from the .bat file, then quickly closes that one and opens the blank command prompt and then it is first after that, the .jar file actually opens. – DevLiv Aug 07 '17 at 21:24
  • I'm sorry... I misunderstood. Same question though, albeit... my confidence that it's not the batch file has waned. – Mark W Aug 07 '17 at 21:25
  • @MarkW no problem, I just tried what you suggested and it did almost the same thing, but instead of closing the first "original" command prompt it kept it open and also opened the blank command prompt. So it now has both the .jar file and two different command prompts open. – DevLiv Aug 07 '17 at 21:28
  • One suggestion you can run you command as background task in the bat file. I remember I had done similar task once to open a java swing based application with embedded JRE, i.e. JRE available along with the app. Are you still getting the issue. Its not clear at what state you are after your update. – Acewin Aug 07 '17 at 21:41
  • @Acewin I appreciate the input, I will look into it and get back. – DevLiv Aug 07 '17 at 21:43
  • I was updating my comment. Are you still having the issue. – Acewin Aug 07 '17 at 21:44
  • @Acewin I am still trying to figure out how to make it work, I tried to use the full file paths, instead of using the %~dp0 function. But doing so it doesn't even open the .jar file and just immediately closes the command prompt. I am currently trying to do what you mention, by running the .bat file/command prompt in the background, but I can't seem to find any example of it. – DevLiv Aug 07 '17 at 21:49
  • ok then let me try to find a working solution. Only problem is I am on mac. Once I am back on a windows machine to play around windows scripting I would be more useful. But be sure what you are trying to do is nothing new. in fact it is similar to not having java set in path variable. – Acewin Aug 07 '17 at 21:55
  • check this one https://stackoverflow.com/questions/8938944/how-to-run-java-application-by-bat-file This has proper batch scripts provided and should work fine. The accepted answer is using @echo off. – Acewin Aug 07 '17 at 21:59
  • Doesn't Java have javaw.exe for running without a console window? BTW, a console is not the command prompt. If java.exe is a console application, it either inherits or creates a console, which has absolutely nothing to do with cmd.exe. – Eryk Sun Aug 07 '17 at 22:02
  • 2
    I found a fix, I have posted an answer for any other people who may run into the same issue. – DevLiv Aug 07 '17 at 22:13

1 Answers1

3

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. 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

DevLiv
  • 573
  • 7
  • 19
  • `/B` runs java.exe without passing the `CREATE_NEW_CONSOLE` flag to `CreateProcess`, so it inherits the current console. But if your app doesn't actually use the console (i.e. it was just creating a blank console window), then why not use javaw.exe to run it without a console rather than having the initial console sitting there until the Java program exits? – Eryk Sun Aug 07 '17 at 22:33
  • @eryksun I tried to use the javaw.exe inside the bin of the jre, but for some reason I couldn't make it work and it still opened a command prompt. Thus I just wen't straight for this fix. But I do appreciate your input and help. – DevLiv Aug 07 '17 at 22:38