0

I have workspace where I save all my java projects worked in Eclipse. Let's call it D:\Workspace\. I have a project in said workspace which route lets say is: D:\Workspace\DoubleDotting\.

In said Java project, in the main method, I create a file object in the following way:

File doubleDotted = new File("..\Folder1\file.xyz");

At the moment of exporting the program as a runnable Jar into D:\Projects\DoubleDotting\Folder2\, the program executes just fine, for D:\Projects\DoubleDotting\Folder1\file.xyz exists.

However file.xyz does not exist in D:\Workspace\DoubleDotting\Folder1\, so naturally when running/debugging the program from Eclipse, I get a FileNotFoundException.

Working with relative paths, is an amazing time-saver when working on generic programs that can be applied to multiple directories with a similar working structure, however it's a headache to debug within Eclipse.

Is there a way to change a project's working directory virtually within Eclipse or is the lazy option of creating such folders/files within the Project directory, the only solution to running/debugging within eclipse?

Ren
  • 4,594
  • 9
  • 33
  • 61
  • 2
    You can set the working directory in the launch configuration (which is by default the project directory): https://stackoverflow.com/a/329141/6505250 – howlger Aug 23 '17 at 22:12

1 Answers1

0

Lets be clever on this. If you would only create simple class in root of your project, you could simply see where .(current directory) is pointing to. Eg

public static void main(String[] args) {
    System.out.println(new File(".").getAbsolutePath());
}

This will show you where current directory from runtime point of view is located. Build your relative path on top of that.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Indeed. I was working with a similar workaround: `.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()`, however, I think your solution might be smarter. – Ren Aug 23 '17 at 20:51