Good afternoon, I'm trying to change the icon in the jar file. For this I use this design

public class Main extends JFrame { public static void main(String[] args){ Main.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getClassLoader().getResource("src/resources/imagesi/mylogo.png"))); } 

but for some reason it constantly underlines with red this. and writes

Can not use this in a static context

    2 answers 2

    To be honest, the first thing you want to do is just send to the nearest online translator. The car gave you the phrase "... static ..." directly, from which everything is clear.

    Your code inside main is executed outside of any instance of the class - main is a static method. Therefore, this in this context basically does not exist, which is what the machine tells you.
    To solve a problem, it’s enough to refer to the class by name, and not through this.getClass()

     Main.class.getClassLoader().getResource("src/resources/imagesi/mylogo.png") 

    Separately, I note that the resource is likely to be available as imagesi/mylogo.png without the src/resources prefix.

    • Main.setIconImage (Toolkit.getDefaultToolkit (). GetImage (Main.getClassLoader (). GetResource ("imagesi / mylogo.png"))); but with this construction, it emphasizes getClassLoader () and ask to create either the getClassLoader () method or change getClass () and when I change it it writesVarg Sieg
    • one
      @VargSieg Well, you ask what methods your class has and which class has getClassLoader () method, Here’s the first thing Google showed me about this word: docs.oracle.com/javase/7/docs/api/java/lang /Class.html - m. vokhm
    • @ m.vokhm honestly read but did not understand your idea my class has only one main method. "Which class has getClassLoader () in the sense? I need to create a separate class in which I need to define the getClassLoader () method and call it in the method main? - Varg Sieg
    • one
      Your method inherits all the methods that are defined for the Object class. getClassLoader() is not included in their number - it is defined by the Class method. See docs.oracle.com/javase/8/docs/api/java/lang/Class.html - m. vokhm
    • one
      You are trying to call getClassLoader() on your class - but there is no such method. You must first get its class (object of class Class ), and getClassLoader() be called from it. Etki clearly wrote to you: Main.class.getClassLoader() , and you write Main.getClassLoader() . - m. vokhm

    A complete example of a finished solution number 1:

     import java.awt.Toolkit; import javax.swing.JFrame; public class Main1 extends JFrame { /** * */ private static final long serialVersionUID = -4465997357046781244L; public static void main(String[] args) { final Main1 instance = new Main1(); /** * Относительный адрес файла изображения. Относительно местоположения * файла-класса. */ final String imgPath = "imagesi\\mylogo.png"; instance.setIconImage( Toolkit.getDefaultToolkit().getImage(Main1.class.getResource(imgPath))); instance.setVisible(true); } } 

    A complete example of a turnkey solution number 2:

     import java.awt.Toolkit; import java.io.File; import javax.swing.JFrame; public class Main2 extends JFrame { /** * */ private static final long serialVersionUID = -7810262901547572556L; public static void main(String[] args) { final Main2 instance = new Main2(); instance.setIconImage( Toolkit.getDefaultToolkit().getImage(new File("resources/imagesi/mylogo.png").toString())); instance.setVisible(true); } } 
    • one
      @VargSieg "You will most likely have the resource available as imagesi / mylogo.png without the src / resources prefix." When the program runs from the IDE, files in the path known to the system are available. When it works from the jar, the classloader has these paths (may be) inaccessible. And you need to make sure that all the necessary resources are packed in jar. You can open it and view the contents of any archiver. - m. vokhm
    • one
      If you want the program icon to be inside a * .jar file, then the use of the method is java1cprog
    • one
      Main1.class.getResource(imgPath) best option, since allows, if desired, to move within the file hierarchy and search for the desired file. java.net.URL url = Main1.class.getResource(imgPath); String filePath = url.getFile(); File file = new File(filePath); File parentDir = file.getParentFile(); //... java.net.URL url = Main1.class.getResource(imgPath); String filePath = url.getFile(); File file = new File(filePath); File parentDir = file.getParentFile(); //... - java1cprog
    • one
      @ varg-sieg All methods can be divided into three categories: \ s \ s 1. command line ( jar , howto utility); 2. means (wizards, options) of a specific IDE; 3. third-party utilities ( 7zip , for example). - java1cprog
    • one
      @ varg-sieg It depends on the "native means". You may need to make adjustments to the export configuration files in the * .jar file. - java1cprog