2

I am trying to read the contents of a file using Java.nio.file.Paths class, my code looks like this

package com.test.json;

Path currentDir = Paths.get(".");
System.out.println(currentDir.toAbsolutePath());

It is giving me the path

/home/rohit/workspace/MapReduceExample/.

while the output should be

home/rohit/workspace/MapReduceExample/src/com/test/json/

It is ignoring the component of src folder and packages. Could someone please tell me what I am doing wrong?

I can't give absolute path because I need this code for a map-reduce path, I have to construct path in relative manner. So, my approach is to do

getCurrentDirectoryPath + filename
Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
Max
  • 9,100
  • 25
  • 72
  • 109
  • 2
    You're asking for the current runtime path, and that's what you're getting. If you're looking for code to figure out the location of a given class, trying looking at [this question](http://stackoverflow.com/questions/11747833/getting-filesystem-path-of-class-being-executed) – azurefrog Feb 28 '16 at 18:46
  • @azurefrog should I move the data out of src folder then? – Max Feb 28 '16 at 18:49
  • IMHO, yes. I consider the `src` hierarchy to be only for source code, but that's party because I'm used to using maven, and it has a separate `resources` directory for things like config files and other non-code resources. – azurefrog Feb 28 '16 at 18:56

1 Answers1

0

It appears to me that you are expecting the Path of the directory where your source Java file (that calls Paths.get(".")) resides. But that's not what the path "." will fetch. When a JVM runs your class on the host file system, the value of "." refers to the current working directory of the JVM process. It's very likely that the JVM that runs your class is actually started in that folder: /home/rohit/workspace/MapReduceExample. If you did a

System.out.println(Paths.get(
   System.getProperty("user.dir")).toAbsolutePath());

you'll see that it prints the same folder, without the trailing ".".

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34