2

I am trying to write a Bash script to (amongst other things) run a particular Java programme stored in a .jar file.

The script is in a directory we will call foo. It includes this line

java -jar ~/Simutrans-Extended/simutrans-extended/Nightly-Updater-V2.jar -cl

When I run the script, the .jar file executes as though it were running in foo. But I need it to be executed in its actual location, in this case ~/Simutrans-Extended/simutrans-extended/. How do I do this please?

The script is running on Ubuntu 20.04, should that be relevant.

1 Answer 1

2

Use cd to change the directory in the line before the command. So the Bash script now reads:

cd ~/Simutrans-Extended/simutrans-extended/ && java -jar ./Nightly-Updater-V2.jar -cl
5
  • 3
    I suggest to use cd ... && java ... or cd ... || exit 1 to ensure that your java program is only called if no problem occurred when switching to destination directory.
    – Cyrus
    Jan 10, 2021 at 8:30
  • 1
    I am using Windows, and if I want to run a .jar file, the commands I use will almost exactly the same syntax as the above mentioned commands, just all slashes will be backslashes, the commands seem to be universal... Jan 10, 2021 at 13:47
  • @Cyrus Thank you, that is a really helpful tip! I have added it to the answer.
    – Matthew
    Jan 12, 2021 at 13:03
  • @XeнεiΞэnвϵς I am learning Linux from a Windows background too. While I agree they are conveniently similar, I believe there are two important differences. Windows does not accept ~ to refer to the user's home directory and the . is not necessary to refer to the present working directory. So it's best to keep this answer as Linux-only.
    – Matthew
    Jan 12, 2021 at 13:05
  • How did you managed to post the Q and the A at ~~.~~.37Z ? Surely answer own question? Nov 9, 2021 at 6:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .