Bash script to start java maven project

Issue

I have a problem. I am new with bash scripts, but I am trying to make a startup script for my java maven project. When I want to start the program I run the following command in a terminal:
cd /var/script/myproject/java && mvn exec:java

Now I am trying to translate this to bash, but I can’t get it to work. First I tried the following line:

exec cd /var/script/myproject/java && mvn exec:java

But this resulted in the error:

/var/script/myproject/java_toggle.sh: line 6: exec: cd: not found

Then I saw this site: https://www.baeldung.com/linux/cd-command-bash-script
where I saw that they used the cd command seperatly, so I tried the following:

cd /var/script/myproject/java 
exec mvn exec:java

But this doesn’t seem to change the directory of the exec, because it said that there was no maven project. How can I run mvn exec:java in a different directory as the bash script? Please let me know!

Solution

cd is internal bash command, and "exec" trying to execute it by itself instead of bash, but there is no /bin/cd (and there is no sense in such tool)

So possibly you need to

exec "/bin/bash -c 'cd /var/script/myproject/java && mvn exec:java'"

Answered By – Saboteur

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published