Link to home
Start Free TrialLog in
Avatar of t3ch
t3chFlag for Afghanistan

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

   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
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

the path will be relative to WEB-INF inside the web-content only.

../pdf/filename.pdf
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.getContextClassLoader().getResourceAsStream ("WebContent/pfd/filename.pdf")
Avatar of t3ch

ASKER

Hi Gurvinder,

                  so "../" is nothing but web-content?
Avatar of t3ch

ASKER

HI Hegemon,

                   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
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

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
Avatar of t3ch

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?
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.
Avatar of t3ch

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/PROJECTNAME/WebContent/WEB-INF/classes/com/companyname/module/business/report/

so what would be the relative path to the pdf folder if we consider
 the structure:

 WebContent/pdf/pdf_file
../../../../../../../pdf/pdf_file

but i would still recommend that you keep it outside the war file
Avatar of t3ch

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
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
Avatar of t3ch

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
Avatar of t3ch

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
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.
Avatar of t3ch

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().getContextClassLoader();
URL url = loader.getResource(resource);
String filePath = url.getFile();
Avatar of t3ch

ASKER

Thanks Objects.

Thanks rrz, will try it soon and post the results back.
Avatar of t3ch

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?
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.
Avatar of t3ch

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.
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.
Avatar of t3ch

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.
SOLUTION
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
Avatar of t3ch

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?
>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.
- 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.
Avatar of t3ch

ASKER

>> Please show us your current code and file location

   String fileName = "abc.xsl";
          
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL fileURL = loader.getResource(fileName);

      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
Avatar of t3ch

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.
>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 ?  
If InputSource is used, then it probably needs an InputStream as a parameter. I am not clear why t3ch mentioned

" Path: WebContent/pdf/filename.pdf"

in the original question, since he needs to read the XSL, not PDF.
Avatar of t3ch

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)
Avatar of t3ch

ASKER

@rrz,

           The file resides under JavaSource. This is the path I am getting with getResource:

xslFilePath file:/C:/Workspace/projectName/WebContent/WEB-INF/classes/abc.xsl
Avatar of t3ch

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?
you can't get the file path from a url. What do you need the path for?
Avatar of t3ch

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.
you can't access it as a file, only as a url (or stream from the url)
Avatar of t3ch

ASKER

ohk. So whats the way to Access it as a file Objects? considering I dont have access to ServletContext.
SOLUTION
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
SOLUTION
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
SOLUTION
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
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.
Avatar of t3ch

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.
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?
Avatar of t3ch

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.
Avatar of t3ch

ASKER

Thanks folks.