7

I have created a java application for "Debian Linux." Now I want that that application reads a file placed in the directory where the jar file of that application is specified. So what to specify at the argument of the File Object?

File fileToBeReaded = new File(...);

What to specify as argument for the above statement to specify relative filepath representing the path where the jar file of the application has been placed?

Amit
  • 33,847
  • 91
  • 226
  • 299

6 Answers6

9

Are you asking about escape character issues?

If that is the case then use forward slashes instead of backward slashes like

"C:/Users/You/Desktop/test.txt"

instead of

"C:\Users\You\Desktop\test.txt"

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Brendan
  • 91
  • 1
  • 1
8

If you know the name of the file, of course it's simply

new File("./myFileName")

If you don't know the name, you can use the File object's list() method to get a list of files in the current directory, and then pick the one you want.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
5

Using relative paths in java.io.File is fully dependent on the current working directory. This differs with the way you execute the JAR. If you're for example in /foo and you execute the JAR by java -jar /bar/jar/Bar.jar then the working directory is still /foo. But if you cd to /bar/jar and execute java -jar Bar.jar then the working directory is /bar/jar.

If you want the root path where the JAR is located, one of the ways would be:

File root = new File(Thread.currentThread().getContextClassLoader().getResource("").toURI());

This returns the root path of the JAR file (i.o.w. the classpath root). If you place your resource relative to the classpath root, you can access it as follows:

File resource = new File(root, "filename.ext");

Alternatively you can also just use:

File resource = new File(Thread.currentThread().getContextClassLoader().getResource("filename.ext").toURI());
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Can you please suggest with the .war rather than the jar. I am finding difficulty to get the file path of an image file when the app is deployed on a jboss server. I have raised separately a question for this - https://stackoverflow.com/questions/54161215/how-to-refer-to-a-file-present-within-src-main-resources-folder-deployed-in-a-jb/54161697?noredirect=1#comment95158413_54161697 – Farhan stands with Palestine Jan 13 '19 at 00:09
  • 1
    Hey @Shirgill :) Depending on server type, WAR deployment usually loads individual files into memory not into disk. Therefore `java.io.File` referencing a WAR-packaged file won't always work. Before giving an answer, you'd better first tell what exactly you need this `File` for and why `InputStream` is not sufficient. E.g. perhaps you need this for some 3rd party API which stubbornly only accepts `File` and not `InputStream`, such as iText PDF. – BalusC Jan 14 '19 at 10:04
  • Yes, while constructing the PDF file, I need to create an image object `Image logo = Image.getInstance(path)` exposed by iText PDF. I see that there is an overloaded method which takes in an `Inputstream` object. But other requirement is related to loading a font file(Arabic) `BaseFont baseNormalFont = BaseFont.createFont(PATH, BaseFont.IDENTITY_H, true);` I need to give the path of the same file. For the time being, I have put the file under a separate configuration folder in linux , and then gave the path as `System.getProperty(".....path to the conf folder") + further path to font file`. – Farhan stands with Palestine Jan 14 '19 at 11:43
  • The above approach works well but is the same not achievable if the file is part of the maven project. Please suggest. Btw, how did you arrive specifically at iText. I see that I haven't mentioned it anywhere ::)) – Farhan stands with Palestine Jan 14 '19 at 11:47
  • 1
    @Shirgill: because this was asked before :) https://stackoverflow.com/q/32251711 Your answer is also there and most likely option 4 is the best for you. – BalusC Jan 14 '19 at 15:41
  • That is stunningly informative. Thanks for your generosity. – Farhan stands with Palestine Jan 15 '19 at 09:04
3

I think this should do the trick:

File starting = new File(System.getProperty("user.dir"));
File fileToBeRead = new File(starting,"my_file.txt");

This way, the file will be searched in the user.dir property, which will be your app's working directory.

Geo
  • 93,257
  • 117
  • 344
  • 520
2

You could ask your classloader to give you the location of the jar:

getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

...but I'd suggest to put the file you are looking for inside your jar file and read it as a resource (getClass().getResourceAsStream( "myFile.txt" )).

tangens
  • 39,095
  • 19
  • 120
  • 139
1

On IntelliJIDEA right click on the file then copy the absolute path, then in the double quotation paste the path as filePath. for example it should be something like this:

"C:\\Users\\NameOfTheComputerUser\\IdeaProjects\\NameOfTheProject\\YourSubFolders\\name-of-the-file.example"