Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Howto get a relative path to a file

    Hi there,

    I'm trying to read a configuration file called "user.ini" into my application.
    When i try to do this from within netbeans everything is fine.

    After building it however it goes wrong.

    The application will eventually be called from within the windows scheduler to run daily. therefore i would like to execute it with just a single command like

    java -classpath "d:\java apps\mahler\dist\jftpmahler.jar" -jar "d:\java apps\mahler\dist\jftpmahler.jar"

    when i do execute that from the project directory ../dist (where also the .ini file resides) everything is fine.

    when i do execute the same command from any other location however i get a FileNotFoundException error for the "user.ini" file.

    now im sure i could make a batch file so i could run it from the application directory. However this is not wanted, therefore i would like to get a relative path to the user.ini file.

    i have read several posts on different forums on how to get a relative path and use that to make an absolute path to the file but i dont seem to get that working. but that is basically what i want, to get a relative path to the file so, no matter where the application is, it will alway be able to find the ini file.


    sofar i use this code to open the ini file:

    ReadIni ini = new ReadIni("user.ini");

    public ReadIni(String IniFileName)
    {

    inifile = IniFileName;
    try
    {
    p.load(new FileInputStream (inifile));
    }
    catch(Exception e)
    { //..... }
    }


    well im hoping for a solution, but in any case thanks for reading.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    put it inside the jar file and use

    Code:
    p.load(this.getClass().getResourceAsStream(inifile));
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks shyam,

    I tried it out and it works fine. this was exactly what i was looking for.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •