0

Like the title says, I need to find the directory/path of a file. I know that the computer running the program has the file but I don't know the path/directory. The only thing I know about the file is its name(file.jar).

File f = new File("file.jar"); String ap = f.getAbsolutePath(); return ap; 

I have already tried using getAbsolutePath() method and it didn't work so I was wondering if there was another way to get the directory/path of file.jar.

9
  • Did you try this: mkyong.com/java/how-to-get-the-filepath-of-a-file-in-java ? Commented Jul 17, 2014 at 15:42
  • Try file.getParent() (or file.getParentFile()) Commented Jul 17, 2014 at 15:42
  • possible duplicate of How do I get a file's directory using the File object? Commented Jul 17, 2014 at 15:45
  • @ayushi .getParent() and getParentFile() just return null when I use them. Commented Jul 17, 2014 at 15:54
  • 1
    @ayushi That's a relative path, and OP can't give the absolute path, or the relative path, because s/he doesn't know it. Commented Jul 17, 2014 at 16:19

1 Answer 1

2

The argument given to the File-constructor is a path relative to the working directory (as long as you don't pass an absolute path). The constructor won't search for this file but just tries to open a file with that name in your working directory.

This might help if you want to search for files: http://docs.oracle.com/javase/tutorial/essential/io/find.html

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.