?
Solved

getResourceAsStream Error?

Posted on 2006-05-29
11
Medium Priority
?
358 Views
Last Modified: 2011-10-03
Hi,
I am trying to parse an XML file using a custom XML parser.
But I am just not even being able to read it ???

I have CECEVFeedConfigParameters class that uses the parser, and in it I tried:
  InputStream lConfigFileInputStream =
                ((Class)mClass)
                    .getResourceAsStream
                        (CEC_EV_FEED_CFG_FILE);

            if (lConfigFileInputStream == null)
            {
                throw new TransferSystemException
                    ("CEC EV Feed config file "+
                     CEC_EV_FEED_CFG_FILE +
                     " missing");
            }

I have an Interface where I declared:
 public static final String
        CEC_EV_FEED_CFG_FILE            = "C:\\Projects\\CECEVFeedConfigParameters.xml";

In my main class I tried as:

File            lCECEVFeedFilePath      = null;
try{
            lCECEVFeedFilePath =
                      new File(CECEVFeedConfigParameters.getInstance().getDataFilePath());
                        
                  }
                  catch(Exception e){
                        e.printStackTrace();
                        System.out.println("Could not parse");
                        
                  }

And I get the error:

null

.........................TransferSystemException printStackTrace:

.........................TransferSystemException: CEC EV Feed config file C:\Projects\CECEVFeedConfigParameters.xml missing
      at ............util.CECEVFeedConfigParameters.getInstance(CECEVFeedConfigParameters.java:123)
      at .............EmailMain.read(EmailMain.java:35)
      at ..............EmailMain.main(EmailMain.java:101)

...................................TransferSystemException: 0

Imbedded Exception   = java.lang.Exception
Imbedded Exception
           message   =

Fri Jun 02 01:40:32 CDT 2006
5
2
5
Here
Could not parse


I NEED TO KNOW WHAT I NEED TO DO SO THAT I CAN READ THE XML FILE ..... (NOT TO THINK ABOUT PARSING NOW :)

I AM JUST RUNNING THE APP WITH ECLIPSE ..
DO I NEED TO SET UP THE CLASSPATH ??? FOR THE RESOURCE???
NEED A SOLUTION ......(FULL POINTS DUE TO ........)

LET ME KNOW..

Thax.
_Esam
0
Comment
Question by:_Esam
  • 4
  • 4
  • 3
11 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 16786130
you can either use:

          lCECEVFeedFilePath =
                     new File(CEC_EV_FEED_CFG_FILE);

or, if you put the xml file in your classpath you can use getResourceAsStream.
For example if u put it in the same directorory as mClass you would use:

 InputStream lConfigFileInputStream =
                ((Class)mClass)
                    .getResourceAsStream
                        ("CECEVFeedConfigParameters.xml");

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16786171
Try


InputStream lConfigFileInputStream = new FileInputStream(CEC_EV_FEED_CFG_FILE);
0
 

Author Comment

by:_Esam
ID: 16786195
I get what you are trying to say....

But..
I have to use it as I tried to illustrated it:

How do I put it in the CLASSPATH then? (Using Eclipse:)

_Esam
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 16786213
Make a resources folder at the project root (let it be called 'resources') and import it there thru Eclipse, then

CEC_EV_FEED_CFG_FILE    = "/resources/CECEVFeedConfigParameters.xml";

InputStream lConfigFileInputStream = getClass().getResourceAsStream(CEC_EV_FEED_CFG_FILE);
0
 
LVL 92

Expert Comment

by:objects
ID: 16786215
> How do I put it in the CLASSPATH then? (Using Eclipse:)

put it in the same directory as the class file and use the command I posted above
0
 

Author Comment

by:_Esam
ID: 16786220
Eventually I will have to use the custome parser to parse through the XML and get the attributes...
I just want to test that I can at least read it using the getResourceAsStream() - this I have to use :)

_Esam
0
 
LVL 92

Expert Comment

by:objects
ID: 16786226
> I just want to test that I can at least read it using the getResourceAsStream() - this I have to use :)

See my earlier comment, it shows what you need to do to use getResourceAsStream()

For example if u put it in the same directorory as mClass you would use:

 InputStream lConfigFileInputStream =
                ((Class)mClass)
                    .getResourceAsStream
                        ("CECEVFeedConfigParameters.xml");


0
 

Author Comment

by:_Esam
ID: 16786234
> InputStream lConfigFileInputStream =
                ((Class)mClass)
                    .getResourceAsStream
                        ("CECEVFeedConfigParameters.xml");

Yes -- I just wanted to use the final variable ?
Anyway,
It is working if I put it in the same directory now....
Just getting my expected error now... Happy with it so far..
But trying to put it in a different directory and try it now ... and getting back soon...

Thanks.
_Esam
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 16786244
>>It is working if I put it in the same directory now....

Your binaries and config files should be logically separated so shouldn't be in the same directory
0
 

Author Comment

by:_Esam
ID: 16786290
>Your binaries and config files should be logically separated so shouldn't be in the same directory

Indeed  that's what I was planning but I needed to make sure that for the least I can make it work :)

It's working both ways now.. If I put it in the same directory of the class file or in a different directory relative to the current working directory..

Thanks both...

Coming back shortly with a parsing problem... :)

_Esam.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16786300
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 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