java - Unable to load image from Jar -
i'm trying load image jar file. here's line:
image imgtrayicon = new image(display, this.getclass().getresourceasstream("icon.ico")); i've seen many examples using method when try so, , error saying image invalid. here's stack trace:
[java] exception in thread "thread-0" org.eclipse.swt.swtexception: invalid image [java] @ org.eclipse.swt.swt.error(swt.java:4083) [java] @ org.eclipse.swt.swt.error(swt.java:3998) [java] @ org.eclipse.swt.swt.error(swt.java:3969) [java] @ org.eclipse.swt.internal.image.winicofileformat.loadinfoheader(winicofileformat.java:200) [java] @ org.eclipse.swt.internal.image.winicofileformat.loadicon(winicofileformat.java:127) [java] @ org.eclipse.swt.internal.image.winicofileformat.loadfrombytestream(winicofileformat.java:119) [java] @ org.eclipse.swt.internal.image.fileformat.loadfromstream(fileformat.java:48) [java] @ org.eclipse.swt.internal.image.fileformat.load(fileformat.java:84) [java] @ org.eclipse.swt.graphics.imageloader.load(imageloader.java:130) [java] @ org.eclipse.swt.graphics.imagedataloader.load(imagedataloader.java:22) [java] @ org.eclipse.swt.graphics.imagedata.<init>(imagedata.java:331) [java] @ org.eclipse.swt.graphics.image.<init>(image.java:545) [java] @ systray.run(unknown source) the icon i'm using valid. i've checked using icon tools. i've tried placing icon in same directory code (not jar-ing it) , using this:
image imgtrayicon = new image(display, "icon.ico"); this works fine, when try put in jar, doesn't. can't seem figure out why happening. i've uncompressed jar check whether file added jar , seems there. jar doesn't has complex folder structure. files , resources in same level of tree.
any ideas on what's wrong here? thanks
here's sample code replicate issue:
example.java
import org.eclipse.swt.widgets.*; import org.eclipse.swt.graphics.*; class example { public static void main(string[] args) { display display = display.getdefault(); image imgtrayicon = new image(display, example.class.getclassloader().getresourceasstream("icon.ico")); } } commands:
javac -cp "swt.jar" example.java jar cf example.jar *.class *.ico java -cp "example.jar;swt.jar" example
seems working on multi-threaded application. stack-trace exception in thread "thread-0" org.eclipse.swt.swtexception: invalid image appears loading image in non-ui thread , trying use in ui element. see this.
within ui thread below code works fine. (the icon file inside test package)
package test; import java.io.inputstream; import org.eclipse.swt.graphics.image; import org.eclipse.swt.layout.filllayout; import org.eclipse.swt.widgets.display; import org.eclipse.swt.widgets.shell; public class icontest { public static void main(string[] args) { final display display = new display(); shell shell = new shell(display); shell.setlayout(new filllayout()); shell.setsize(200, 200); shell.setlocation(20, 20); inputstream stream = icontest.class.getresourceasstream("/test/icon.ico"); image imgtrayicon = new image(display, stream); shell.setimage(imgtrayicon); shell.open(); while (!shell.isdisposed()) { if (!display.readanddispatch()) display.sleep(); } if(imgtrayicon != null) imgtrayicon.dispose(); display.dispose(); } }
Comments
Post a Comment