0

I'm writing a program that has a JFileChooser.

I want default location opened in it to be the folder in which (once the program is created) programs (runnable) .jar file is residing.

Is there a simple way to locate the program inside a file system, or do I have to do it manually?

Also, if there is a simple way, what will be default location while my program is still just a project (not jar)?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • http://stackoverflow.com/questions/3153337/how-do-i-get-my-current-working-directory-in-java – pb2q Sep 11 '12 at 21:31
  • try to use a xml/properties file that will hold the default location and load it in the filechooser during its initialisation – Jayant Varshney Sep 11 '12 at 22:30

1 Answers1

2

To find the executing location:

public static void main(String[] args) {
  new Test();
}

public Test() {
  JFileChooser jfc = new JFileChooser();
  File loc = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
  jfc.setCurrentDirectory(loc);
  jfc.showOpenDialog(null);
}

When you're in the project, the directory will be where your class files are located. For example, project_dir/bin/.

martinez314
  • 12,162
  • 5
  • 36
  • 63