Reputation: 23780
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.
Upvotes: 1
Views: 6223
Reputation: 101
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...
Upvotes: 3
Reputation: 977
You can obtain the directory at runtime:
URL url = ClassLoader.getSystemResource("relative to/classpath/resources/firefoxprofile");
File file = new File(url.getFile()); // the directory
....
Upvotes: 2
Reputation: 7945
You actually need to use directory path considering current directory of the launcher script. I.e. your root directory is this script's directory.
Upvotes: 2
Reputation: 34900
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?
Upvotes: 3