relative file path in java class

i am trying to add an image to a pdf file using itext in a java class (struts action class).  when the project gets uploaded to the server the path will be different.  therefore i am trying to keep all the paths relative.  I thought this would be straightforward but is proving quite difficult.

here are some configurations i have already tried:

logo = Image.getInstance("../images/logo.jpg");
(creates relative to c: and not my project path)

logo = Image.getInstance(new URL("http://" + request.getServerPath() + ":" + request.getServerPort + "/images/nbm_logo_light.jpg"));
(security didn't allow anonymous access to the url)

logo = Image.getInstance(mapping.getPrefix() + "/images/nbm_logo_light.jpg");
(seems to point to root of tomcat directory)

Is there any easy way to dynamically retrieve the project root inside a java class?
LVL 1
MitchBroadheadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Why don't you simply create an upload directory off the application root and upload things to there?
0
MitchBroadheadAuthor Commented:
the file systems are different on windows and linux, and i would rather keep everything in the project.
there must be some way to retrieve the path of the project!
0
pete_playaCommented:
try this in the init()

String prefix = getServletContext().getRealPath("/") + File.separator;
System.out.println("root = " + prefix);

This should print the root of the webapp, for example if webapp is "test", then the aboeve should print c:\jakarta-tomcat\webapps\test or /usr/local/tomcat/webapps/test

let me know
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

CEHJCommented:
Why do you need to know the 'real' path? Just use /upload in each case. You should have a dedicated upload directory
0
MitchBroadheadAuthor Commented:
are we talking about the same thing CEHJ?
0
CEHJCommented:
I certainly hope so ;-) You have to upload a file to a web application, but seem to be worried about paths. I'm saying just create a directory /upload and upload it to that. What's the problem with that?  
0
MitchBroadheadAuthor Commented:
pete_playa,
that looks like a good idea.  can't seem to call the getServletContext().  Is it available in a Struts Action class?  I am not really familiar with init() functions.
0
MitchBroadheadAuthor Commented:
Ah-ha, got it:

used getServlet().getServletContext().getRealPath("/") to get there in an action class

thanks pete_playa
0
pete_playaCommented:
cool man...

first answer given and accepted in EE, now maybe someone can answer my question. lol

P
0
MitchBroadheadAuthor Commented:
CEHJ,
the problem with that is that it is important to try and answer the question properly and not offer a solution which doesn't bear any resemblence at all.
if you have a valid reason why i shouldn't try to solve my problem (i.e. security or something) then let me know, otherwise stick to the subject.
thanks for trying (no sarcasm intended at all),
Mitch
0
CEHJCommented:
>>therefore i am trying to keep all the paths relative.  is what you were saying in your first posting. The answer you accepted is in fact doing the reverse, i.e. finding out the absolute path. The question is, why? As far as I can see, there is no need to know the absolute path. There would only be this need *outside* the logic and context of the web application.
0
MitchBroadheadAuthor Commented:
CEHJ,

This is the question I originally asked (if you were to read it properly):
- Is there any easy way to dynamically retrieve the project root inside a java class?

>>therefore i am trying to keep all the paths relative.
In this context i am referring to "relative to the project path" not to the final resting place of the application on the webserver

pete_playa has read the question and given me a perfectly good answer with no warnings as to why i should not use such a method.  If he could understand the question why couldn't you?  Why are you bashing on saying you were right when you are not?  are you just argumentative by nature?  

Why don't you 'get a life' and go and answer a question that you know something about?

0
CEHJCommented:
There *was* a slight misundertanding though. Since you used the word 'upload' (i'm used to the word 'deploy') i thought the file was being uploaded, hence my recommendation for an /upload directory. Quite apart from that, my point about absolute versus relative paths holds anyway. Why don't you do the following and then proceed from there?  URL imageUrl = getServlet().getServletContext().getResource("/images/yourImage.jpg"); //assuming file in images directory
0
CEHJCommented:
>>Is there any easy way to dynamically retrieve the project root inside a java class?  Why would you need to know? That's the whole point of what relative paths are about.  >>pete_playa has read the question and given me a perfectly good answer   It will work OK, yes. The point is that getRealPath() works *against* what you want, i.e. it is querying for an *absolute* path
0
MitchBroadheadAuthor Commented:
CEHJ,

now you are just saying what has already been accepted as the correct answer.  this is very strange!

also, you are using URL imageUrl in your answer when I have already told you in the question that I cannot retrieve the file by URL due to security and would prefer to use the local filepath.

could you stop posting to this question now as the answer has already been accepted.  you are not going to get any credit at all!
0
CEHJCommented:
>>now you are just saying what has already been accepted as the correct answer

No i'm not. You obviously don't understand the fact the accepted answer uses a call to get an *absolute* path whereas my answer uses relative paths.

>> have already told you in the question that I cannot retrieve the file by URL

a. where did you say that?
b. your very second code example uses a url
c. what has this got to do with security?

>>you are not going to get any credit at all!
     
I am well aware of that, but for the benefit of future users I have posted the correct answer based on your question.
Also I don't take kindly to being accused of ignorance and should warn you that users have had their accounts suspended in the past for openly insulting postings.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.