Re: [jnc-users] [javaCompiler-users] compiles and executes but throws java exception --help
Status: Beta
Brought to you by:
soapy
From: Marco T. <mt...@gm...> - 2007-02-15 08:02:11
|
Please always do "reply all", else we loose the mailinglist... Steven Blade wrote: > ok sure thing but please show me where to use the static block of code ok. Thanks for the sample. Just put that code in an own class (as the attached XmlLoader.java) and add this class to the JNC compilation source. This will ensure that the needed objects are pulled into the binary. You do not have to change your source. > did it work both ways with you reading and transforming xml ? Reading works, writing (transforming) fails. I'll create a bugreport (it's probably a GNU classpath bug) and hope that the fix will be in JNC 1.2. > one more thing does that mean i have to use gnu's jar instead of java's ? No, you only have to ensure that your binary contains the xml stuff. This is done with the static initializer since it references the xml stuff and thus ld - the linker - will add them. Marco > import java.io.BufferedReader; > import java.io.File; > import java.io.InputStreamReader; > > import javax.xml.parsers.SAXParser; > import javax.xml.parsers.SAXParserFactory; > import javax.xml.transform.OutputKeys; > import javax.xml.transform.Transformer; > import javax.xml.transform.sax.SAXTransformerFactory; > import javax.xml.transform.sax.TransformerHandler; > import javax.xml.transform.stream.StreamResult; > > import org.xml.sax.Attributes; > import org.xml.sax.SAXException; > import org.xml.sax.helpers.AttributesImpl; > import org.xml.sax.helpers.DefaultHandler; > > > public class Foo extends DefaultHandler{ > > /** > * @param args > */ > private static File file; > private static String contents; > private static Foo f=new Foo(); > public static void main(String[] args) { > // TODO Auto-generated method stub > System.out.print("enter location of xml file to read "); > BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); > try > { > String sfile=input.readLine(); > file=new File(sfile); > readXml(); > System.out.print(contents); > writeXml(); > }catch(Exception _){_.printStackTrace();} > } > private static void writeXml() > { > SAXTransformerFactory factory=(SAXTransformerFactory)SAXTransformerFactory.newInstance(); > try{ > StreamResult stream=new StreamResult(System.out); > TransformerHandler handler=factory.newTransformerHandler(); > Transformer trans=handler.getTransformer(); > trans.setOutputProperty (OutputKeys.ENCODING,"ISO-8859-1"); > trans.setOutputProperty(OutputKeys.INDENT,"yes"); > handler.setResult(stream); > handler.startDocument(); > AttributesImpl atts=new AttributesImpl(); > handler.startElement("", "", "test", atts); > atts.clear(); > handler.startElement("", "", "foo", atts); > handler.characters(new char[]{'a','b','c'}, 0, 3); > handler.endElement("", "", "foo"); > handler.endElement("", "", "test"); > handler.endDocument(); > }catch(Exception _){_.printStackTrace();} > } > > private static void readXml() > { > System.out.println("reading xml..."); > try{ > SAXParserFactory factory=SAXParserFactory.newInstance(); // the exception points to this line > factory.setValidating(false); > SAXParser parser=factory.newSAXParser(); > parser.parse (file, f.new OwnHandler()); > } > catch(Exception _){_.printStackTrace();} > } > > private class OwnHandler extends DefaultHandler{ > > public void startElement(String namespaceURI, > String sName, > String qName, > Attributes attrs) throws SAXException{ > contents+="<"+qName+">"; > } > public void endElement(String uri, String localName, String qName) throws SAXException{ > contents+="<"+qName+"/>"; > } > public void characters(char[] buf,int offset,int length)throws SAXException{ > contents+=new String(buf,offset,length); > } > } > } > > > > > ------------------------------------------------------------------------ > > <?xml version=1.0 encoding=UTF-8?> > <test> > <line>testing java native compiler</line> > </test> |