Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to slove follwoing error "Unreported exception java.io.IOException;"

843834Nov 14 2003 — edited Nov 18 2003
Currently I'm using following:



XP Professional

J2sdk1.4.2_01

Xerces-2_5_0

Xalan-j_2_5_1

Jakarta-tomcat-4.1.27

Jdom-b9



Current Classpath setting


User variables



PATH = c:\bmrt2.5\bin; c:\j2sdk\bin;%PATH%;%JAVA_HOME%\bin;



CLASSPATH=.;c:\xerces\xmlParserAPIs.jar;c:\xerces\xercesImpl.jar;
c:\xerces\xercesSamples.jar;c:\xalan\xercesImpl.jar;c:\xalan\xmlapis.jar;c:\xalan\xalan.jar;c:\tomcat\lib\webserver.jar;c:\tomcat\lib\jasper.jar;c:\tomcat\lib\xml.jar;c:\tomcat\common\lib\serlet.jar;c:\tomcat\lib\tools.jar; c:\tomcat\lib\tools.jar;c:\jdom\build\jdom.jar;

CATALINA_HOME= c:\tomcat
JAVA_HOME= c:\j2sdk

System variables
PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
c:\j2sdk\bin;%Path%;%JAVA_HOME%\bin;

CLASSPATH=.;c:\xerces\xmlParserAPIs.jar;c:\xerces\xercesImpl.jar;
c:\xerces\xercesSamples.jar;c:\xalan\xercesImpl.jar;c:\xalan\xmlapis.jar;c:\xalan\xalan.jar;c:\tomcat\lib\webserver.jar;c:\tomcat\lib\jasper.jar;c:\tomcat\lib\xml.jar;c:\tomcat\common\lib\serlet.jar;c:\tomcat\lib\tools.jar; c:\tomcat\lib\tools.jar;

CATALINA_HOME= c:\tomcat
JAVA_HOME= c:\j2sdk


Program

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.jdom.*;
import org.jdom.Element;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;
import org.jdom.input.SAXBuilder;
import org.jdom.Attribute;
import java.util.List;

public class AddToList extends HttpServlet
{
public Document getDocument(File sourceFile, PrintWriter errorsOut)
{
try
{
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(sourceFile);
return document;
}
catch(JDOMException e)
{
errorsOut.print("there was a problem building the document: " e. getMessage()"<br/>"+ "Returning blank document.");
return new Document(new Element("blank"));
}
}

public void saveDocument(Document saveDoc, File saveFile, PrintWriter errorsOut)
{
try
{
FileOutputStream outStream= new FileOutputStream(saveFile);
XMLOutputter outToFile= new XMLOutputter(" ",true);
outToFile.output(saveDoc, outStream);
outStream.flush();
outStream.close();
}
catch (IOException e)
{
errorsOut.print("Can't save order.xml: " + e.getMessage()+"<br />");
}
}

public void addItem(Element orderElement, String product_id, String quant)
{
Element newItem =new Element("item");
newItem.setAttribute("product_id", product_id);
Element quantity =new Element("quantity");
quantity.addContent(quant);
newItem.addContent(quantity);
orderElement.addContent(newItem);
}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException
{
String thisProduct_id=request.getParameter("addProduct_id");
String thisQuantity=request.getParameter("addQuantity");
response.setContentType("text/html");

PrintWriter out= response.getWriter();
out.print("Adding "+ thisQuantity + "of item to chart "+ thisProduct_id +" ... ");

File orderFile = new File ("file:///c:/jdk1.3/orders.xml");
if(!orderFile.exists())
{
out.print("Creating ordersrocks....<br />");
Element root= new Element("orders");
root.addContent(" ");
Document document = new Document(root);
saveDocument (document, orderFile, out);
}
else
{
out.print("Orders File Exists.");
}
Document document =getDocument(orderFile, out);

HttpSession session =request.getSession(true);
String session_id= session.getId();

Element root =document.getRootElement();
List orders =root.getChildren();

int num_orders =orders.size();
boolean orderExists =false;
int orderIndex =0;
for(int i=0; i<num_orders; i++)
{
Element iOrder=(Element)orders.get(i);
String iOrder_id=iOrder.getAttributeValue("order_id");
if (iOrder_id.equals(session_id))
{
orderExists=true;
orderIndex=i;
break;
}
}
if(!orderExists)
{
Element order =new Element("order");
order.setAttribute("order_id", session_id);
Element status =new Element("order_status");
status.setText("open");
order.addContent(status);
addItem(order, thisProduct_id, thisQuantity);
root.addContent(order);
}
else
{
Element thisOrder=(Element)orders.get(orderIndex);
boolean itemExists=false;
int itemIndex =0;
List items =thisOrder.getChildren("item");
int num_items =items.size();
for(int i=0; i<num_items; i++)
{
Element iItem=(Element)items.get(i);
String iProduct_id =iItem.getAttribute("product_id").getValue();
if(iProduct_id.equals(thisProduct_id))
{
itemExists =true;
itemIndex =i;
break;
}
}
if(itemExists)
{
Element thisItem=(Element)items.get(itemIndex);
String currentQuantity= thisItem.getChildText("quantity");
int newQuantity = Integer.parseInt(currentQuantity)+ Integer.parseInt(thisQuantity);
String strQuantity= new String().valueOf(newQuantity)+1;
thisItem.getChild("quantity").setText(strQuantity);
}
else
{
addItem(thisOrder, thisProduct_id, thisQuantity);
}
}

saveDocument (document, orderFile, out);

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}

}

When I compile above program, it gives me following error.

Error

C:\tomcat\webapps\book\WEB-INF\classes>javac AddToList.java

AddToList.java:19: unreported exception java.io.IOException; must be caught
or declared to be thrown

Document document = builder.build(sourceFile);

^
1 error

Any help regarding this will be appreciated

Thank you

Rocks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 16 2003
Added on Nov 14 2003
2 comments
402 views