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 to read embedded resource text file

You can use the Assembly.GetManifestResourceStream Method: Add the following usings using System.IO; using System.Reflection; Set property of relevant file: Parameter Build Action with value Embedded Resource Use the following code var assembly = Assembly.GetExecutingAssembly(); var resourceName = “MyCompany.MyProduct.MyFile.txt”; using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd(); } … Read more

Reading a resource file from within jar

Rather than trying to address the resource as a File just ask the ClassLoader to return an InputStream for the resource instead via getResourceAsStream: try (InputStream in = getClass().getResourceAsStream(“/file.txt”); BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { // Use resource } As long as the file.txt resource is available on the classpath then this approach will … Read more

Loading resources like images while running project distributed as JAR archive

First of all, change this line : image = ImageIO.read(getClass().getClassLoader().getResource(“resources/icon.gif”)); to this : image = ImageIO.read(getClass().getResource(“/resources/icon.gif”)); More info, on as to where lies the difference between the two approaches, can be found on this thread – Different ways of loading a Resource For Eclipse: How to add Images to your Resource Folder in the Project … Read more