Distribution - How to change the start script with no shell window

Hi everone

I’m using the distribution plugin. It generates a start script – .bat file – which works nice and makes things easier. But it opens unfortunately also a shell window which remains as long as my application runs. If I close the shell window, I close my application also.

Any idea how to change the generating process of this script file to close the window automaticly after starting the application?

You may try the assembleDist command and take a look in the build directory here:

Probably make it call javaw, not java

Thank you. It was a little tricky first to understand what to do, but I have the batch file now in a way that closes the disturbing window.

Now: If I make a new distribution, I have to edit the .bat file again. Any idea how to set gradle up to spite out a start file with these new customization?

Edit:
I know that the documentatin gives some examples for customized distributions. But, as far I know, gives no information how to change already existing.

tasks.startScripts {
    doLast {
        listOf(unixScript, windowsScript).forEach { script ->
            script
                .readText()
                .replace("java", "javaw")
                .let(script::writeText)
        }
    }
}

Or alternatively configure own start script templates for the task, but then you will also not get eventually made improvements of future Gradle versions.

1 Like

Ah, I see. Tweaking the start script after generating it.

I do not know a lot about batch scripts. The generated batch script does not contain the simple java command. Instead of, “%JAVA_EXE%” (including quotation marks) is used. I do not know exactly what it means, as far I know %anyname% means a variable but I do not have an idea what the quoation marks are for.

I just replaced

"%JAVA_EXE%"

by

start javaw

even though I’m sure that this is a kind of dirty. But as any fail checks are done before, I guess one can do so. But if I would do this with a simple string replacement, I will replace things I do not want to replace, so I better search in one specific line (line 75 where first line is no. 1). Is there such a nice fitting line for selecting only one line to do that replacement?

My snippet was not a guess.
And yes, it does use simple java command that is searched before in various sources.
Just replacing all java by javaw is fine for both start scripts, at least in their current state in Gradle 8.14.1.

1 Like

With what you did, you make great parts of the start script a waste of time as you ignore the result and also it will only work if you have javaw in the PATH. And using start should be unnecessary.

1 Like

I’m sure that you know what to do, but this did not work, unfortunately. I do not know why, as the replacement is correct. But the changes I described before with replacing

"%JAVA_EXE%"

with

start javaw

worked as intended. With the only single change I intended, all the checks before are not obsolete. The change is after the “:execute” jump mark (to be exactly: the line were java actually called only), and this can only reached as the checks before are fine with no problem. At least as I understand the batch file, ofcourse. It is a quiet dirty solution, I know, and the exit code will get lost.

Nevertheless, I read your post a while before but did not have time to answer, but thinking about it, I added a little echo before calling java, to investigate what is actually in JAVA_EXE. And I get this line:

C:\Program Files\Eclipse Adoptium\jdk-21.0.3.9-hotspot\/bin/javaw.exe

I’m a little confused about the ‘/’ as linux styled path separator and surprised that I get java called and not an error message. In special because the doubled path separator “/”. Is this intended to be so?

I’m sure that you know what to do, but this did not work,

Hm, strange, sorry.
The whole point of javaw vs. java is, that no console window is shown.
Seems to still be different when using a batch script.

With using start you do not even need to use javaw, but can also use java, so it would be this (tested this time :smiley: ):

tasks.startScripts {
    doLast {
        windowsScript
            .readText()
            .replace("""^(?="%JAVA_EXE%")""".toRegex(RegexOption.MULTILINE), """start "" /min """)
            .let(windowsScript::writeText)
    }
}

But as javaw also shows dialog boxes on errors, it might still be better to replace java by javaw, so I would recommend

tasks.startScripts {
    doLast {
        windowsScript
            .readText()
            .replace("java", "javaw")
            .replace("""^(?="%JAVA_EXE%")""".toRegex(RegexOption.MULTILINE), """start "" /min """)
            .let(windowsScript::writeText)
    }
}

With the only single change I intended, all the checks before are not obsolete. The change is after the “:execute” jump mark (to be exactly: the line were java actually called only), and this can only reached as the checks before are fine with no problem.

That’s exactly the point.
You let it first do the discovery which Java to use for execution, and then at the line where you would execute the discovered result, you just throw away the result of the work done previously and just use javaw from PATH and only if it is available there.

I’m a little confused about the ‘/’ as linux styled path separator and surprised that I get java called and not an error message. In special because the doubled path separator “/”. Is this intended to be so?

In what you show there is no double-separator, as you did not use verbatim formatting:
image
But knowing that this is the case in this forum software I know what you mean, which is caused by your JAVA_HOME ending with a path separator.

That forward and backward slashes are mixed is a minor nit in the Gradle-built-in template for that script.
It might be a bit cleaner to use backslashes, feel free to post an issue report or pull request that fixes it, but it works either way, cmd.exe is just intelligent enough to understand it it seems.

1 Like

Hell, now it works just fine. Thanks a lot for your gentle and patient help.

1 Like