enter code herei have my webapplication and h2database as a docker containers. I have docker compose yml file all set. Now, i need to initialize this database ( h2database). But i am unable to do so. When i go inside h2database docker container, i see docker-entrypoint-initdb.d/initdb.sh inside this initdb.sh i have added my script
#!/bin/bash
java -cp h2-1.4.190.jar org.h2.tools.RunScript \
-script /App1/BasePl/sql-scripts/h2/common-scripts/BasePl-schema-h2.sql \ -url "jdbc:h2:http://localhost/code1/Demo2./BasePl"
but when i try to execute this initdb.sh using bash command it gives me error : Could not find or load main class org.h2.tools.RunScript
I have tried all possible class path combinations and also downloaded different h2 jarfiles zip folders and also checked for RunScript file under h2\src\main\org\h2\tools and its there. So i do not understand what is the issue here.
Error: Could not find or load main class org.h2.tools.RunScript
my Docker-compose.yml file:
version: '3'
services:
baseplapplication:
build:
context: .
dockerfile: Dockerfile
environment:
BASEPL_HOME: "/usr/local/BASEPL"
ports:
- "882:8080"
volumes:
- basepl_data:/var/bio
depends_on:
- db
links:
- db
db:
image: "buildo/h2database"
volumes:
- ./dumps/init.sh:/docker-entrypoint-initdb.d/initdb.sh
- h2data:/var/lib/h2database
ports:
- "8082:8082"
volumes:
basepl_data:
h2data:
now the initdb will be created when i run docker-compose command and thus i need to go inside the database(db) container to the location of initdb.sh and then put my script #!/bin/bash java -cp h2-1.4.190.jar org.h2.tools.RunScript \ -script /App1/BasePl/sql-scripts/h2/common-scripts/BasePl-schema-h2.sql \ -url "jdbc:h2:http://localhost/code1/Demo2./BasePl" . How could i then put my h2-1.4.190.jar file which is located on my desktop to that location inside the database(db) container. All i want is to populate (initialize) the docker database by running a script against it during docker compose. Please i am new to docker so help me
This error means java did not find h2-1.4.190.jar.
First, you need to put h2-1.4.190.jar in the same folder of initdb.sh, that is docker-entrypoint-initdb.d in your case. Then, in your Dockerfile, the WORKDIR I guess is not docker-entrypoint-initdb.d.
Then, when do java -cp h2-1.4.190.jar, it intends to find the jar in WORKDIR, not in docker-entrypoint-initdb.d. E.g. if WORKDIR is /root/, then it will try to find h2-1.4.190.jar in /root, not in the folder store the script, so you can not find the jar.
To overcome this, you can change WORKDIR in dockerfile, or just add next to init.db.sh:
#!/bin/bash
dir=$(dirname "$0")
java -cp "$dir/h2-1.4.190.jar" org.h2.tools.RunScript \
-script /App1/BasePl/sql-scripts/h2/common-scripts/BasePl-schema-h2.sql \ -url "jdbc:h2:http://localhost/code1/Demo2./BasePl"
The dir=$(dirname "$0") will assure it's the same folder of current running script, not the folder where to start the script.
Also, you can use absolute dir for h2-1.4.190.jar, it's also ok.