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: Eric Sosman 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 13:03:37 -0400 Organization: A noiseless patient Spider Lines: 71 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:03:38 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="17046"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/yVc1qVyHFlNYtc/A1yKTd" 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:1AF4DmAXEkS7xPFwzbLWrIrJaEM= Xref: csiph.com comp.lang.java.programmer:28824 On 6/23/2014 12:02 PM, www wrote: > Hi: > > 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 > > So this class is a bit pain. Abc.class won't work in the absence of abc.png. When I create an executable JAR, this is pain: always need to remember to include abc.png file inside it. > > 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? > > Hope I have explained my intention well. Martin Gregorie's response seems to be good advice. However, if you simply *must* incorporate your PNG image as a class, you could do so with a big byte[] array: private static final byte[] PNG_BUFFER = { (byte)0x89, 0x50, 0x4E, 0x47, ... }; public static Image getMyPNG() { return ImageIO.read(new ByteArrayInputStream(PNG_BUFFER)); } However, this approach will only work for fairly small images. The problem is that Java class files have no way to store an initialized array: The byte[] array above actually compiles to the equivalent of private static final byte[] PNG_BUFFER = new byte[whatever]; static { PNG_BUFFER[0] = (byte)0x89; PNG_BUFFER[1] = 0x50; PNG_BUFFER[2] = 0x4E; PNG_BUFFER[3] = 0x47; ... } ... with about four or so bytecode instructions per array element. At that rate of bloat, even a modest-sized image risks generating more code than a Java method can hold. You might evade the limits by writing the initialization explicitly and breaking it up into multiple methods: private static final byte[] PNG_BUFFER = new byte[whatever]; static { firstK(); secondK(); ... } private static void firstK() { PNG_BUFFER[0] = (byte)0x89; PNG_BUFFER[1] = 0x50; PNG_BUFFER[2] = 0x4E; PNG_BUFFER[3] = 0x47; ... PNG_BUFFER[1023] = whatever; } private static void secondK() { PNG_BUFFER[1024] = whatever; ... PNG_BUFFER[2047] = whatever; } ... IMHO, no sane person would do this. -- Eric Sosman esosman@comcast-dot-net.invalid