Question

I want to run a java program in Linux and change its working directory to a specific location. I'm a bit of a noob with Linux, so I need quite a bit of help. I managed to figure this much out:

java -jar program.jar

That will run the program in the working directory it chooses. Now I need to find a way to change the working directory. I think this computer is running UBuntu (or however you spell it) but I'm not sure. I'm also running on an account that has severely limited privileges.

Was it helpful?

Solution

EDIT (based on info given in asker's comment below):

So, it seems like you just need a batch script to cd into its own directory before launching java.

Something like this script should do it.

#!/bin/bash
DIR=`dirname "$BASH_SOURCE[0]"`
cd "$DIR"
echo "Current dir: $DIR"
javaw -jar program.jar &

So, make sure it's executable (chmod +x minecraft.sh), then when you double-click it or run the script from any folder, it will treat the script's folder as its working directory. More information can be found in SO questions like this one.

I'm not going to install Minecraft, otherwise my life will disappear down a dark gaming hole ;)

Original answer:

say for desired working directory /x/y/z, and program.jar is in directory /a/b/c

cd /x/y/z
java -jar /a/b/c/program.jar
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top