0

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.

5
  • possible duplicate of Changing the current working directory in Java? Commented Apr 26, 2012 at 19:27
  • The link talks about a running java program not being able to change its own working directory. So far it's looking like this may be about choosing the directory at the time of startup, which may be different. Commented Apr 26, 2012 at 19:32
  • @ChrisStratton: and change its working directory - this question is about changing its own working directory too. Commented May 14, 2012 at 5:49
  • @userunknown - no, it's not about a java program needing to change it's own working directory, but simply about needing to change the directory in which the java program will work, at the time of startup - see the comment Big Endian made to amir's answer below. Commented May 14, 2012 at 5:56
  • @ChrisStratton: Yes. BigEndian is totally confused about the question - see his first comment there too. See the reason why the question is tagged Java. It has nothing to do with Java, imho, and guess you agree. Commented May 14, 2012 at 12:00

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

6 Comments

But I believe the program changes its working directory away from the directory it is in.
java just uses the current working directory of the calling process (i.e. the terminal), so if you cd, then it'll use that folder. If you think you're experiencing anything different then I think you need to give more details.
Yeah, it's the Minecraft game minecraft.jar file. If you have minecraft you can try to help me. A friend of mine was able to change the working directory using a batch file on windows so that minecraft would save the worlds on a thumb drive where the program and batch file were residing. I'm trying to do the same on Linux.
haha, ok. Now I understand what you're after, i.e. a bash script which begins by cding into its own folder, regardless of where it was invoked from. I'll edit my answer accordingly.
At least, I think that's what you're after!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.