getting an absolute path in Java?

Discussion in 'OT Technology' started by babygodzilla, Nov 20, 2007.

  1. babygodzilla

    babygodzilla I love rice

    Joined:
    Nov 5, 2001
    Messages:
    3,108
    Likes Received:
    0
    I'm a little confused on how to get an absolute path to a file in Java. Right now I'm using

    Code:
    String currDir = System.getProperty("user.dir");
    obviously this is the wrong thing to use. when I run the code in Eclipse, it churns out a different path, when I run it in Firefox, it churns out yet another different path, and so on.

    what I am trying to point to is a properties file that contains info such as database username/password, URLs, etc. what is the proper way to do this? how do I get a path that will never change?

    thank you sirs.
     
  2. babygodzilla

    babygodzilla I love rice

    Joined:
    Nov 5, 2001
    Messages:
    3,108
    Likes Received:
    0
    OK so I actually got this to work by putting my properties file under /classes/properties, and find it using getClass().getResourceAsStream("/properties/prop.properties").

    now, if i want to WAR my application, will that method still work? the idea is to have the same WAR file on multiple servers, with slightly different properties file that bears the same filename.

    neone?
     
  3. Dnepr

    Dnepr Guest

    What you are doing is requesting that file as a resource which is included into war when you build the application.

    Basically you will not be able to have a same exact WAR. Since you'll have to rebuild the application with new propr files for it to be included.

    What exactly are you trying to do?

    Why not use a persistence solution? i.e. database.
     
  4. babygodzilla

    babygodzilla I love rice

    Joined:
    Nov 5, 2001
    Messages:
    3,108
    Likes Received:
    0
    lol. what i am trying to do is have a config file that contains info to connect to the DB (e.g. username, password, etc). if i put that info in the database, then i'll have a catch-22. what i need is to put the config file in a relative path that will always be the same, no matter what machine it is on, and of course my code must be able to reach that path. most ideal would probably be the WAR file's root directory.
     
  5. Dnepr

    Dnepr Guest

    :o

    Right. The way you are doing it, its not gonna work.


    May I ask why you want it to be in a separate file in a first place? What is your application supposed to be doing if its not a secret?

    Why not just hardcode the values into a separate class and access them when needed? You know since you will need to rewrite config file for every different server anyways... might as well put it in the code where you dont have to parse it.
     
  6. babygodzilla

    babygodzilla I love rice

    Joined:
    Nov 5, 2001
    Messages:
    3,108
    Likes Received:
    0
    but... if i hardcode them that means i have to change code and compile for each system...? that doesnt sound like it makes sense to me.
     
  7. Dnepr

    Dnepr Guest

    You do realise you are hardcoding them in your prop file anyways and you will need to change it for every different deployment...


    Or am I missing something here? :coolugh:
     

Share This Page