4 Replies Latest reply: Feb 6, 2008 6:43 PM by 807601 RSS

    Problem with my program looking for the settings file in the wrong folder

    807601
      I have been writing a simple FTP file uploader, what I want to do is be able to select the files I want to upload in windows explorer and then right click and click the menu item and it launches the program and passes the files paths that I have selected to it.

      So I use this in the windows registry "C:\Program Files\Java\jre1.6.0_03\bin\java.exe -jar D:\BenFTP\BenFTP.jar %1"

      It launches fine and has no problem finding the files I want to upload. The problem is that it tries to look for the settings file in the same folder that the file I am try to upload is in. Which it's not suppose to do since the settings file is in the same folder that the .jar is in.

      Edited by: ColNewman on Feb 5, 2008 6:55 PM
        • 1. Re: Problem with my program looking for the settings file in the wrong fold
          807601
          Java will use whichever directory the operating system sets as the current directory when reading files. The other directory that is knows by default is the user's home directory. Any other directory will need to be provided as a parameter.
          • 2. Re: Problem with my program looking for the settings file in the wrong fold
            DrClap
            So, you're looking for your settings file in your current working directory. There's no way to set the CWD in your registry entry (is there?) so that isn't a practical thing to do. Presumably you're using a File object or a FileReader or something? Can't do that.

            One alternative is to look for the settings file in the classpath. You can get a URL to a file in the classpath like this:
            URL settings = this.getClass().getResource("/settings.xml");
            Or you can get an InputStream to read the file by using the getResourceAsStream method. You would have to make sure that your executable jar file contained a Class-Path entry that specified the right directory, because the directory the jar is contained in isn't automatically in an executable jar's classpath.

            Another alternative is to ask the user where the settings file is supposed to be, and put an entry in the Preferences (java.util.prefs) to remember that location.
            • 3. Re: Problem with my program looking for the settings file in the wrong fold
              807601
              yes I am using FileReader.

              when I used this it returned as Null,
              URL settings = this.getClass().getResource("/settings.xml");
              I know nothing about class-paths in .jar files, so are you saying that class-path entry would be absolute and not relative to my .jar? Wouldn't that defeat the purpose?


              What I am trying to do it make it so it can be run on other computers with minimum effort by the user to set it up (since I haven't messed with any installers for it yet)

              The weird thing is that if I use the app FileMenu Tools to add it to the registry, and run it from that registry entry it runs fines.
              • 4. Re: Problem with my program looking for the settings file in the wrong fold
                807601
                You really need to separate the Windows Registry issues from the way that this Java app uses that. The Operating System will set a "Current directory" which will be used by the app. When you use the classpath, you can find resources in the class directories and jar file son that path.

                Before you attempt to build a deployable desktop app you need to have a better much understanding of this area than I have. I tend to write JEE server code most of the time, and these issues are very different in that environment.

                I believe there is a forum on this site dedicated to these kind of questions.