0

I have implemented a Java class that handles files of .txt-Format by relative paths. It works all well during execution in Eclipse. But if I’m executing the (unchanged) compiled file in the Java JRE outside of Eclipse, I’m getting a java.io.FileNotFoundException.

I implemented new File(".").getCanonicalPath() and found the source of my problem:

Executed in Eclipse I get returned: C:\Users\xy\git\Project\Project

Executed in Java 8 JRE I get returned: C:\Users\xy\git\Project\Project\bin

I thought of a workaround, but handling two different code versions for development and deployment seems an unnecessary source of errors to me.

So my question is how to fix the problem. I would prefer a hint how to reconfigure Eclipse.

Thanks in advance Threx

Threx
  • 73
  • 1
  • 2
  • 9
  • Please post your minimum working code. – Aman Aug 16 '17 at 06:02
  • Please use this link: https://stackoverflow.com/questions/1099300/whats-the-difference-between-getpath-getabsolutepath-and-getcanonicalpath – Aman Aug 16 '17 at 06:13

2 Answers2

0

There are many ways:

  1. do not put your text file in the /bin, point to another more static path
  2. load the file into classpath
  3. use combination of System.getProperties("user.dir") and if-else statement
  4. use maven, ref: Maven add properties file to jar file
Irwan Hendra
  • 150
  • 10
0

The solution lies in adapting the configuration of Eclipse.

Under Run | Run Configurations… | slide Arguments there is the entry "Working directory" . By default it is set to ${workspace_loc: ProjectName}. After changing it to ${workspace_loc:ProjectName/bin} the java.io.FileNotFoundException-warnings are gone and everything works as intended.

Now Eclipse and the JRE outside of Eclipse work with the same path and no more redundant code with workarounds is needed.

Threx
  • 73
  • 1
  • 2
  • 9