Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Workaround for gradle application plugin 'the input line is too long' error on Windows
tasks.withType(CreateStartScripts).each { task ->
task.doLast {
String text = task.windowsScript.text
text = text.replaceFirst(/(set CLASSPATH=%APP_HOME%\\lib\\).*/, { "${it[1]}*" })
task.windowsScript.write text
}
}
@jlmelville

This comment has been minimized.

Copy link
Owner Author

@jlmelville jlmelville commented Oct 27, 2015

The gradle application task handily helps in creating a CLI program from a gradle project. However, in the Windows batch file, the classpath is set by manually specifying every jar on the classpath, e.g.:

set CLASSPATH=%APP_HOME%\lib\some.jar;%APP_HOME%\lib\another.jar;%APP_HOME%\lib\yet-another.jar

and then using that with the -classpath switch of your java executable. If you have a large classpath (and let's face it, it's Java, so that's probable), you are in imminent danger of overflowing CMD's puny line length restrictions, resulting in a the input line is too long error.

If you insert the snippet above in your build.gradle file, it will use:

set CLASSPATH=%APP_HOME%\lib\*

which works with Java 6 and above.

@barrybecker4

This comment has been minimized.

Copy link

@barrybecker4 barrybecker4 commented Jan 5, 2019

Thanks. This helped a lot. I was not having a problem with a classpath that was too long, but I did want everything that was in the /lib directory included on may classpath. Here is what I found worked for both linux and windows.

tasks.withType(CreateStartScripts).each { task ->
    task.doLast {
        task.windowsScript.write task.windowsScript.text.replaceFirst(/(set CLASSPATH=%APP_HOME%\\lib\\).*/, { "${it[1]}*" })
        task.unixScript.write task.unixScript.text.replaceFirst(/(CLASSPATH=.APP_HOME\/lib\/).*/, { "CLASSPATH=\$(echo \$APP_HOME/lib/*.jar | tr ' ' ':')" })
    }
}
@kovzol

This comment has been minimized.

Copy link

@kovzol kovzol commented Apr 7, 2020

Thanks! @barrybecker4: Unfortunately, the line
task.unixScript.write task.unixScript.text.replaceFirst(/(CLASSPATH=.APP_HOME\/lib\/).*/, { "CLASSPATH=\$(echo \$APP_HOME/lib/*.jar | tr ' ' ':')" }) does not work properly when the folder names contain a space character in Unix. Instead, the same idea as for Windows may be used:
task.unixScript.write task.unixScript.text.replaceFirst(/(CLASSPATH=.APP_HOME\/lib\/).*/, { "${it[1]}*" })

@james-d-brown

This comment has been minimized.

Copy link

@james-d-brown james-d-brown commented Sep 14, 2020

Helpful workaround. A slightly cleaner approach in the same vein is to override the classpath property in the startScripts, which is the pre-configured task of type CreateStartScripts added by the "application" plugin.

startScripts {
    classpath = files( '$APP_HOME/lib/*' )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment