• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find the current path of a file  RSS feed

 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
First of all, i am using ubuntu and jdk8.
My problem: displaying the current path of a file in my system
Approach: I have a file called dummy.txt in a given directory which have enough permissions and i did the following:

I expected to see displayed the current path of the file without the name of the file but it is showing a different path.
Anybody who can help?
I just want to display the current path of the file without the name.
 
Knute Snortum
Sheriff
Posts: 4273
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code works for me. Do you have more than one dummy.txt?

Try breaking the code into pieces, like this:

 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute, I am doing my test with a servlet.
I broke it as you indicated but cannot still
get the right path. I guess it is the same problem
as using a classic java class.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can only help if we know the details. Please print out what you expected to see and what you are seeing. Also tell us whether you are using Windows®.
 
Knute Snortum
Sheriff
Posts: 4273
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The environment of a servlet is tricky. Try putting the file in WEB-INF and see if you can find it.

Other than that, post the details of the problem.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"dummy.txt" is a relative path - which you should never use in web apps, as web apps have no defined default directory. Always use absolute paths in web apps.

The ServletContext.getRealPath method helps you find the absolute path of files in a given web app.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,
I said I use Ubuntu.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Sorry I missed that. But what do you expect and what is being printed?
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,
/home/Paul/testprog is displayed instead
of /opt/tomcat8/webapps//pathapp/WEB-INF/classes/web
I wonder if you can derive anything from that.
 
Knute Snortum
Sheriff
Posts: 4273
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try file="WEB-INF/classes/web/dummy.txt"
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,
Thanks for you post. I will try that.
 
Knute Snortum
Sheriff
Posts: 4273
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a personal preference, I would put dummy.txt directly in WEB-INF.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try file="WEB-INF/classes/web/dummy.txt"

I would not put files into the classpath that are not intended to be used as resources.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,
That might not be a good thing to do but
I assume i don't know the full path to the
file. So i thought that getting the path to
the dummy.txt file would help me reference
other resources correctly in my servlet.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mentioned the getRealPath method; can I assume that you have tried that by now? What where the results?
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,
I tried the method but it returned a null value.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really have a double / in that path?
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works fine:

Thanks to Knute, Ulf and Campbell.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!