t3ch
asked on
get path to a file
Hi,
I have a file which resides under WebContent folder in my web application.
Path: WebContent/pdf/filename.pd f
I need to supply the path of this file to a java class under JavaSource in my project.
I know a way to do it in Servlet using ServletContext.getRealPath (), but how do I do this in
a regular java class?
Thanks
I have a file which resides under WebContent folder in my web application.
Path: WebContent/pdf/filename.pd
I need to supply the path of this file to a java class under JavaSource in my project.
I know a way to do it in Servlet using ServletContext.getRealPath
a regular java class?
Thanks
If your web application is deployed exploded and you can work out the actual path, you can do just that.
Otherwise, if it is a .war file or it is not possible to determine the physical path, try the following:
Thread.currentThread.getCo ntextClass Loader().g etResource AsStream ("WebContent/pfd/filename. pdf")
Otherwise, if it is a .war file or it is not possible to determine the physical path, try the following:
Thread.currentThread.getCo
ASKER
Hi Gurvinder,
so "../" is nothing but web-content?
so "../" is nothing but web-content?
ASKER
HI Hegemon,
The content is in a war file which eventually goes into an EAR file.
The content is in a war file which eventually goes into an EAR file.
So that's the only way to get the content of the file then (I think so)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Gurvinder,
so once I do URL myURL = MyClass.class.getResource( "./") "Is it ./ or ../"?
I'll have the path to my current class?
so for my PDF file I can do:
myURL + "webcontent/pdf/"????
Can I define this path in properties file as well?
so once I do URL myURL = MyClass.class.getResource(
I'll have the path to my current class?
so for my PDF file I can do:
myURL + "webcontent/pdf/"????
Can I define this path in properties file as well?
I would suggest that you keep any such resource out of your war file. It will be much easier for you to maintain it, as you do not need to open your war file, if you wish to change the pdf.
ASKER
Hi Gurvinder,
I tried running the first line of code that you gave and this is what it printed for the Class where I need to access the path value:
URL value: file:/C:/WorkSpace/PROJECT NAME/WebCo ntent/WEB- INF/classe s/com/comp anyname/mo dule/busin ess/report /
so what would be the relative path to the pdf folder if we consider
the structure:
WebContent/pdf/pdf_file
I tried running the first line of code that you gave and this is what it printed for the Class where I need to access the path value:
URL value: file:/C:/WorkSpace/PROJECT
so what would be the relative path to the pdf folder if we consider
the structure:
WebContent/pdf/pdf_file
../../../../../../../pdf/p df_file
but i would still recommend that you keep it outside the war file
but i would still recommend that you keep it outside the war file
ASKER
Hi Gurvinder,
Thanks for the suggestion, actually with current architecture files already exist outside the war, but for some strange reason when we added this file under the same directory, its throwing File not found exception, hence we decided to move this one inside the war.
The relative path you suggested, isnt that pointing to the class where I am referencing the path of the pdf, instead of the pdf path itself?
Is the below path correct? Please correct me if I am thinking something completely different.
(for project name) ../ (WebContent) ../ (pdf) ../ pdf_file
Thanks for the suggestion, actually with current architecture files already exist outside the war, but for some strange reason when we added this file under the same directory, its throwing File not found exception, hence we decided to move this one inside the war.
The relative path you suggested, isnt that pointing to the class where I am referencing the path of the pdf, instead of the pdf path itself?
Is the below path correct? Please correct me if I am thinking something completely different.
(for project name) ../ (WebContent) ../ (pdf) ../ pdf_file
path might be correct, but you wont be able to access the file from this path, from any class.
You need to use the path that i have replied in my last reply
You need to use the path that i have replied in my last reply
ASKER
Thanks Gurvinder, I will try it out and post results.
afaik you need to use getRealPath() anything else is not going to be reliable
ASKER
Hi Objects,
How to go about it? I tried Gurvinders solution and My code isnt picking up the file.
How do I use getRealPath? Its not a servlet, just a regular java class
How to go about it? I tried Gurvinders solution and My code isnt picking up the file.
How do I use getRealPath? Its not a servlet, just a regular java class
you need to pass the servlet context to it (or find the path from somewhere that has access to the context and pass the path to your class)
Alternative would be to put the file in the classes directory, then you could access it from the class loader.
Alternative would be to put the file in the classes directory, then you could access it from the class loader.
ASKER
Not sure, How I would use Servlet Context but If I put the file in classes directory, How do I access it using Class loader?
I agree with objects. You should put file into your web app's classes folder(which puts it into the classpath).
Please try something like this.
String resource = "filename.pdf";
ClassLoader loader = Thread.currentThread().get ContextCla ssLoader() ;
URL url = loader.getResource(resourc e);
String filePath = url.getFile();
Please try something like this.
String resource = "filename.pdf";
ClassLoader loader = Thread.currentThread().get
URL url = loader.getResource(resourc
String filePath = url.getFile();
ASKER
Thanks Objects.
Thanks rrz, will try it soon and post the results back.
Thanks rrz, will try it soon and post the results back.
ASKER
Hi Objects, rrz, gurvinder,
I tried the Class loader way and I am able to get the path of the file, still have to see if it will pick it up correctly. I am facing another problem now, I kept the file in classes folder under WEB-INF, but anytime I clean the project, this file gets cleaned up too (Deleted from the classes folder.) How do I persist the file at this location?
I tried the Class loader way and I am able to get the path of the file, still have to see if it will pick it up correctly. I am facing another problem now, I kept the file in classes folder under WEB-INF, but anytime I clean the project, this file gets cleaned up too (Deleted from the classes folder.) How do I persist the file at this location?
The file should be initially anywhere else, i.e. if you want to keep it under classes, the original should be under src. Alternatively, it does not have to be under classes, you can keep it in any other folder with your build making process placing it where necessary.
ASKER
Hi Hegemon,
I am trying to run the application locally at the moment, so I am not making use of the ant build file. I have some other properties files which are under a package called com.prop.files. So I kept this new file in the same package as well, all the properties files are visible under classes folder except for this new one.
I am trying to run the application locally at the moment, so I am not making use of the ant build file. I have some other properties files which are under a package called com.prop.files. So I kept this new file in the same package as well, all the properties files are visible under classes folder except for this new one.
That's strange, check your IDE settings for whether there is a filter copying only certain file types to classes as it compiles the application.
ASKER
Hi Hegemon,
so I just tried running it, heres the scenario.
1. When I keep the file in classes folder(not inside a specific pacakage folder), it works.
2. When I keep the file in some package, the same file is reflected under the same package sturcture under classes. For example, if I keep my file in package com.abc.xyz, I can see the file under,
WEB-INF->classes->com->abc ->xyz
but in this scenario, the pdf is not generating.
so I just tried running it, heres the scenario.
1. When I keep the file in classes folder(not inside a specific pacakage folder), it works.
2. When I keep the file in some package, the same file is reflected under the same package sturcture under classes. For example, if I keep my file in package com.abc.xyz, I can see the file under,
WEB-INF->classes->com->abc
but in this scenario, the pdf is not generating.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Hegemon,
Thanks for the detailed reply. I eventually got it to work and it works fine on my local machine. As soon as I deploy on remote WAS 6.1, it never works. Do this depend on machine to machine?
Thanks for the detailed reply. I eventually got it to work and it works fine on my local machine. As soon as I deploy on remote WAS 6.1, it never works. Do this depend on machine to machine?
>Do this depend on machine to machine?
Ideally, "Write once, run anywhere" (Sun slogan).
Please show us your current code and file location. Maybe someone can help you.
Ideally, "Write once, run anywhere" (Sun slogan).
Please show us your current code and file location. Maybe someone can help you.
- Do this depend on machine to machine?
If you pack all resources you need into a .WAR and read the file off classpath, it will work on any servlet container on any machine.
I guess you are currently running it from your IDE. Make sure the WAR is packed correctly and try locally without the IDE as well.
If you pack all resources you need into a .WAR and read the file off classpath, it will work on any servlet container on any machine.
I guess you are currently running it from your IDE. Make sure the WAR is packed correctly and try locally without the IDE as well.
ASKER
>> Please show us your current code and file location
String fileName = "abc.xsl";
ClassLoader classLoader = Thread.currentThread().get ContextCla ssLoader() ;
URL fileURL = loader.getResource(fileNam e);
This is my current code.
Is there any difference between getResource and getResourceAsStream?
Current structure of code:
JavaSource:
com.package1
com.package2
com.package3
abc.xsl
WebContent:
images
jsp
html
js
WEB_INF
String fileName = "abc.xsl";
ClassLoader classLoader = Thread.currentThread().get
URL fileURL = loader.getResource(fileNam
This is my current code.
Is there any difference between getResource and getResourceAsStream?
Current structure of code:
JavaSource:
com.package1
com.package2
com.package3
abc.xsl
WebContent:
images
jsp
html
js
WEB_INF
>Is there any difference between getResource and getResourceAsStream?
Yes. The first gives a URL. The second gives a stream. You should tell us what you are trying to do here. What do you want to do with the file and it's content ?
>Current structure of code:
It looks like you are giving us the structure in your IDE. As Hegemon requested, we are interested in the war file that you are deploying to your remote server. Using a zip utility(rename it to .zip if necessary), unzip it and see where your file is located. Alternatively from the command line, you can use
jar tvf your.war
Yes. The first gives a URL. The second gives a stream. You should tell us what you are trying to do here. What do you want to do with the file and it's content ?
>Current structure of code:
It looks like you are giving us the structure in your IDE. As Hegemon requested, we are interested in the war file that you are deploying to your remote server. Using a zip utility(rename it to .zip if necessary), unzip it and see where your file is located. Alternatively from the command line, you can use
jar tvf your.war
ASKER
Hi rrz@871311,
Thanks for the reply. Wont be possible to supply war file, its a huge one (close to 88 mb).
Heres the scenario what I trying to do:
Trying to generate a pdf through an XSL file. and this is the file that my class needs to access in order to generate the PDF.
Thanks for the reply. Wont be possible to supply war file, its a huge one (close to 88 mb).
Heres the scenario what I trying to do:
Trying to generate a pdf through an XSL file. and this is the file that my class needs to access in order to generate the PDF.
>Wont be possible to supply war file
We don't want the file. You can open it up and see where XSL file is located.
>this is the file that my class needs to access in order to generate the PDF.
What does the method of that class expect as a parameter ?
We don't want the file. You can open it up and see where XSL file is located.
>this is the file that my class needs to access in order to generate the PDF.
What does the method of that class expect as a parameter ?
If InputSource is used, then it probably needs an InputStream as a parameter. I am not clear why t3ch mentioned
" Path: WebContent/pdf/filename.pd f"
in the original question, since he needs to read the XSL, not PDF.
" Path: WebContent/pdf/filename.pd
in the original question, since he needs to read the XSL, not PDF.
ASKER
Hi Hegemon,
I apologize, I just replace xsl with pdf (because in the beginning I thought the task is just to get to that file)
I apologize, I just replace xsl with pdf (because in the beginning I thought the task is just to get to that file)
ASKER
@rrz,
The file resides under JavaSource. This is the path I am getting with getResource:
xslFilePath file:/C:/Workspace/project Name/WebCo ntent/WEB- INF/classe s/abc.xsl
The file resides under JavaSource. This is the path I am getting with getResource:
xslFilePath file:/C:/Workspace/project
ASKER
I saw another difference in my code then what rrz suggested.
At the very last line of his code, to get filepath, he did
String filepath = url.getFile();
I didnt do that, instead I used String filepath = url.toString();
Do you think this can be the cause of it?
At the very last line of his code, to get filepath, he did
String filepath = url.getFile();
I didnt do that, instead I used String filepath = url.toString();
Do you think this can be the cause of it?
you can't get the file path from a url. What do you need the path for?
ASKER
Hi Objects,
The code looks for an XSL file, populates all the attributes and eventually prints a PDF. I just added this new XSL file with some different attributes just like the previous XSL files that are already present in this application. The only difference is all those files reside outside the WAR file in a folder on the same unix box which holds the EAR deployment, but when we added this new file, we always got file not found exception, so eventually decided to move it in the EAR.
The code looks for an XSL file, populates all the attributes and eventually prints a PDF. I just added this new XSL file with some different attributes just like the previous XSL files that are already present in this application. The only difference is all those files reside outside the WAR file in a folder on the same unix box which holds the EAR deployment, but when we added this new file, we always got file not found exception, so eventually decided to move it in the EAR.
you can't access it as a file, only as a url (or stream from the url)
ASKER
ohk. So whats the way to Access it as a file Objects? considering I dont have access to ServletContext.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To repeat, if you are using XSL to to generate PDF, you are likely to need a Stream, not a File. Anyway, you cannot access a File in a packed .war. If you do need a file, you will have to create a temporary one, streaming the content from the original file using a stream.
ASKER
>> It works in Tomcat. I ran my code from a unpack war. On my machine it returned the following.
Hi rrz, it prints the file path on WAS6.1 as well.
Hi rrz, it prints the file path on WAS6.1 as well.
Where do we stand now ? You got the path to the file. Are you able to use it to do what you want ?
@t3ch: Is your problem resolved?
ASKER
folks, eventually the problem got solved. There was an extra white space in the path written under the properties file :)
I will assign points to all of you. You all really helped a lot.
I will assign points to all of you. You all really helped a lot.
ASKER
Thanks folks.
../pdf/filename.pdf