简体   繁体   中英

running Java application with '-cp ...' option in Docker

When I want to run my java program through the command line ( cmd ), I do the following:

  1. Open the first cmd to start the server
    1.1 - javac API.java
    1.2 - javac CLI.java
    1.3 - java -cp.;.\mysql-connector-java-5.1.36.jar API

  2. Open a second cmd for the client
    2.1 - java CLI

In the Dockerfile I wrote it like this:

FROM openjdk

COPY . /java

WORKDIR /java

RUN javac API.java
RUN javac CLI.java

CMD [ "java", "-cp", ".;.\mysql-connector-java-5.1.36.jar", "API" ]
CMD [ "java", "CLI" ]

The -cp option doesn't want to work.
And is it possible somehow to run java CLI in a new terminal?

Java recognizes a CLASSPATH environment variable . You can set this environment variable using a Docker ENV directive, and if you do this, you don't need to worry about getting the command-line java options correct.

# Note Unix conventions: `:` separates elements, `/` between path components
ENV CLASSPATH .:./mysql-connector-java-5.1.36.jar

CMD ["java", "API"]
docker run -d -p 8080:8080 my-image # runs the default CMD
docker run --rm -it my-image \
  java CLI                          # runs this command instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM