aspose file tools*
The moose likes JSP and the fly likes FileNotFoundException is thrown when i try to read a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "FileNotFoundException is thrown when i try to read a file?" Watch "FileNotFoundException is thrown when i try to read a file?" New topic
Author

FileNotFoundException is thrown when i try to read a file?

Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

i have a Login.jsp which creates an instance of DatabaseAccess.java code which in turn Creates an instance of Connections.java class from which i trying to get the file named COnfig.xml but it throws an error in dat line. here are the code snippets.
Login.jsp
<%
String university[]=null;

DatabaseAccess db1=new DatabaseAccess();

university=db1.ListUniversity();

int i2=0;
for(int i1=0;i1<university.length;i1++)
{

if(university[i1]==null ||university[i2]==null)
{
break;
}
%><option value=<%=""+university[i2+1]+""%>><%=""+university[i2]+""%></option><%
i2=i2+2;

}
System.out.println("Count "+i2/2);
%>


DatabaseAccess.java

public DatabaseAccess() throws IOException
{

conn = new Connections();
connectionActive=conn.Connect();
}

Connections.java
public boolean Connect()
{
boolean flag=false;
String path="Config.xml";
ConfigHandler config=new ConfigHandler(path);
//ConfigHandler config=new ConfigHandler("/WEB-INF/Config.xml");
URL=config.getConfigValues("DBConnectionString");
System.out.println(config.getConfigValues("DBConnectionString"));
//System.out.println("The url is--->"+URL);
driverURL=config.getConfigValues("DriverName");
dbName=config.getConfigValues("DBName");
dbUser=config.getConfigValues("DBUserID");
dbPwd=config.getConfigValues("DBPwd");


ConfigHandler.java

public ConfigHandler(String xmlFile)
{
System.out.println("asdf-->"+xmlFile);
this.xmlFile = xmlFile;
initObjects();
}

private void initObjects()
{
try
{
xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
//xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
xPath = XPathFactory.newInstance().newXPath();
}
catch (IOException ex)
{
ex.printStackTrace();
}
catch (SAXException ex)
{
ex.printStackTrace();
}
catch (ParserConfigurationException ex)
{
ex.printStackTrace();
}
}

this are the code snippets kindly can ne 1 help im stuck here for the past three days.
wat i m trying to do is just get the path of Config.xml file in Connections.java and pass it to ConfigHandler.java
when i run the same code as javaApplication it works f9 but when i run the code from Login.jsp it gives me FileNotFoundException error.
can anybody help me wid this.
[ April 05, 2008: Message edited by: Bear Bibeault ]

[Vipassana] - It is seeing the reality as it is, And not as you want it to be.!!!
SCJP1.5.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Welcome to JavaRanch.

In web applications there is no concept of a default directory, so you can't use relative file paths like "Config.xml". You also can't use absolute file paths like "/WEB-INF/Config.xml" because WEB-INF is not a top-level directory in the file system (even though it is in the web application).

You need to get hold of the ServletContext object, and then something like ServletContext.getRealPath("/WEB-INF/Config.xml") will get you the absolute file system path to use.
[ April 05, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

Dittmer thanks for your reply.but For your information i m trying to access the class file from my jsp, Login.jsp which Access DatabaseAccess.java which creates an object of connections.java in its constructor and call its connect method which passes the Config.xml to the ConfigHandler which should read the file from the project location ../ABC/WEB-INF/Config.xml but it reads from the d:\eclipse\ABC\WEB-INF\Config.xml. which is wrong as my Config.xml is stored in c:\documents and settings\pravin\workspace\ABC\WEB-INF\Config.xml
my question is how to i get the path of the Config.xml from a java class to access it location?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Re-read Ulf's post.
He told you exactly how to get the path to the file.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

when i tried using getServletContext().getRealPath(/WEB-INF/Config.xml);
i got the following error.



C:\Documents and Settings\pravin\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UNDP-MIS\WEB-INF\Config.xml
SuperC:\Documents and Settings\pravin\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UNDP-MIS\WEB-INF\Config.xml
asdf-->C:\Documents and Settings\pravin\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UNDP-MIS\WEB-INF\Config.xml
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.CONFIG.ConfigHandler.initObjects(ConfigHandler.java:41)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.CONFIG.ConfigHandler.<init>(ConfigHandler.java:25)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.UTILITY.Connections.Connect(Connections.java:131)
at org.apache.jsp.Login_jsp._jspService(Login_jsp.java:180)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
If the method you're using it with needs an URL, prefix the file path with "file:///".
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

thank you for your reply, but when i tried using file:///config.xml then again it gave me the same error MalformedURLException: unknown protocol: c

java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.CONFIG.ConfigHandler.initObjects(ConfigHandler.java:41)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.CONFIG.ConfigHandler.<init>(ConfigHandler.java:25)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.UTILITY.Connections.Connect(Connections.java:149)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.UTILITY.DatabaseAccess.<init>(DatabaseAccess.java:40)
at org.apache.jsp.Login_jsp._jspService(Login_jsp.java:176)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

Is there anyone who can help me out of this?? I am really stuck here for the past Four days. I have tried everything what i can do and what others have suggested me to do in this forum. need your help urgently???
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by pravin shirke:
when i tried using getServletContext().getRealPath(/WEB-INF/Config.xml);


The argument to getRealPath needs to be a string.
You're missing the quotes.
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

sir i have passed the string value like this
request.getRealPath("/WEB-INF/Config.xml");
but when i pass this as the value to retrieve the file then it gives me the above error.
i.e MalformedURLException: Unknown Protocol : c
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

getServletContext().getRealPath(/WEB-INF/Config.xml);
when i use this code then also it gives the same error
MalformedURLException: Unknown Protocol: c
please help me??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61436
    
  67

What line is giving the exception? Please be complete when posting questions.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

getServletContext().getRealPath("/WEB-INF/Config.xml");
when i use this code then also it gives the same error
MalformedURLException: Unknown Protocol: c
please help me??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
What does the documentation of the ConfigHandler(String) constructor say about the parameter it expects? Does it need a file name, or an URL, or something else?
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

getServletContext().getRealPath("/WEB-INF/Config.xml");
This line of code give this Error stacktrace.

C:\Documents and Settings\pravin\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UNDP-MIS\WEB-INF\Config.xml
SuperC:\Documents and Settings\pravin\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UNDP-MIS\WEB-INF\Config.xml
asdf-->C:\Documents and Settings\pravin\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UNDP-MIS\WEB-INF\Config.xml
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.CONFIG.ConfigHandler.initObjects(ConfigHandler.java:41)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.CONFIG.ConfigHandler.<init>(ConfigHandler.java:25)
at DEVELOPMENT.PACKAGES.COMMON.DISIPL.COMMON.UTILITY.Connections.Connect(Connections.java:131)
at org.apache.jsp.Login_jsp._jspService(Login_jsp.java:180)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)

ConfigHandler expects a String value.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is that string supposed to be in the format of a URL?

It looks like it's treating the "c:\" part as a scheme.
Maybe you need to append "file://" to the beginning.
[ April 08, 2008: Message edited by: Ben Souther ]
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

thanks for all of your help. The problem is now sorted out. actually i was using jdk1.6 and trying the parse the xml file using SAX so it was giving the error as there is a bug in jdk1.6. The link is as follows

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6506304

i solved it by creating the environment variable where i store the xml files physical path then in the connections.java class i read the environment variable and pass it to the confighandler.java and it did the trick
Here is the code for the above.
String path=System.getenv("CONFIG_PATH").toString();//read the environment variable named CONFIG_PATH
filePath=new File(path).toURI().toString();
and pass it on to the ConfigHandler
ConfigHandler config=new ConfigHandler(filePath);

but now i have got one more prob is that i need to deploy it on the linux redhat 5 server.
over there also i have declared the environment variable with the same but it does not reads the file when i deploy the project as a war in tomcat 5.5 but when i tried to test it with a sample jsp it read the file. can any one give any hint. i have given 777 rights for that war file so root can execute the project.
any suggestions??
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Hello Pravin:

That was not a SAX parser exception . It was because it was not able to find the file.

Setting an environment variable is not a good approach , I think.This would add complexity to the installation script which would install the application.

Is there any constructor available for ConfigHandle which takes an inputstream as parameter.

You might then want to use ServletContext's getResourceAsStream method and path of you config.xml file as parameter to it.

Have you tried the approach mentioned by Ulf. Make sure that your application is not deployed as war.
[ April 10, 2008: Message edited by: Rahul Bhattacharjee ]

Rahul Bhattacharjee
LinkedIn - Blog
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

hi Rahul,
Yes i have tried the method suggested by Ulf but it didn't solve my problem so i used a different approach. now i want to deploy my web application as a war on linux readhat 5 as a war on tomcat 5.5 but its not running neither it gives any error. but when i try the same code as a Test.jsp it works fine. i have already set the environment variable. any suggestions.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Pravin,
If you have a new issue, you'd be better off to start a new thread.
Threads with this many posts don't tend to be read by anyone except those who've already posted to it.
 
 
subject: FileNotFoundException is thrown when i try to read a file?