1

I run the following code. But output error.

(Docker file) From openjdk COPY ./* /tmp WORKDIR /tmp ENV CLASSPATH=target/spring_batch_commandline_sample-1.0-SNAPSHOT.jar:lib/:target/classes/ CMD java org.springframework.batch.core.launch.support.CommandLineJobRunner -next job-setting.xml job1

Files:

Files

Error running container after building image

Error: Could not find or load main class org.springframework.batch.core.launch.support.CommandLineJobRunner Caused by: java.lang.ClassNotFoundException: org.springframework.batch.core.launch.support.CommandLineJobRunner

I could run the files on Windows powershell and wsl.

(powershell)

java -cp 'target/spring_batch_commandline_sample-1.0-SNAPSHOT.jar;lib/*' org.springframework.batch.core.launch.support.CommandLineJobRunner -next job-setting.xml job1

(wsl)

java -cp 'target/spring_batch_commandline_sample-1.0-SNAPSHOT.jar:lib/*' org.springframework.batch.core.launch.support.CommandLineJobRunner -next job-setting.xml job1

I can not understand the reason for this error.
Please answer it.

HoRn
  • 1,458
  • 5
  • 20
  • 25
lll
  • 11
  • 1
  • Check which files were copied - were they done recursively and completely? Check whether `$CLASSPATH` is what you think it is – g00se Sep 22 '22 at 08:18
  • unless you have to, would strongly recommend the single jar spring-boot produces: https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.first-application.executable-jar for maven and https://stackoverflow.com/a/52404325/995891 for gradle and https://www.baeldung.com/spring-boot-console-app for an example regarding commandlinerunner. You only need the `spring-boot-starter` (without the usual `-something` suffix) to have a single jar that contains all the libraries and would need nothing but `java -jar myjar.jar -next job-setting.xml job1` to run – zapl Sep 22 '22 at 11:40

1 Answers1

1

Try to surround the value of CLASSPATH with "", meaning:

ENV CLASSPATH="..."

Also try to explicitly add the classpath to your command:

CMD java -cp $CLASSPATH ...

That said, I would recommend shading your jar and use a command like:

java -jar mybachjob.jar

Spring Boot does that for you, otherwise you can use maven shade plugin.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50