Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar

I thought this question was already asked and answered! What is the difference between Class.getResource() and ClassLoader.getResource()? getClass().getResource() searches relative to the .class file while getClass().getClassLoader().getResource() searches relative to the classpath root. If there’s an SSCCE here, I don’t understand why it doesn’t 1) Show the directory organization in the .jar, and… 2) Take package … Read more

Eclipse exported Runnable JAR not showing images

Works fine for me. Check what you may have different. Example 1: (resources in src) Steps: File Structure Code package com.stackoverflow.test; import java.net.URL; import javax.swing.*; // Wild carded for brevity. // Actual code imports single classes public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { URL url = … Read more

Can I link a plain file into my executable? [duplicate]

You could do this: objcopy –input binary \ –output elf32-i386 \ –binary-architecture i386 my_file.xml myfile.o This produces an object file that you can link into your executable. This file will contain these symbols that you’ll have to declare in your C code to be able to use them 00000550 D _binary_my_file_xml_end 00000550 A _binary_my_file_xml_size 00000000 … Read more

Embed resources (eg, shader code; images) into executable/library with CMake

As an alternative to the answer of sfstewman, here’s a small cmake (2.8) function to convert all files in a specific folder to C data and write them in wished output file: # Creates C resources file from files in given directory function(create_resources dir output) # Create empty output file file(WRITE ${output} “”) # Collect … Read more

Java Swing: Displaying images from within a Jar

To create an ImageIcon from an image file within the same jars your code is loaded: new javax.swing.ImageIcon(getClass().getResource(“myimage.jpeg”)) Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL. To construct a URL for a resource in a jar not on your “classpath”, see the documentation for java.net.JarURLConnection.

How do I add an image to a JButton

I think that your problem is in the location of the image. You shall place it in your source, and then use it like this: JButton button = new JButton(); try { Image img = ImageIO.read(getClass().getResource(“resources/water.bmp”)); button.setIcon(new ImageIcon(img)); } catch (Exception ex) { System.out.println(ex); } In this example, it is assumed that image is in … Read more