10 Replies Latest reply: Feb 12, 2009 4:17 AM by 800308 RSS

    Getting server's directory path

    703027
      From my java application I need to get the directory path of a file. I'm using the following command

      File destFile=new File(getServletContext().getRealPath("/"))

      error says cannot resolve symbol getServletContext(). What is the problem?
        • 1. This Thread is now moved
          Darryl Burke
          Note: This thread was originally posted in the Swing forum, but moved to this forum for closer topic alignment.
          • 2. Re: Getting server's directory path
            800283
            Show us your code. That method is available from GenericServet...are you in a Servlet?

            m

            Edited by: yorkroad on Feb 11, 2009 9:54 AM - spelling
            • 3. Re: Getting server's directory path
              843785
              harinibiligiri wrote:
              From my java application...
              Your choice of words implies you're working with J2SE, servlets and hence ServletContext is a J2EE thing.

              File class has methods to do most of the things you need, I suggest you go through the API at http://java.sun.com/javase/6/docs/api/
              • 4. Re: Getting server's directory path
                703027
                Mine is a swing application. I'm connecting to a server to get some data. File is also stored in that. I want to display the file stored on the server . How do I get the path?
                • 5. Re: Getting server's directory path
                  843785
                  You can't access the file on the server directly from the client. They can be on different computers altogether, so there's no way your client can access the files without additional work.

                  Your server needs to give access the the files on it in some way, such as HTTP, FTP or other protocols.
                  • 6. Re: Getting server's directory path
                    703027
                    will u pl give the sample code?
                    • 7. Re: Getting server's directory path
                      703027
                      The file is in Tomcat's root directory. That's why I thought I can use this getServletContext().getRealPath("/") to get the file from my application.
                      • 8. Re: Getting server's directory path
                        800308
                        harinibiligiri wrote:
                        will u pl give the sample code?
                        No. And get a hair cut!
                        • 9. Re: Getting server's directory path
                          843785
                          harinibiligiri wrote:
                          The file is in Tomcat's root directory. That's why I thought I can use this getServletContext().getRealPath("/") to get the file from my application.
                          That would be quite magical. So if you put program1 in the same directory as program2, you can automatically access program2's functions from program1? Nope, doesn't work like that.

                          I recommend going through basic tutorials.
                          • 10. Re: Getting server's directory path
                            800308
                            h1. The Ubiquitous Newbie Tips

                            * DON'T SHOUT!!!

                            * Homework dumps will be flamed mercilessly. [Feelin' lucky, punk? Well, do ya'?|http://www.youtube.com/watch?v=1-0BVT4cqGY]

                            * Have a quick scan through the [Forum FAQ's|http://wikis.sun.com/display/SunForums/Forums.sun.com+FAQ].

                            -----

                            h5. Ask a good question

                            * Don't forget to actually ask a question. No, The subject line doesn't count.

                            * Don't even talk to me until you've:
                                (a) [googled it|http://www.google.com.au/] and
                                (b) had a squizzy at the [Java Cheat Sheet|http://mindprod.com/jgloss/jcheat.html] and
                                (c) looked it up in [Sun's Java Tutorials|http://java.sun.com/docs/books/tutorial/] and
                                (d) read the relevant section of the [API Docs|http://java.sun.com/javase/6/docs/api/index-files/index-1.html] and maybe even
                                (e) referred to the JLS for "advanced" questions.

                            * [Good questions|http://www.catb.org/~esr/faqs/smart-questions.html#intro] get better Answers. It's a fact. Trust me on this one.
                                - Lots of regulars on these forums simply don't read badly written questions. It's just too frustrating.
                                  - FFS spare us the SMS and L33t speak! Pull your pants up, and get a hair cut!
                                - Often you discover your own mistake whilst forming a "Good question".
                                - Often you discover that you where trying to answer "[the wrong question|http://blog.aisleten.com/2008/11/20/youre-asking-the-wrong-question/]".
                                - Many of the regulars on these forums will bend over backwards to help with a "Good question",
                                  especially to a nuggetty problem, because they're interested in the answer.

                            * Improve your chances of getting laid tonight by writing an SSCCE
                                - For you normal people, That's a: Short Self-Contained Compilable (Correct) Example.
                                - Short is sweet: No-one wants to wade through 5000 lines to find your syntax errors!
                                - Often you discover your own mistake whilst writing an SSCCE.
                                - Often you solve your own problem whilst preparing the SSCCE.
                                - Solving your own problem yields a sense of accomplishment, which makes you smarter ;-)

                            -----

                            h5. Formatting Matters

                            * Post your code between a pair of {code} tags
                                - That is: {code} ... your code goes here ... {code}
                                - This makes your code easier to read by preserving whitespace and highlighting java syntax.
                                - Copy&paste your source code directly from your editor. The forum editor basically sucks.
                                - The forums tabwidth is 8, as per [the java coding conventions|http://java.sun.com/docs/codeconv/].
                                  - Indents will go jagged if your tabwidth!=8 and you've mixed tabs and spaces.
                                  - Indentation is essential to following program code.
                                  - Long lines (say > 132 chars) should be wrapped.

                            * Post your error messages between a pair of {code} tags:
                                - That is: {code} ... errors here ... {code}
                                - OR: [pre]{noformat} ... errors here ... {noformat}[/pre]
                                - To make it easier for us to find, Mark the erroneous line(s) in your source-code. For example:
                                    System.out.println("Your momma!); // <<<< ERROR 1
                                - Note that error messages are rendered basically useless if the code has been
                                  modified AT ALL since the error message was produced.
                                - Here's [How to read a stacktrace|http://www.0xcafefeed.com/2004/06/of-thread-dumps-and-stack-traces/].

                            * The forum editor has a "Preview" pane. Use it.
                                - If you're new around here you'll probably find the "Rich Text" view is easier to use.
                                - WARNING: Swapping from "Plain Text" view to "Rich Text" scrambles the markup!
                                - To see how a posted "special effect" is done, click reply then click the quote button.

                             

                            If you (the newbie) have covered these bases *you deserve, and can therefore expect, GOOD answers!*

                             

                            -----

                            h1. The pledge!

                            We the New To Java regulars do hereby pledge to refrain from flaming anybody, no matter how gumbyish the question, if the OP has demonstrably tried to cover these bases. The rest are fair game.