How can I get the classpath from Gradle to execute a Java program with?

Gradles javaexec is failing miserably by not returning any error messages when it fails. All it will tell me is that the java binary returned a non-zero error code. That’s worthless for actually finding out what is wrong.

I want to work around this by executing the Java program using Java code in a task – i.e. Runtime.getRuntime.exec(“java -cp …”) instead of javaexec. In order to do this I will have to get the complete classpath from Gradle in the form of properly qualified or relative path names to all of the .jar files that have to be in the classpath.

I’ve tried this to get the classpath from Gradle

println sourceSets.main.runtimeClasspath

println sourceSets.main.compileClasspath

println sourceSets.test.runtimeClasspath

but all it prints is

file collection configuration ‘:compile’ file collection

How do I get Gradle to give me the classpath in the form that an Java program needs?


It’s ‘sourceSets.main.runtimeClasspath.asPath’.

Do you get better error messages when you run with ‘–info’?

@Daz - No. The --info, --stacktrace, and --debug options just spew out hundreds of lines of org.gradle… stack trace which is completely worthless.

javaexec needs to return the stderr and stdout from the process.

If you want to forward the output from javaexec to the parent process you need to explicitly say so.

javaexec {
 standardOutput = System.out
 standardError = System.err }