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 13:01:51
|
Steven Blade wrote: > >package ch.mtSystems.jnc.model.utilities; > *and the package declaration should be the same as you gave me right ? No. It doesn't matter in which class or package it is. The only thing that matters is that the compiler runs into statements like: gnu.xml.dom.ls.SAXEventSink.class.getName(); So that he knows to include gnu.xml.dom.ls.SAXEventSink because you need it. Did you read the manual part about that I told you? It's all explained in there... > i > just create this class in my project in this package after adding the > gnu jar and that's it.* You don't have to do that. Just put the class I gave you or one that you made yourself somewhere on your drive and add it only to the compilation. Don't add a jar to the project or that file to the project when you're developing. It's not necessary and not needed. It's only needed for native compilation and only a hint to the compiler. You won't need it ever... > *but no support for writing xml yet , oh well.* Definitely not the way you did in your sample applications. There probably are hundreds of other ways that will work... > *Maroc thanks alot for your time man, if you have any other suggestions > please tell me .* You could always hire me to fix it or find another solution. Of course that would cost something... Contact me in private if you're interested... Marco > *Thanks* > > On 2/15/07, *Marco Trudel* <mt...@gm... <mailto:mt...@gm...>> wrote: > > 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 <http://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> > > > package ch.mtSystems.jnc.model.utilities; > > public class XmlLoader > { > static > { > gnu.xml.dom.ls.SAXEventSink.class.getName(); > gnu.xml.transform.TransformerFactoryImpl.class.getName(); > gnu.xml.xpath.XPathFactoryImpl.class.getName(); > org.xml.sax.ContentHandler.class.getName(); > gnu.xml.stream.SAXParserFactory.class.getName (); > } > } > > > > > -- > kc |