Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: is that possible to include a small PNG file as part of Java class? Date: Mon, 23 Jun 2014 10:39:20 -0700 Organization: A noiseless patient Spider Lines: 36 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 23 Jun 2014 17:39:23 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="9358cda03e1e0dfd3c4b757daa608707"; logging-data="1292"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18saWQoCV72TO2h8Mqx4e7nXF/qD2BBZyM=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: Cancel-Lock: sha1:FgCVoNGWe22XyUSlAq4YirfhfUg= Xref: csiph.com comp.lang.java.programmer:28825 On 6/23/2014 9:02 AM, www wrote: > I have a simple Java class: Abc.java. The method within it needs to > access a small PNG file(it's an icon picture). Right now, this > picture file("abc.png") is located at nonsrc/icons/abc.png ^^^^^^ > I am wondering if there is a way that I can "convert" abc.png into > some sort of Java code and I can put it in Abc.java. So Abc.class > alone can perform the job, without the need of presence of abc.png. > Is that possible? I think Marcel has the right of it. You should be using getResource() (or getResourceAsStream()) to load this file. However the basic problem is that you need to include the file. You should be using a build script that automatically copies the file. Most IDEs will do this *if you put the image in the source directory.* I see here you've deliberately excluded the image from the source. That's normally wrong. Try something like: src/my/stuff/Abc.java src/my/stuff/abc.png Most build system will now automatically figure out that you want the PNG file to be included in the Jar file, and do the copy for you. If you're using entirely home-rolled tools, you'll have to do the copying yourself, but at least add it to an automated build script so you don't have to do the copy manually each time. Notice on this page how they do that exact thing just by calling getResource(): http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html