4 Replies Latest reply: Mar 14, 2008 7:34 AM by 807601 RSS

    Get current directory(without user.dir)!!!!

    807601
      Dudes,

      need help badly... i need to get the current directory of the jar file where it gets launched from. Since the launching takes place from an html file, the system property "user.dir" does not get set to the current directory. Both the html as well as the jar file is in the same location.
      so in short, i need to get the current dir without using the user.dir that i can later set it explicitly thru my program(to the current dir)
        • 1. Re: Get current directory(without user.dir)!!!!
          800308
          No can do... except by nasty, slow, nefarious, ugly, devious, sloppy and not very clever means.

          Search the CLASSPATH for the first occurence of your jar file... but FFS only do it once, and save the result.

          Now why would want to do that anyway? To keep an updatable properties file in the same place as the jar? Not a good idea, that's why it's difficult... One thing I've learned about java is that: "if it's a fairly common problem and the solution is complicated it's because you're doing it wrong".
          • 2. Re: Get current directory(without user.dir)!!!!
            807601
            "Search the CLASSPATH for the first occurence of your jar file... but FFS only do it once, and save the result"

            Cud ya elaborate that statement.

            Also, the reason i wanna know is this : my jar file references all the images that it uses, thru a relative path(./a/b/c.....). So wen i simply double click the jar file, the relative path("./") gets replaced with the current workin directory(from user.dir) and fetching of images works perfectly. But if it is launched via html, the user.dir does not get set to the current working dirctory and as a result the jar file looks for its images in the default location which is the desktop. so it doesnt work

            Now that the context is clear, is there a way out???? or is there any way in which i can set the user.dir from the html itself(maybe by using javascript or somethin??)
            • 3. Re: Get current directory(without user.dir)!!!!
              807601
              If the images are static (ie: they remain unchanged once released) then include the images in your jar file (with a "copy" target in ant) and access them via getResourceAsStream... see the help in your IDE, most of them have "a feature" to facilitate adding additional resources to jar files.

              If the images aren't static, then one solution is to require the user to create a system environment variable specifying the directory which contains your jarfile... MYAPP_HOME=c:\program files\my\app

              If you really need to find my.jar in the classpath then search the forums... I know there's a few posts with code for a recursive breadth-first directory search, coz I posted 'em (probably as corlettk)... I just can't recall the details, and you can do your own searching.

              Cheers. Keith.
              • 4. Re: Get current directory(without user.dir)!!!!
                807601
                File f = new File(".");
                 f.getPath();
                This might do it.