Include image in jar file with java static method

A memo when you are in trouble because the image is not included when you create a jar file using Eclipse.

problem

The image is not displayed even if I create a jar file. The method to read the image is static. If this is left as it is, the image will not be included in the jar file.

Main.java


public static ImageIcon getImageIcon() {
    ImageIcon icon = new ImageIcon("nanase.jpg ");
    return icon;
}

solution

Take a class as an argument

Main.java


public static ImageIcon getImageIcon(Main m) {
    ClassLoader cl = m.getClass().getClassLoader();
    ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
    return icon;
}

Solution when not static

Reference → https://virtualwalk.hatenadiary.org/entry/20121013/1350127275

Main.java


public ImageIcon getImageIcon() {
    ClassLoader cl = this.getClass().getClassLoader();
    ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
    return icon;
}

reference

・ Htps: // er tsua lk. Hatena Jia ry. Rg / Entry / 20120113/1350127275 ・ Htps: // ww. Well then. jp / Tsutoria l / Imageikon / Inde x2. html -Creating a jar with eclipse https://java.keicode.com/lang/how-to-compile-jar-with-eclipse.php

The entire

Main.java


package include;
import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JFrame{

	  public static void main(String[] args){
	    Main frame = new Main();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setBounds(10, 10, 150, 150);
	    frame.setTitle("title");
	    frame.setVisible(true);
	  }

	  Main(){
//	    ImageIcon icon = this.getImageIcon();
	    ImageIcon icon = Im.getImageIcon(this);
	    JLabel label = new JLabel(icon);

	    JPanel p = new JPanel();
	    p.add(label);

	    getContentPane().add(p, BorderLayout.CENTER);
	  }

//	  private ImageIcon getImageIcon() {
//		  ClassLoader cl = this.getClass().getClassLoader();
//		  ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
//		  return icon;
//	  }
	}


class Im{
	public static ImageIcon getImageIcon(Main m) {
	  ClassLoader cl = m.getClass().getClassLoader();
	  ImageIcon icon = new ImageIcon(cl.getResource("nanase.jpg "));
	  return icon;
	 }
}

Recommended Posts

Include image in jar file with java static method
Include image resources in jar file
Concurrency Method in Java with basic example
Read xlsx file in Java with Selenium
Read a string in a PDF file with Java
Static code analysis with Checkstyle in Java + Gradle
Include external jar in package with Spring boot2 + Maven3
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
[Java] JavaConfig with Static InnerClass
Benefits of Java static method
[Java, Scala] Image resizing with ImageIO
Morphological analysis in Java with Kuromoji
Read Java properties file in C #
Automatic photo resizing method in Java
How to make a jar file with no dependencies in Maven
Unzip the zip file in Java
Try using Redis with Java (jar)
Log output to file in Java
EXCEL file update sample with JAVA
About file copy processing in Java
Play with Markdown in Java flexmark-java
Call the super method in Java
For the time being, run the war file delivered in Java with Docker
[Java] Create a jar file with both compressed and uncompressed with the jar command
Generate source code from JAR file with JD-GUI of Java Decompiler project
How to read log4j configuration file in Java project summarized in jar file Memo
[Java] How to compare with equals method
Static file access priority in Spring boot
How to add jar file in ScalaIDE
What is the main method in Java?
Split a string with ". (Dot)" in Java
Working with huge JSON in Java Lambda
Sample to unzip gz file in Java
Element operation method in appium TIPS (Java)
Java method
java (method)
Java static
include method
Java method
[Java] method
[Java] method
Java that outputs vmg format file in eml format
[Java] Handling of JavaBeans in the method chain
Refactored GUI tools made with Java8 + JavaFX in 2016
Java method call from RPG (method call in own class)
Text extraction in Java from PDF with pdfbox-2.0.8
Dynamically change private static final fields in Java
Method name of static factory method learned from Java 8
A bat file that uses Java in windows
Check static and public behavior in Java methods
[Java] Read the file in src / main / resources
Refactor property access handling with Java Method Util
Practice working with Unicode surrogate pairs in Java
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
Method name of method chain in Java Builder + α
Check method call arguments in blocks with RSpec
Encrypt / decrypt with AES256 in PHP and Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Programming with direct sum types in Java (Neta)
[Java] Convert and import file values with OpenCSV