How to use wildcards in JavaExec classpath

So I have a folder that has manu jar files. And I have to use these jar files in classpath for java exec task. But it gives me -

What went wrong:
Execution failed for task ‘:TestProj:runTest’.
A problem occurred starting process 'command ‘C:\Program Files\Java\jdk1.6.0_31\bin
java.exe’'
Caused by: java.io.IOException: CreateProcess error=206, The filename or extension is
too long

I am doing something like

def jarFiles = files { file('testDir').listFiles() }
task runTest(type: JavaExec) {
main = 'Main'
classpath = jarFiles 
} 

I also tried the same on cygwin. Same error…

I’ve tried to use ‘/*’ in path but gradle throws exception

"Could not normalize path for file

Try running your build with the --info argument to see what the actual command Gradle is attempting to run is. Also, you can simplify this a bit to include only .jar files.

classpath = fileTree(dir: 'testDir', include: '*.jar')

The command that is generated is like

C:\Program Files\Java\jdk1.6.0_31\bin\java.exe -XX:MaxPermSize=1024m -Xmx2048m -Dfile.encoding=windows-1252 -cp.... 

it has around 300 jars and then around 60 arguments. Any way to reduce these?

You may want to consider whether all those JARs are necessary. You’re running to the command line length limit on Windows. If you do some Googling there are all kinds of hacky workarounds for this. Another alternative is always to use an operating system other than Windows :wink:

One way to get around the Windows command line length limit is to create a jar that just has a manifest with a Class-Path attribute and main class.

There’s some info here: http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle

I’ve cobbled together some code from various places to implement Luke’s suggestion of creating a manifest jar. I’ve used it for the BootRunTask that’s shipped with the Spring Boot plugin but it should be the same for JavaExec as BootRunTask extends it.

task pathingJar(type: Jar) {
    classifier 'pathing'
    doFirst {
        manifest {
            attributes 'Class-Path': configurations.runtime.files.collect{ project.uri(it) }.join(' ')
        }
    }
    dependsOn configurations.runtime
}
task bootWin(type: org.springframework.boot.gradle.run.BootRunTask) {
    dependsOn pathingJar
    dependsOn processResources
    dependsOn compileJava
    jvmArgs = applicationDefaultJvmArgs
    classpath = files(project.buildDir.toString() + '/classes/main', project.buildDir.toString() + '/resources/main', pathingJar.archivePath)
    main = 'mypackage.MyMainClass'
}