10

I have the following structure for my project.

In Eclipse:

myPorjectName
  src
    com.example.myproject
        a.java
    com.example.myproject.data
        b.xml

In a.java, I want to read b.xml file. How can I do that? Specifically, in a.java, I used the following code:

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("data/b.xml"));

This code cannot find b.xml. However, if I change the path to src/com/example/myproject/data/b.xml then it works. The current location seems to be in the root of my project file.

But I see other people's examples, if b.xml and a.java are in the same folder, then we can directly use new File("b.xml"). But I try putting b.xml in the same folder of a.java rather than putting in the sub folder, but it still does not work. If this works, then in my case, I should be able to use new File("data/b.xml"), right? I really do not understand why this is not working.

Lii
  • 11,553
  • 8
  • 64
  • 88
Joey
  • 2,732
  • 11
  • 43
  • 63
  • 2
    It's most likely going to be more like `getClass().getResourceAsInputStream("/com/example/myproject/data/b.xml");` – MadProgrammer May 01 '13 at 05:38
  • It's not working because your **default directory (that java is executing from)** is `myPorjectName` it's not in `myproject` – xagyg May 01 '13 at 05:43
  • xagyg and MadProgrammer 's comments make me understand why I was doing it wrong. Thank you guys. The following answer makes me learn something more. – Joey May 01 '13 at 06:17

1 Answers1

22

If it is already in the classpath and in the same package, use

URL url = getClass().getResource("b.xml");
File file = new File(url.getPath());

OR , read it as an InputStream:

InputStream input = getClass().getResourceAsStream("b.xml");

Inside a static method, you can use

InputStream in = YourClass.class.getResourceAsStream("b.xml");

If your file is not in the same package as the class you are trying to access the file from, then you have to give it relative path starting with '/'.

ex : InputStream input = getClass().getResourceAsStream
           ("/resources/somex.cfg.xml");which is in another jar resource folder
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • I thinks this is the best way! – buch11 May 01 '13 at 05:44
  • what if it is not in the same package? in my example, a.java is in com.example.myproject. b.xml is in com.example.myproject.data. – Joey May 01 '13 at 05:47
  • @user1387727 The use a fully qualified path ie `"/com/example/myproject/data/b.xml"` instead... – MadProgrammer May 01 '13 at 06:00
  • In my case, I am trying to read a xml file using: URL url = getClass().getResource("b.xml"); Document doc = docBuilder.parse (new File(url.getPath())); Lets assume a.java and b.xml are in the same folder. It does not work. I print out url.getPath(), I found that the path starts from /c:/.../.../../myproject/bin/com/../../b.xml. Actually the code: docBuilder.parse (new File(url.getPath())) does not like the full file path. If I manually typed the file path: docBuilder.parse (new File("bin/com/../../b.xml")), then it works. Do you know what I can do instead of manually typing the file path? – Joey May 01 '13 at 06:02
  • The first one will only work if the JAR or WAR file has been exploded, or there wasn't one in the first place, only a directory system of .class files. – user207421 Apr 13 '17 at 08:11