在jar中加载jpeg图像 [英] Loading a jpeg image within a jar
本文介绍了在jar中加载jpeg图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这个问题已被回答了太多次,但无论解决方案我尝试过,我无法修复它。
我想用jpeg图像作为背景但我无法解决它不管我试过。
I know that this question had been answered too many times but I couldn't fix it no matter solution I tried.
I want to use a jpeg image as background but I can't resolve it no matter I tried.
下面是我的最终包结构:
Below is my final package structure :
images/
-- bg.jpeg
org/
-- Main.java (used for test)
代码
Code
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load(){
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:"+cl);
InputStream url = getClass().getClassLoader().getResourceAsStream("/images/bg.jpg");
System.out.println("URL:"+url);
this.img = ImageIO.read(url); // Null argument exception
} catch (IOException ex) {
Logger.getLogger(BoardView.class.getName()).log(Level.SEVERE, null, ex);
}
}}
输出
Output
CL:sun.misc.Launcher$AppClassLoader@15663a2
URL:null
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at org.Main.load(Main.java:32)
at org.Main.main(Main.java:24)
我正在使用JDK7和Maven项目。
I am using JDK7 and Maven project.
推荐答案
你的图像路径有点正确......
You're image path is somewhat correct...
当使用 getClassLoader()
时,你不使用额外的 /
在图像前面
。
When using getClassLoader()
, you don't use the extra /
in front of images
.
getClass().getClassLoader().getResourceAsStream("images/bg.jpg");
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load() {
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:" + cl);
InputStream url = getClass().getClassLoader().getResourceAsStream("resources/stackoverflow5.png");
System.out.println("URL:" + url);
this.img = ImageIO.read(url); // Null argument exception
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)), "No ClassLoader", JOptionPane.PLAIN_MESSAGE);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
如果你_don't不使用 getClassLoader()
,那么你做需要它
getClass().getResourceAsStream("/images/bg.jpg");
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load() {
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:" + cl);
InputStream url = getClass().getClass().getResourceAsStream("/resources/stackoverflow5.png");
System.out.println("URL:" + url);
this.img = ImageIO.read(url); // Null argument exception
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)), "With ClassLoader", JOptionPane.PLAIN_MESSAGE);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
为什么?...
尽可能简单的状态这里
- 使用
.getClass()。getResource(fileName)
它认为fileName的
位置与调用
类的位置相同。 - 当您使用
时.getClass()。getClassLoader()。getResource(fileName)
it
从根开始考虑fileName的位置
- When you use
.getClass().getResource(fileName)
it considers the location of the fileName is the same location of the of the calling class. - When you use
.getClass().getClassLoader().getResource(fileName)
it considers the location of the fileName from the root
注意:我的文件结构类似于您的
Note: My file structure is similar to your
ProjectRoot
resources
stackoverflow5.png
mypackage
Main.java
这篇关于在jar中加载jpeg图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文