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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Void Star
  • 2,401
  • 4
  • 32
  • 57
  • possible duplicate of [Changing the current working directory in Java?](http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java) – home Apr 26 '12 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. – Chris Stratton Apr 26 '12 at 19:32
  • @ChrisStratton: `and change its working directory` - this question is about changing its own working directory too. – user unknown May 14 '12 at 05: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. – Chris Stratton May 14 '12 at 05: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. – user unknown May 14 '12 at 12:00

1 Answers1

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
Community
  • 1
  • 1
laher
  • 8,860
  • 3
  • 29
  • 39
  • But I believe the program changes its working directory away from the directory it is in. – Void Star Apr 26 '12 at 19:32
  • 5
    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. – laher Apr 26 '12 at 19:38
  • 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. – Void Star May 14 '12 at 01:32
  • haha, ok. Now I understand what you're after, i.e. a bash script which begins by `cd`ing into its own folder, regardless of where it was invoked from. I'll edit my answer accordingly. – laher May 14 '12 at 02:17
  • At least, I think that's what you're after! – laher May 14 '12 at 02:48
  • Hey thanks! I'll try this as soon as I'm back on that computer. Marking this as the answer for now. – Void Star May 26 '12 at 01:01