'Could not normalize path for file' for Windows classpath

I recently wrote a build.gradle script that does the following (using Gradle 1.7):

  1. Build a classpath using a custom ant task. The resulting classpath would be in the form of C:\path\to\dir;C:\another\path\to\jar\myJar.jar;etc…

  2. Assign the above classpath to a project property e.g. ext.myClasspath = ant.properties[‘ant.generated.classpath’]

  3. In my dependecies I added the following:

<dependencies>
    <compile>
      files("$myClasspath")
    </compile>
  </dependencies>

When I run the script on Linux, everything works fine. When I run the script on Windows, I get the error:

Could not resolve all dependencies for configuration ‘:MyProject:compile’. Could not normalize path for file ‘C:…’

The only way I could fix this issue was to check if the operating system is Windows, change all of the backslashes to forward slashes and ensure that every path contained a preceding forward slash i.e. /C:/path/to/dir;/C:/path/to/jar/myJar.jar;/C:/…etc.

I couldn’t find anything else online about this issue and it took me a while to find a solution, so I just thought I would post this in case anyone else runs into the same problem.

I don’t understand. Where is this XML snippet coming from?

Sorry, it should be:

dependencies {
    compile(
      files("$myClasspath")
    )
  }

That’s still not correct. The ‘{’ after ‘compile’ needs to go, and ‘files’ needs to be passed a collection of file paths, not a class path. Something like:

dependencies {
    compile files(myClasspath.split(";"))
}

I’ve edited my previous post to be in the correct format.

Splitting the classpath by path separator causes my build to hang. Having the classpath in its original format allows the build to compile successfully.

There may be other problems with your build script then. Your code effectively puts nothing on the compile class path, because there isn’t any single file or directory named ‘"/C:/path/to/dir;/C:/path/to/jar/myJar.jar;…"’. Unless ‘myClasspath’ isn’t a String, but a collection of Strings (but it doesn’t look like it).

Ok, just tried it again, splitting the classpath and it compiled :confused:

As you say though, it’s strange that it also still compiles with the separator…

Means that either none of the classes on the class path are used by the code to be compiled, or the class path is configured in some other way, or something else (e.g. an Ant task) is actually compiling the code. Impossible to say without knowing the details of your build.

I know that that classes on the classpath are definitely being used and all my ant task is doing is building the classpath.

Is it possible that the compiler is just ignoring the path separators?

The compiler is eventually passed a semicolon separated class path string. ‘files’, however, expects to be fed with a collection of individual file paths. Maybe it can be tricked in this way, but I wouldn’t put it to a test.

I think you’re right, I will keep the split by separator to keep things right.