0

I have a Java 8 application running on Windows. I would like to locate a file in my Java project without using the absolute path. The following code works when I run my application from a command prompt: System.getProperty("user.dir"). However, when I run my java application from a batch file, this code return the path of the batch file, which is not the same as the path of the java application.

How do I get the path of file abc.txt that is located in a folder in my Java project?

3 Answers 3

1

System.getProperty("user.dir") got you the right directory because your application was started in the batch file. It's the way it works.

You can always load your files in your application by using a ClassLoader.

Say you have put abc.txt inside your project folder. Actually, if you're using it somewhere in your code as a resource file, you should put it in src/resources folder of your project. Then in your code, do things as below:

   File file = new File(getClass().getResource("abc.txt").getFile());

That will give you access to the file no matter where your application starts.

Taking a look at this post on using ClassLoader will help you get a better sense on how to load resources in Java.

UPDATE

Here is my project in Eclipse, abc.txt is under src folder, which is in the class path.

The MyClass.java only contains below code:

    public class MyClass {
        public static void main(String[] args) {
            System.out.println("s: " + MyClass.class.getClassLoader().getResource("abc.txt").getFile());
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your reply. I am not asking why running the application from a batch file returns the path of the batch file. The question was how to get the path of a file in my Java project. Moving the file to the Src\Resources folder is not an option. Thanks anyway.
@Mario_2207 yes, that's exactly what my answer was about. Moving the file is not a must, since that line of code would still work, and then you just call file.getPath() and you get the path.
My bad. I misunderstood you. I see. As long as I get the path of the Resources folder, then I could go back from there to my custom folder that contains my file. I just tried it, but my code can't resolve getClass(): Cannot make a static reference to the non-static method getClass() from the type Object. I'm using eclipse Mars.
@Mario_2207 You're getting the path of the file that you specified its name, not the resource folder. You're calling the getClass() from main() method, is that the case? Say your class name is 'TestClass', then you can just use TestClass.class.getResource("abc.txt"). Just make sure these files are in the class path.
Here's my code: public class MyClass { public static void main(String[] args) { System.out.println("s: " + MyClass.class.getClassLoader().getResource("abc.txt").getFile()); } } This compiles and runs but it is coming back with NullPointerException. I even copied my file to the src\Resources\ folder for now. I tried MyClass.class but class wasn't recognized.
|
0

I had the same problem and the only work around I found after searching A LOT was locating the starter Batch file in the same path as the java application, then generate a shortcut of the batch and this is what I could place anywhere in other folders....

After that, the java was running fine...

1 Comment

I like your workaround, however I can't use it. Thanks!
0

Assuming that you basically want to have relative paths in your code, You can use the below function to get the source directory and then get any file using the relative location of the file within your source code path.

public static String getMySourcePath() {
        URL location = <Your class>.class.getProtectionDomain().getCodeSource()
                .getLocation();
        String srcPath = location.toString().replace("file:/", "")
                .replace("bin", "src");
        return srcPath;
    }

4 Comments

Where does Review come from? Do I need to import any library to resolve it?
Here Review was a class in my project. You can use any of your class. For example, if you're working in a project with a class called MyTest.java then you will need to write MyTest.class.getProtectionDomain().getCodeSource().getLocation();
But my class doesn't have a static method called getClass(). It didn't work, and I didn't expect it to. Please note that I'm using this code in a class that doesn't contain the "main" method.
Don't use getClass() method, simply Write, MyTest.class and it will give you a class of type MyTest. This is java's inherent property for any class. This answer will help clarification: stackoverflow.com/a/10947795/3973420

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.