0

I have been trying to "dockerize" a microservice that I have built using java. However, I am having some issues with the classpath when using Docker. I am on a Linux machine using java open jdk 1.8. My dockerfile looks something like:

FROM openjdk:version

#`Make the directories I need
RUN mkdir -p /opt/service/libs

ENV LIBS_PATH=/opt/service/libs
ENV CLASSPATH=LIBS_PATH"/*"

COPY ./target/libs $LIBS_PATH
COPY ./target/MY_SERVICE.jar $LIBS_PATH

ENTRYPOINT java -cp $CLASSPATH "com.org.service.mainClass"

I have noticed when I do this and I echo the $CLASSPATH inside of the docker container, (using docker run), it returns all of the adresses of the jar files, but with a space in between instead of a : seperator. This implies two things, it knows where all of these jar files are and that the jar files have been successfully copied. I still get an error on the java -cp line. The error is as follows:

Error: Could not find or load main class .opt.service.libs.FirstJarInLibsFolder.jar

If I change the CLASSPATH to have the : separators where it is supposed to, I instead get the error:

Error: Could not find or load main class com.org.service.mainClass

even though my JAR contains that class.

Any thoughts on what it could be?

I have tried what is detailed above.

  • Specifying a classpath with spaces separating the elements is incorrect and won’t work. Show us your attempt using colons. – Tim Moore Apr 21 '23 at 21:39
  • Also please verify that the name of the class is correct; the convention is that class names begin with a capital letter, so `MainClass` instead of `mainClass` – Marcono1234 Apr 23 '23 at 13:02
  • @TimMoore Hi Tim, I did not set these as spaces, that is just how the $CLASSPATH environment variables appears after I do: ENV CLASSPATH="/opt/service/libs/*" as shown in the question. To use colon seperators I copied the output of $echo CLASSPATH and did a replace on all " ", with ":", then it finds the MainClass, but not the other jars. – boredStackOverflowuser Apr 25 '23 at 17:56
  • @Marcono1234 Hi! Sorry about that typo, yes the name is MainClass. I just typod when making the question. – boredStackOverflowuser Apr 25 '23 at 17:57

1 Answers1

0

This line looks incorrect:

ENV CLASSPATH=LIBS_PATH"/*"

It should probably have a $ before the variable name, see also the Dockerfile reference.

ENV CLASSPATH=$LIBS_PATH"/*"

Additionally to be safe you should probably put quotes around the java -cp argument:

ENTRYPOINT java -cp "$CLASSPATH" "com.org.service.MainClass"

(As mentioned in the comments, this also uses the correct capitalization of MainClass)


when I do this and I echo the $CLASSPATH inside of the docker container, (using docker run), it returns all of the adresses of the jar files, but with a space in between

This probably performs wildcard expansion in the shell for the * in the classpath; you would have to run echo "$CLASSPATH" (with quotes) to prevent this and verify that the path is correct. The result from the wildcard expansion might not necessarily be the same as how java behaves. For example as seen in this question the shell will expand *.jar just fine, but passing it to java -cp will not work, you will have to use only * (as you have done).

Marcono1234
  • 5,856
  • 1
  • 25
  • 43