Greenbox
Greenbox

Reputation: 113

Execute Java Jar using shell script in Docker container

I want to execute a java jar with the help of shell script in docker container. My java program simply accepts command line argument and print the message along with the malue passed as command line argument.

class SampleJar {
   public static void main(String [] args) {
      String msg = args[0];
      System.out.println("Hello " + msg);
   }
}

I build the jar and jar is working fine. java -jar SampleJar.jar John o/p: Hello John.

Inside my working directory there is Dockerfile and scripts folder. Script folder having 3 files:

  1. executeJar.sh
  2. execute.sh
  3. SampleJar.jar

My Dockerfile content:

FROM ubuntu
WORKDIR /opt
#COPY execute.sh .
#COPY executejar.sh .
#COPY SampleJar.jar .
COPY scripts /
ENTRYPOINT ["/execute.sh"]
CMD ["alpha"]

executeJar.sh content:

#!/bin/bash
echo "This is from execute jar . sh"
java -cp SampleJar.jar SampleJar John

execute.sh content:

#!/bin/bash
echo "This is from execute.sh"

case "$1" in
        alpha)
                echo "executing alpha script"
                /executejar.sh "@"
        ;;
esac

The jar file is working fine tested independently.

I build the image using command: docker build -t exp . exp is the name of the image & . indicates the Dockerfile present in current directory

I create container docker run -it --name exp-container exp:latest alpha

-it for interactive mode exp-container is the name of the container

So execute.sh will be executed and case alpha will be executed and executeJar.sh will invoke. Now executejar.sh should execute the java jar. So the final output comes as

docker run -it --name exp-container2 exp:latest alpha
This is from execute.sh
executing alpha script
This is from execute jar . sh
/executejar.sh: line 3: java: command not found

How can I get the ouput of the jar?

Upvotes: 3

Views: 3947

Answers (1)

skeeks
skeeks

Reputation: 379

The plain ubuntu docker container (FROM ubuntu) does not include the java runtime environment.

You have the following options:

I would prefer the second option unless you really need ubuntu as your base container. See also the docker hub page for openjkd, which has several other variantes available, e.g. opnejdk

Upvotes: 1

Related Questions