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?

Mario_2207
  • 107
  • 1
  • 7

3 Answers3

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());
        }
    }
lkq
  • 2,326
  • 1
  • 12
  • 22
  • 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 Mar 17 '16 at 18:51
  • @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. – lkq Mar 17 '16 at 18:55
  • 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 Mar 17 '16 at 18:59
  • @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. – lkq Mar 17 '16 at 19:12
  • 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. – Mario_2207 Mar 17 '16 at 19:52
  • @Mario_2207 See my update. The code you gave me works in Eclipse. – lkq Mar 17 '16 at 20:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106652/discussion-between-keqiang-li-and-mario-2207). – lkq Mar 17 '16 at 20:20
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...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
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;
    }
Milind Gokhale
  • 575
  • 2
  • 14
  • Where does Review come from? Do I need to import any library to resolve it? – Mario_2207 Mar 17 '16 at 18:55
  • 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();` – Milind Gokhale Mar 17 '16 at 18:58
  • 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. – Mario_2207 Mar 17 '16 at 19:04
  • 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: http://stackoverflow.com/a/10947795/3973420 – Milind Gokhale Mar 17 '16 at 19:17