Jonathan, I checked in some code for you. The solution is just to use
absolute paths, not relative ones. This works for me both in Eclipse and
from the Jar file.
Bytes[] data =
getResourceAsBytes("org/openscience/jmol/app/images/angleButton.gif")
private static byte[] getResourceAsBytes(String fullPath) {
byte[] buf = new byte[1024];
byte[] bytes = new byte[4096];
BufferedInputStream bis = new BufferedInputStream(ClassLoader
.getSystemResourceAsStream(fullPath));
int len = 0;
int totalLen = 0;
try {
while ((len = bis.read(buf)) > 0) {
totalLen += len;
if (totalLen >= bytes.length)
bytes = ArrayUtil.ensureLength(bytes, totalLen * 2);
System.arraycopy(buf, 0, bytes, totalLen - len, len);
}
buf = new byte[totalLen];
System.arraycopy(bytes, 0, buf, 0, totalLen);
} catch (Exception IOException) {
Logger.error("WebPanel IO ERROR reading resource " + fullPath);
return null;
}
return buf;
}
On Wed, Apr 28, 2010 at 8:06 PM, Jonathan Gutow <[email protected]> wrote:
> I've run into a problem using some of the internal resources inside the
> Jmol.jar for WebExport. If anybody knows what I'm doing wrong please let me
> know.
>
> Running inside Eclipse my code works.
> As a compiled .jar I find that the resource URL is null instead of a usable
> value.
> Note it continues to work for things inside the WebExport.html part of the
> jar. I assume this may have to do with the fact that WebExport is below the
> resources I am trying to access in the jar structure. I have not been able
> to figure out what to do. Should I be getting the URL with a call like
> object.getParent...?
>
> Here's the piece of code.
>
> object = this (the WebExport object)
> fileName = ( button.png, which works for files in WebExport.html, or
> ../image/button.png or org/openscience/jmol/app/images/ neither of the last
> two work in the compiled .jar, but all three work in Eclipse)
> flagError=True.
>
> static URL getResource(Object object, String fileName, boolean flagError) {
> URL url = null;
> if (!fileName.contains("/"))fileName="html/"+fileName;
> try {
> if ((url = object.getClass().getResource(fileName)) == null &&
> flagError)
> System.err.println("Couldn't find file: " + fileName);
> } catch (Exception e) {
> System.err.println("Exception " + e.getMessage() + " in getResource "
> + fileName);
> }
> return url;
> }
>
> Thanks,
> Jonathan
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Jmol-developers mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jmol-developers
>
--
Robert M. Hanson
Professor of Chemistry
St. Olaf College
1520 St. Olaf Ave.
Northfield, MN 55057
http://www.stolaf.edu/people/hansonr
phone: 507-786-3107
If nature does not answer first what we want,
it is better to take what answer we get.
-- Josiah Willard Gibbs, Lecture XXX, Monday, February 5, 1900
------------------------------------------------------------------------------
_______________________________________________
Jmol-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-developers