Solved

relative file path in java class

Posted on 2003-10-24
16
3,657 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:MitchBroadhead
  • 7
  • 7
  • 2
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Why don't you simply create an upload directory off the application root and upload things to there?
0
 
LVL 1

Author Comment

by:MitchBroadhead
Comment Utility
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
 

Accepted Solution

by:
pete_playa earned 250 total points
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Why do you need to know the 'real' path? Just use /upload in each case. You should have a dedicated upload directory
0
 
LVL 1

Author Comment

by:MitchBroadhead
Comment Utility
are we talking about the same thing CEHJ?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 1

Author Comment

by:MitchBroadhead
Comment Utility
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
 
LVL 1

Author Comment

by:MitchBroadhead
Comment Utility
Ah-ha, got it:

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

thanks pete_playa
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:pete_playa
Comment Utility
cool man...

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

P
0
 
LVL 1

Author Comment

by:MitchBroadhead
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 1

Author Comment

by:MitchBroadhead
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 1

Author Comment

by:MitchBroadhead
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now