Skip to content

Instantly share code, notes, and snippets.

@jlmelville
Created October 27, 2015 17:17
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlmelville/2bfe9277e9e2c0ff79b6 to your computer and use it in GitHub Desktop.
Save jlmelville/2bfe9277e9e2c0ff79b6 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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
Copy link

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
Copy link

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