Link to home
Start Free TrialLog in
Avatar of MitchBroadhead
MitchBroadhead

asked on

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?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Why don't you simply create an upload directory off the application root and upload things to there?
Avatar of MitchBroadhead
MitchBroadhead

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of pete_playa
pete_playa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why do you need to know the 'real' path? Just use /upload in each case. You should have a dedicated upload directory
are we talking about the same thing CEHJ?
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?  
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.
Ah-ha, got it:

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

thanks pete_playa
cool man...

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

P
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
>>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.
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?

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
>>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
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!
>>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.