2

I follow several entries here with solutions but still, I cannot make it work.

I have a Java application to be executed inside a Docker container, and that requires running with two JAR files in classpath and I'm not able to make it with all these combinations in my Dockerfile.

ENTRYPOINT [ "java", "-classpath", "app.jar;another.jar", "com.pol.Main" ]
CMD [ "java", "-classpath", "app.jar;another.jar", "com.pol.Main" ]
CMD [ "java", "-classpath", "app.jar:another.jar", "com.pol.Main" ]
CMD [ "java", "-classpath", "app.jar;/*", "com.pol.Main" ]

I know the app.jar is there since if I add this one it's able to find the Main class, but then it fails because it cannot find some Classes required from another.jar

With option 3

CMD [ "java", "-classpath", "app.jar:another.jar", "com.pol.Main" ]

I can find the main class of app.jar but another.jar is not loaded

java.util.NoSuchElementException: null
    at java.util.ServiceLoader$2.next(ServiceLoader.java:1308) ~[?:?]
    at java.util.ServiceLoader$2.next(ServiceLoader.java:1296) ~[?:?]
    at java.util.ServiceLoader$3.next(ServiceLoader.java:1394) ~[?:?]

In local running with the command

java -cp "app.jar;./*"  com.pol.Main

Works perfectly fine and Service provider load the instance fine.

paul
  • 12,873
  • 23
  • 91
  • 153
  • I would have guessed that the third one works. Are the files in the same directory? Java uses `:` as the separator on Linux, `;` is only used on Windows. Also make sure to not have *both* `ENTRYPOINT` and `CMD` in your Dockerfile, unless you know why/how to use that combination. – Joachim Sauer Nov 08 '22 at 10:12
  • Also, can you post the error message? Because it can happen that a class from `another.jar` fails to load because *it itself* is missing a required class from `third.jar` (which is why I usually leave the job of "collect the dependencies" to a proper build system like Gradle). – Joachim Sauer Nov 08 '22 at 10:14
  • Related: [Run a JAR file from the command line and specify classpath](https://stackoverflow.com/questions/18413014/run-a-jar-file-from-the-command-line-and-specify-classpath). – MC Emperor Nov 08 '22 at 10:20
  • I already update with error – paul Nov 08 '22 at 10:51
  • Looks like your `ServiceProvider` stuff is wrong – g00se Nov 08 '22 at 11:03
  • It's working in local fine – paul Nov 08 '22 at 11:24
  • You cannot use both cmd and entrypoint like that. Also, you're missing the `.` before the `/*` in your example. Try removing cmd and using just the `./*` classpath. – Software Engineer Nov 08 '22 at 11:36
  • 1
    could you please post the "ls -l" of the folder you're trying to run. I believe that the third option is the way to go, however there can be many things that can go wrong: permissions (that's why I've asked to post / check the ls inside the docker), lack of other jars that you haven't included and many others. Also its possible to define a CLASSSPATH env. variable 'separately' (define it in Dockerfile) and you won't need to use -cp. In non-dockerized env. such a global var can be too fragile, but in docker you have one process anyway, so it doesn't matter. – Mark Bramnik Nov 08 '22 at 11:43
  • *Works perfectly fine and Service provider load the instance fine.* That's a different thing. In additional to the trivial difference (that it's running on WIndows), you are using a glob there that could take in other paths (e.g. to other jars) as well. Do `java -verbose:class -cp "app.jar;./*" com.pol.Main` and make a careful note of the paths. Then make sure those are used in the Docker version. Of course the equivalent would be `CMD [ "java", "-classpath", "app.jar:/*", "com.pol.Main" ]` but I'd avoid globs – g00se Nov 08 '22 at 12:03
  • @MarkBramnik how the Dockerfile it should looks like if I use ENV CLASSPATH, not sure how to configure then the CMD – paul Nov 08 '22 at 12:23
  • Your classpath is wrong. Trying to place it elsewhere is not going to help you – g00se Nov 08 '22 at 12:27
  • @MarkBramnik you were right after do ls -l I found the another.jar is actually empty, thanks a lot! – paul Nov 08 '22 at 12:55

0 Answers0