-1

I wrote a program in Java and packaged it up as a Mac App using Netbeans. It has the typical Java, MacOS, Resources, and Plugins folders and works just fine. The problem is that my program has a save feature which, whenever used, saves files to the computer's User directory instead of the directory in which the program itself is running—the Java folder.

In the past I got around this with a custom executable containing the lines

# Set the working directory
DIR=$(cd "$(dirname "$0")"; pwd)
cd $DIR

But since Netbeans creates the executable now and encodes it in some unreadable way, that's not an option. I would use the old one but Netbeans adds the benefit of bundling a JRE with the program so I'm stuck having to choose between saving in the right spot or bundling the JRE.

Does anyone know how to change the working directory of a packaged program?

•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

EDIT:

I took Tyler's advice and decided to find the directory I wanted within the program itself instead of trying to change the PWD with scripts.

Say you're running a Jar file by double clicking it or using terminal/cmd (This only works by directly running, not through ANT or an IDE). This will give you the directory that your Jar is in:

String path1 = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String path2 = (new File(path1)).getParentFile().getPath();
String PWD = URLDecoder.decode(path2, "UTF-8");
  • The first line gets the path to your Jar file.
  • The second line removes the name of the Jar from the end, leaving you with the directory it's in.
  • The third line accounts for special characters like spaces.
corpico
  • 617
  • 3
  • 16
  • 26
  • You shouldn't rely on the current directory for figuring out where to save a file. Use an environment variable or other configuration option. – T.D. Smith Jun 22 '15 at 14:59
  • Can you provide an example? I'm not well versed in this sort of thing. – corpico Jun 22 '15 at 15:33

1 Answers1

2

I'd use something like

System.getenv("HOME");

to find the user's home directory instead of changing Java's working directory.

T.D. Smith
  • 984
  • 2
  • 7
  • 22
  • Note that this should work on a Mac. For other platforms, you can run the shell command `set` to see what environment variables are available. You can also consider adding a new environment variable in your installation process if one doesn't exist that meets your needs. – T.D. Smith Jun 22 '15 at 16:00
  • Sorry, I reread my post. It wasn't too clear. What I'd like to do is save files to the directory in which my jar file is running instead of the user's home directory. So once it's packaged up, I'd like to save to the "Java" folder of the App. Is there a way to do that? – corpico Jun 22 '15 at 18:06
  • Ah, gotcha. It might not be exactly what you need, but [this thread](https://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java) looks pretty close. – T.D. Smith Jun 22 '15 at 18:38
  • Thanks for the lead. It makes sense that simply finding the directory I want is more appropriate than changing the program's working directory, which could cause other problems. I'll update my question with what I found. – corpico Jun 22 '15 at 19:36