0

i am developping an application where i have to specify the path of a file called dao.properties it works just fine but when i execute the jar using the cmd : java -jar StockManagement.jar i get the error that the file is not found (it works fine in netbeans) the class and the file are in the same folder. i've tried a lot of relative paths and nothing works so this is my last hope here is the code and the hierarchy:

Hierarchy

Code

thank y in advance

Peppo
  • 1,107
  • 1
  • 12
  • 19
Sara
  • 79
  • 1
  • 10

2 Answers2

1

If your file is in your code base you should use the classLoader to load it.

Filippo Fratoni
  • 379
  • 2
  • 7
  • Thank you Filippo for your answer This is exactly what i am Doing – Sara Oct 22 '15 at 15:39
  • This generally works for me this.getClass().getClassLoader().getResourceAsStream(path_relative) – Filippo Fratoni Oct 22 '15 at 15:42
  • this is what i do i am new here i can't add the image in this comment i will add it in the post – Sara Oct 22 '15 at 15:45
  • You are using the context class loader of the current thread and not the classloader of the class. What you are doing is correct but the relative path to pass may change – Filippo Fratoni Oct 22 '15 at 15:52
  • you are right, i've got you, but how to adapt the relative path – Sara Oct 22 '15 at 17:43
  • i can"t use your suggestion because my methode is static :/ – Sara Oct 22 '15 at 17:47
  • If you have jdk source code attached to your IDE you can debug and see what is the absolute path it's trying to access to build the URL of you resource and adapt your relative path consequently – Filippo Fratoni Oct 23 '15 at 07:34
0

If I'm not mistaken, the way you're using ClassLoader is it looking for a file path relative to where it is being called.

From the picture, it seems that you're using ClassLoader from the DAOFactory class, is that right? You're declaring the path to your file to be

stock/DAO/dao.properties

If you're calling it from DAOFactory, Java looks for the file in

<where DAOFactory is>/stock/DAO/dao.properties

If DAOFactory and dao.properties reside in the same file I think your file path should just be

dao.properties

So it looks in the same folder that DAOFactory is in.

EDIT: Use DAOFactory class to read in properties file.

Using something like the following code snippet, call this function from the DAOFactory class using just the main method to try to see if you can read the properties file without anything else. Change any classes or names you need to to work on your local machine.

public static String getProperty(String property) {
  String value = "";

  try (InputStream is = DAOFactory.class.getResourceAsStream("dao.properties")) {
    Properties prop = new Properties();

    prop.load(is);
    value = prop.getProperty(property);
  } catch (Exception e) {
    e.printStackTrace();
  }

  return value;
}
Morgan Kenyon
  • 3,072
  • 1
  • 26
  • 38
  • Hi morgan thanks for your answer they are in the same folder and i've tried this before posting here, it didn't workd – Sara Oct 22 '15 at 17:39
  • At some point I saw you were using the Thread to attempt to read in your properties file. What if you use the DAOFactory class instead of a thread? Look at edited answer above? – Morgan Kenyon Oct 22 '15 at 17:56
  • This is what i did Morgan, i used the DAOFactory instead of the thread as Fillippo suggedted your comment also helped a lot i understood some things that was ambiguous for me Thank you so much – Sara Oct 22 '15 at 18:01
  • You're welcome, if you found my or Filipoo's answer you can upvote and/or accept our answer to show your appreciation. – Morgan Kenyon Oct 22 '15 at 18:03
  • i've done it i received that it will take time to appear publicly because i am new here – Sara Oct 22 '15 at 18:29
  • Okay, just letting you know. When I was new to SO I didn't know about accepting answers or upvoting till about my fourth question. – Morgan Kenyon Oct 22 '15 at 18:36