Solved

relative file path in java class

Posted on 2003-10-24
16
3,794 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 2
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9614869
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
ID: 9614944
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
ID: 9615104
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9615129
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
ID: 9615283
are we talking about the same thing CEHJ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9615329
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
ID: 9615384
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
ID: 9615397
Ah-ha, got it:

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

thanks pete_playa
0
 

Expert Comment

by:pete_playa
ID: 9615416
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
ID: 9615445
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
ID: 9615581
>>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
ID: 9615630
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
ID: 9615906
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
ID: 9615974
>>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
ID: 9616019
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
ID: 9616615
>>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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 67
running on tomcat not jboss eap 7.0 3 55
What is the use of Forwarding Class in java 1 54
Java array 21 150
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

752 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