Java relative paths: trick or treat?


We all had problems with relative paths in Java! Do not deny it!

Every time I have to understand where the path is relative to is a puddle of blood, so how can we unveil this mystery?
  • Will it be relative to the classpath?
  • Will it be related to the project root?
  • And in the tests?
I usually use a little trick: in the point of the code where I want to use the relative path I write/log
Java: new File(".").getAbsolutePath()
Kotlin: File(".").AbsolutePath
Simple and fast, at this point we know exactly where is the starting point for our relative path!

Comments

Popular posts from this blog

"Back to values" agile retrospective format

Contracts: diagonal saber vs. agile

ScuolaXP 2017 - Com'è andata?