-1

I am trying to access .doc a file from my project directory. I am getting an error when I use a relative path in windows. but it works fine when I get an absolute path.

File initialFile = new File("D:\\Demo\\src\\test\\java\\com\\pro\\mockfiles\\My-DOC-FILE.doc");
InputStream uploadStream = new FileInputStream(initialFile);

works fine but,

File initialFile = new File("test/java/com/pro/mockfiles/My-DOC-FILE.doc");
InputStream uploadStream = new FileInputStream(initialFile);

gives the following error

java.io.FileNotFoundException: test\java\com\pro\mockfiles\My-DOC-FILE.doc (The system cannot find the path specified)

I want to run with a relative path, can you help?

Netra
  • 23
  • 10
  • Java works with relative paths; check the CWD! – MrTux Dec 03 '18 at 07:08
  • 2
    You may use **File.separatorChar** instead of "/" or "\" – BishalG Dec 03 '18 at 07:09
  • 2
    relative to what? where are your class files? – Scary Wombat Dec 03 '18 at 07:11
  • relative path to my doc file which is in a separate directory in my project @ScaryWombat – Netra Dec 03 '18 at 07:15
  • can you please explain more about File.separatorChar @BishalGautam – Netra Dec 03 '18 at 07:16
  • Thanks, I already know that, but I am still getting this error, I want to know the root cause. @MrTux – Netra Dec 03 '18 at 07:18
  • You may read about that here: https://www.journaldev.com/851/java-file-separator-separatorchar-pathseparator-pathseparatorchar – BishalG Dec 03 '18 at 07:18
  • You make a crucial mistake in thinking. A relative path is always relative from the working directory. That is the directory in which the application is executed, not the one the application is stored. So to figure out what relative path to use you will first have to figure out what the current working directory is. You can do that by executing: Paths.get("").toAbsolutePath().toString() – Gerben Jongerius Dec 03 '18 at 07:27

3 Answers3

2

Event though relative paths may work in Java, I personally would recommend against using them. The mean reason being that the 'relative' path to a file changes depending on the working directory of Java. If you start the application from 'c:\tmp' then the relative path would be different then when you start it from 'c:\test'. Even if the application is located in 'c:\myApp'.

But if you want to use it first figure out where your applications was started from (called the working directory) by executing the code below:

 System.out.println(Paths.get("").toAbsolutePath().toString());

After this change your relative path according to the difference between the working directory and the file you want.

As a side note I noticed the file is stored in the classpath. So you may want to look into the ClassLoader to acces the file rather then using the file system. You can do that (in case the code is executed in a test) by the following code:

MyClass.class.getClassLoader().getResourceAsStream("/com/pro/mockfiles/My-DOC-FILE.doc");

This code is more robust as it will work no matter where the application is started from.

Gerben Jongerius
  • 719
  • 4
  • 13
0

It seems that you are running your application on any server.
So when you run it on server, application will take path relative to that server's root directory (May be /bin or something else depending on your server).
When you run it as standalone java program it take path relative to where standalone program is executing.
So if you want to run it on server give path relative to server's root directory.
Like:-
File initialFile = new File("<Path after Server root>/test/java/com/pro/mockfiles/My-DOC-FILE.doc");

<Server root> can be a path after C:/Myserver/bin

Tarun
  • 986
  • 6
  • 19
0

Here is an other solution, how to get the directory, the application was started in:

String current_dir = System.getProperty("user.dir");

After this the absolute path to the .doc can be built using the current working directory:

Paths.get(current_dir, "test/java/com/pro/mockfiles/My-DOC-FILE.doc");

with Paths.get(...)

awgold90
  • 72
  • 13