我有一个简单的 maven 应用程序,其中有几个类,其中一个主类是 WiremockApplication.java 和 Stubs.java,它使用 WireMockServer 对象来启动一个 wiremock 服务器。我用一些 json 文件存根了它,当我作为独立应用程序运行时它可以工作。但是当我将它放在 docker 文件中时,即使我将独立 jar 放在 docker 文件的类路径中,它也无法识别 WireMockServer 类。 它给出了找不到类的错误
Exception in thread "main" java.lang.NoClassDefFoundError: com/github/tomakehurst/wiremock/WireMockServer
at com.homesite.api.wiremock.Stubs.setUp(Stubs.java:16)
at com.homesite.api.wiremock.WiremockApplication.main(WiremockApplication.java:8)
Caused by: java.lang.ClassNotFoundException: com.github.tomakehurst.wiremock.WireMockServer
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 2 more
下面是两个文件和docker文件。我不确定当独立 jar 位于类路径中时为什么会出现此错误。 这真的让我觉得 jar 没有在类路径中设置,但不知道为什么会这样。
WiremockApplication.java
------------------------
public class WiremockApplication { // Main method
public static Stubs stubs = new Stubs();
public static void main(String[] args) {
stubs.setUp()
.stubForPost("DummyPost.json")
.status();
}
}
Stubs.java
----------
public class Stubs { // Stubbing class
private JsonUtil jsonUtil;
public WireMockServer wireMockServer;
public Stubs setUp() { // Stubbing setup
wireMockServer = new WireMockServer(7080);
wireMockServer.start();
jsonUtil = new JsonUtil();
return this;
}
public Stubs stubForPost(String responseFileName) { // Stub method
wireMockServer.stubFor(post("/dummyPost")
.willReturn(aResponse()
.withStatus(200)
.withFixedDelay(200)
.withHeader("Content-Type", "application/json")
.withBodyFile(responseFileName)));
return this;
}
}
Docker file
-----------
FROM maven:3.6.3-openjdk-11
ENV WIREMOCK_PORT=8080
RUN apt-get install wget -y
RUN wget -O wiremock-standalone.jar 'https://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-standalone/2.27.2/wiremock-standalone-2.27.2.jar' --no-check-certificate
COPY target/*api*.jar /app.jar // download the standalone wiremock
RUN ls -a
EXPOSE ${WIREMOCK_PORT}
ENTRYPOINT ["java", "-classpath", "wiremock-standalone.jar", "-jar", "app.jar"]