Home > front end >  running a docker container and trying to check the log of the container prints an error on console
running a docker container and trying to check the log of the container prints an error on console

Time:07-13

I have built the image into a container, why I try to print the log of the container I run in detach mode I get an error printout :

Error: Could not find or load main class EazyBankApplication
Caused by: java.lang.ClassNotFoundException: EazyBankApplication

Please how can I fix this, I have tried changing the run command to accommodate a directory path and it still persists.

FROM openjdk:17
RUN mkdir /eazyApp
COPY ./src/main/java/com/eazybank/ /eazyApp
WORKDIR /eazyApp
CMD java EazyBankApplication

CodePudding user response:

Your copy command does not copy the files in the directory, use

COPY ./src/main/java/com/eazybank/* /eazyApp

Also you don't need mkdir command, copy command will create the directory.

CodePudding user response:

In your Dockerfile, you're COPYing only a single source directory into the image. When the container tries to execute the CMD java EazyBankApplication, the corresponding .class file doesn't exist.

Many SO Java questions address this by compiling the application on the host, and then COPY the built jar file (but not any of its source) into the image.

FROM openjdk:17
WORKDIR /eazyApp
COPY target/eazybankapplication-0.0.1.jar ./
CMD java -jar eazybankapplication-0.0.1.jar

If you want to build the application inside Docker, then you need to invoke the build tool somehow. A typical approach is to COPY the entire application tree into the image, excluding parts of it via a .dockerignore file.

FROM openjdk:17
WORKDIR /eazyApp

# Copy in the entire source tree
# (.dockerignore file excludes host's build/)
COPY ./ ./

# Build the application
RUN ./gradlew build

# Run it
CMD java -jar build/libs/eazybankapplication-0.0.1.jar

(You could combine the two approaches with a multi-stage build to get a final image without the source code, and possible with only the JRE for a smaller image.)

You probably need a build system like Gradle or Maven in practice, but if you only have the source files, you can still directly use javac.

FROM openjdk:17
WORKDIR /eazyApp
COPY ./src/ ./src/
RUN mkdir class
ENV CLASSPATH=/eazyApp/class
RUN find src -name '*.java' -print > files.txt \
 && javac -dclass @files.txt \
 && rm files.txt
CMD java com.eazybank.EazyBankApplication
  • Related