Hi there,
I do have a question where my knowledge is too limited: How can I ready Java RESOURCE Files from a JAR-Archive (in NetRexx)? Any hint will be appreciated :-) Thomas. -- Thomas Schneider (www.thsitc.com) _______________________________________________ Ibm-netrexx mailing list [hidden email]
Thomas Schneider, Vienna, Austria (Europe) :-)
www.thsitc.com www.db-123.com |
Here is one way:
Get a File object for the JAR and use it to create a JarFile object, then you can read from it by creating a Stream from the JarEntry elements. Here is a sample from code I used to process a JAR and copy some of the files to a directory: do jf=JarFile(getPluginJAR.getFile) entries = jf.entries() loop while entries.hasMoreElements() je=JarEntry entries.nextElement() if Rexx(je.getName).translate(fs,'/').pos('NetRexx'fs)>0 then copyjarentry(jf,je,setdir) -- another Java "gotcha" - jar entries have unix file separators thus requireing the translate above end catch err=Exception say "NetRexxScript.jar ==>" err end Here is the nitty gritty code that makes it work. Probably not the best way to do it but I used an unbuffered byte copy for my binary files and buffered for the rest: ------------------------------------------------------ general purpose routines ------------------------------------------------------- method copyjarentry(jf=JarFile,je=JarEntry,scriptdir=String) -- method to copy a jarfile entry to a directory if df then trace results nrscript=scriptdir||fs||je.getName nrs=File(nrscript) if \nrs.exists then do rc=copyjarentrytofile(jf,je,nrs) if rc=1 then say je.getName "copied to:" scriptdir end method copyjarentrytofile(jf=JarFile,je=JarEntry,nrs=File) if df then trace results do scriptstream=jf.getInputStream(je) if Rexx(je.getName).pos(".class")=0 then do scriptreader=BufferedReader(InputStreamReader(scriptstream)) scriptwriter=BufferedWriter(FileWriter(nrs)) copyfile(scriptreader,scriptwriter) scriptwriter.close end else do outstream=FileOutputStream(nrs) copybin(scriptstream,outstream) outstream.close end catch badguy=Exception say je.getName "copy error =" badguy return 0 end return 1 /* methods to copy a file: */ method copyfile(ifq=java.io.BufferedReader,ofq=java.io.BufferedWriter) signals IOException if df then trace results line=ifq.readline if line = null then return ofq.write(string line,0,line.length) trace off loop forever line=ifq.readline if line = null then leave ofq.newline() ofq.write(string line,0,line.length) end method copybin(ifq=InputStream,ofq=OutputStream) binary signals IOException bite=ifq.read loop while bite \= -1 ofq.write(bite) bite=ifq.read end -- Kermit On 3/9/2011 12:42 PM, Thomas Schneider wrote: > Hi there, > I do have a question where my knowledge is too limited: > > How can I ready Java RESOURCE Files from a JAR-Archive (in NetRexx)? > > Any hint will be appreciated :-) > Thomas. > _______________________________________________ Ibm-netrexx mailing list [hidden email] |
Thanks a lot, Kermit !
I will try this, yours technique tomorrow. Time to go a'sleep now... Cheers, Tom. ================================================= Am 09.03.2011 22:57, schrieb Kermit Kiser: > Here is one way: > > Get a File object for the JAR and use it to create a JarFile object, > then you can read from it by creating a Stream from the JarEntry > elements. > > Here is a sample from code I used to process a JAR and copy some of > the files to a directory: > > > do > jf=JarFile(getPluginJAR.getFile) > entries = jf.entries() > loop while entries.hasMoreElements() > je=JarEntry entries.nextElement() > if > Rexx(je.getName).translate(fs,'/').pos('NetRexx'fs)>0 then > copyjarentry(jf,je,setdir) > -- another Java "gotcha" - jar entries have unix file separators > thus requireing the translate above > end > catch err=Exception > say "NetRexxScript.jar ==>" err > end > > > Here is the nitty gritty code that makes it work. Probably not the > best way to do it but I used an unbuffered byte copy for my binary > files and buffered for the rest: > > ------------------------------------------------------ general purpose > routines ------------------------------------------------------- > > method copyjarentry(jf=JarFile,je=JarEntry,scriptdir=String) -- > method to copy a jarfile entry to a directory > if df then trace results > nrscript=scriptdir||fs||je.getName > nrs=File(nrscript) > if \nrs.exists then do > rc=copyjarentrytofile(jf,je,nrs) > if rc=1 then say je.getName "copied to:" scriptdir > end > > method copyjarentrytofile(jf=JarFile,je=JarEntry,nrs=File) > if df then trace results > do > scriptstream=jf.getInputStream(je) > if Rexx(je.getName).pos(".class")=0 then do > > scriptreader=BufferedReader(InputStreamReader(scriptstream)) > scriptwriter=BufferedWriter(FileWriter(nrs)) > copyfile(scriptreader,scriptwriter) > scriptwriter.close > end > else do > outstream=FileOutputStream(nrs) > copybin(scriptstream,outstream) > outstream.close > end > catch badguy=Exception > say je.getName "copy error =" badguy > return 0 > end > return 1 > > /* methods to copy a file: */ > > method copyfile(ifq=java.io.BufferedReader,ofq=java.io.BufferedWriter) > signals IOException > if df then trace results > line=ifq.readline > if line = null then return > ofq.write(string line,0,line.length) > trace off > loop forever > line=ifq.readline > if line = null then leave > ofq.newline() > ofq.write(string line,0,line.length) > end > > method copybin(ifq=InputStream,ofq=OutputStream) binary signals > IOException > > bite=ifq.read > loop while bite \= -1 > ofq.write(bite) > bite=ifq.read > end > > > -- Kermit > > > On 3/9/2011 12:42 PM, Thomas Schneider wrote: >> Hi there, >> I do have a question where my knowledge is too limited: >> >> How can I ready Java RESOURCE Files from a JAR-Archive (in NetRexx)? >> >> Any hint will be appreciated :-) >> Thomas. >> > -- Thomas Schneider (www.thsitc.com) _______________________________________________ Ibm-netrexx mailing list [hidden email]
Thomas Schneider, Vienna, Austria (Europe) :-)
www.thsitc.com www.db-123.com |
I think it can be done a lot easier - use .getClass.getResourceAsStream(
When executing code from a jar, you can read files in the jar relative to classpath. This is explained, among many places, here: http://mindprod.com/jgloss/getresourceasstream.html
best regards, René.
On Thu, Mar 10, 2011 at 12:51 AM, Thomas Schneider <[hidden email]> wrote: Thanks a lot, Kermit ! _______________________________________________ Ibm-netrexx mailing list [hidden email] |
Thanks a lot, Rene!
Thomas. ==================================================== Am 10.03.2011 16:32, schrieb René Jansen: I think it can be done a lot easier - use .getClass.getResourceAsStream( --
Thomas Schneider (www.thsitc.com) _______________________________________________ Ibm-netrexx mailing list [hidden email]
Thomas Schneider, Vienna, Austria (Europe) :-)
www.thsitc.com www.db-123.com |
Free forum by Nabble | Edit this page |