0

I am running a spring boot application with wiremock. My files structure is like this:

project/main/ 
  - java/package/Wiremock.java
  - resources/wiremock/__files/file.json

Inside Wiremock.java I am calling WireMockServer like this:

WireMockServer wiremockServer = new WireMockServer(WireMockConfiguration.wireMockConfig()
   .withRootDirectory(getClass().getResource("/wiremock").getPath())
    .port(port));
wiremockServer.start();


wiremockServer.stubFor(get(urlEqualTo("/myurl"))
    .willReturn(aResponse()
        .withBodyFile("file.json")
        .withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
        .withStatus(HTTP_OK)));

When I am running it locally it works as expected.

When I compile the app to a jar file , a jar file /Users/user/project-0.0.1-SNAPSHOT.jar is generated with the structure:

BOOT-INF/classes/
  - wiremock/__files/file.json
  - package/Wiremock.class

But when I run the jar file , I'm getting the following error:

java.lang.RuntimeException: java.io.FileNotFoundException: /Users/user/jar:file:/Users/user/project-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/wiremock/__files/file.json (No such file or directory)

Please help, thanks

Elad Cohen
  • 79
  • 9
  • A java.io.File represents a file on the file system, in a directory structure. The Jar is a java.io.File. But anything within that file is beyond the reach of java.io.File. As far as java is concerned, until it is uncompressed, a class in jar file is no different than a word in a word document. See if this [workaround](https://github.com/tomakehurst/wiremock/issues/504#issuecomment-383869098) works for you – Sudhir Jun 24 '20 at 14:40

2 Answers2

0

if this path is correct?

/Users/user/jar:file:/Users/user/project-0.0.1-SNAPSHOT.jar!/BOOT- INF/classes!/wiremock/__files/file.json (No such file or directory)

I find there is one more "!" after XXXX.jar and classess.

Hunter
  • 57
  • 7
  • The correct path of the jar is: /Users/user/project-0.0.1-SNAPSHOT.jar and inside the jar: /BOOT- INF/classes/wiremock/__files/file.json. I don't know what is the meaning of the "!" , this is the exception message. – Elad Cohen Jun 24 '20 at 14:48
  • I found this post that explains the "!" so it seems that the path is correct. https://stackoverflow.com/questions/50067888/why-classloader-returns-a-path-with-extraneous-second-exclamation-point – Elad Cohen Jun 24 '20 at 14:59
0

I just meet the same issue today, when i run wire mock in IDEA, it works. but when i run the application by java -jar mode, wired mock server cannot find the json mock file. the root cause of this issue is that when initialization of wire mock server, it will found the json file by com.github.tomakehurst.wiremock.common.ClasspathFileSource class, it will recursively add files to list of the config path which you specified. the logic of add file is showed below like this.

    public List<TextFile> listFilesRecursively() {
    if (this.isFileSystem()) {
        this.assertExistsAndIsDirectory();
        List<File> fileList = Lists.newArrayList();
        this.recursivelyAddFilesToList(this.rootDirectory, fileList);
        return this.toTextFileList(fileList);
    } else {
        return FluentIterable.from(toIterable(this.zipFile.entries())).filter(new Predicate<ZipEntry>() {
            public boolean apply(ZipEntry jarEntry) {
                return !jarEntry.isDirectory() && jarEntry.getName().startsWith(ClasspathFileSource.this.path);
            }
        }).transform(new Function<ZipEntry, TextFile>() {
            public TextFile apply(ZipEntry jarEntry) {
                return new TextFile(ClasspathFileSource.this.getUriFor(jarEntry));
            }
        }).toList();
    }
}

it will recursively add file which is absolutely started with the path. but when you run with java -jar, the jarEntry.getName is started with 'BOOT-INF'. one of the solution is that override the method with a subclass extend ClasspathFileSource, and modify the match rule. it will fix