1

I am using a Mac and I am new to it. Here is my question:

There is a folder that I require to include as a File object in Java. When I try this:

File firefoxProfileFolder = new File("/Users/prime/work/dmall/selenium/src/test/resources/firefoxprofile");

It works fine. This code is located in file: /Users/prime/work/dmall/selenium/src/test/java/com/dmall/utils/WebUtil.java

But when I try this:

File firefoxProfileFolder = new File("../../../../resources/firefoxprofile");

I can not load the folder. So the relative path from this file to that folder seems not to work. So what should I do? What is it I am doing wrong?

I require to use the relative path because this code will be run from the server, on which I have no idea what the absoulte path will be.

Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • 3
    That `.java` file is going to be compiled to a `.class` and run from somewhere else. – S.D. Jan 28 '13 at 08:01
  • Please post a rough diagram of the directory structure. – TechSpellBound Jan 28 '13 at 08:01
  • @Singularity So what should I do? – Koray Tugay Jan 28 '13 at 08:02
  • You can not obtain the resource simply based on the package hierarchy of your Java source file, you can get resource at runtime via Classloader though. see this: http://stackoverflow.com/questions/9111484/usage-of-thread-currentthread-getcontextclassloader-getresourceasstreamsys – Gavin Xiong Jan 28 '13 at 08:04
  • @GavinXiong But this is not a property file it is a folder? – Koray Tugay Jan 28 '13 at 08:04
  • @KorayTugay When a java app is launched the command `java ` is run, the current directory of that command shell will be `./` for the app process. But still, hard-coding relative paths is no good. – S.D. Jan 28 '13 at 08:07

4 Answers4

3

It is most likely, that you have made a mistake in relative path. It should work.

For checking this, you can create new test directory using new File('testDir').mkdirs() and see - where Java will create this directory. Will it be in the expected place or somewhere else?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
3

You could use firefoxProfileFolder.getCanonicalPath() and check if it is the same path as String s = new File("/Users/prime/work/dmall/selenium/src/test/resources/firefoxprofile").getCanonicalPath();
If it isn't the same path, your relative path is false...

redc0w
  • 101
  • 4
2

You actually need to use directory path considering current directory of the launcher script. I.e. your root directory is this script's directory.

Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34
2

You can obtain the directory at runtime:

URL url = ClassLoader.getSystemResource("relative to/classpath/resources/firefoxprofile");
File file = new File(url.getFile()); // the directory
....    
Gavin Xiong
  • 957
  • 6
  • 9
  • I do not need a txt file, I need the folder itself. – Koray Tugay Jan 28 '13 at 08:10
  • Thank you. It is clear. Should I really use the string relative to/classpath/resources... as the parameter to the method? It did not work.. – Koray Tugay Jan 28 '13 at 08:14
  • @KorayTugay No, it's just an example, depending on your package structure. It is noticeable that if you are using maven, the src/test/resources folder will not be packaged at runtime conventionally, you should put your resources into src/main/resources. – Gavin Xiong Jan 28 '13 at 08:19