Tag: embedded resource

Java Path ImageIcon URL .JAR

我可能已经尝试过了,没有人工作过…… 该文件是: /Users/Toto/Desktop/Titi/IUT/Java/TP2/project/src/fichierPointJava/img1.png fichierPointJava是包的名称。 当我位于包含build.xml的项目中时,我启动了ant 以下是我测试的代码: URL urlImage1=this.getClass().getClassLoader.getResource(“/src/fichierPointJava/img1.png”); URL urlImage1=this.getClass().getClassLoader.getResource(“/fichierPointJava/img1.png”); URL urlImage1=this.getClass().getClassLoader.getResource(“fichierPointJava/img1.png”); URL urlImage1=this.getClass().getClassLoader.getResource(“/img1.png”); URL urlImage1=this.getClass().getClassLoader.getResource(“img1.png”); System.out.println(“Value = “+ urlImage1); 无论有没有this ,我有一个遗嘱,有或没有getClassLoader() 希望有人能帮助我。 谢谢

在jar文件中包含一个文本文件并将其读取

可能重复: Java资源作为文件 我是Java的新手,我试图在Jar文件中获取一个文本文件。 在我执行我的jar时,我必须将我的文本文件放在与jar文件相同的文件夹中。 如果文本文件不存在,我将得到一个NullPointerException ,我想避免。 我想要做的是在jar中获取txt文件,所以我不会遇到这个问题。 我尝试了一些指南,但它们似乎没有用。 我目前的读取function如下: public static HashSet readDictionary() { HashSet toRet = new HashSet(); try { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(“Dictionary.txt”); try (DataInputStream in = new DataInputStream(fstream)) { BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File […]

显示Windows 7的ImageIcon png文件的正确路径是什么?

我想测试一个带有简单png图像的程序。 我写了一个简短的程序来做到这一点,但我似乎无法让路径正确。 我已经检查,再次检查,重新检查,并检查了我的路径名称四倍,但没有正确,但无论我做什么,这个图像都不会显示。 我使用了Oracle在ImageIcon文档( creaetImageIcon() )中编写的一个简短类来完成此任务,但它似乎没有帮助。 我将在下面发布整个程序,因为它很短。 package practiceImages; import java.awt.BorderLayout; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class ImageIconGUITest { public static void main(String[] args) { ImageIconGUITest gui = new ImageIconGUITest(); gui.display(); } private ImageIcon createImageIcon(String path, String description) { java.net.URL imgURL = getClass().getResource(path); if (imgURL != null) { return new ImageIcon(imgURL, […]

从.jar文件中获取图像

当我在eclipse中导出可执行jar文件时,我还需要获取res文件夹,当我使用getClass().getResource()方法时它不起作用。 当前读取图像代码 public Image loadImage(String fileName) { return new ImageIcon(fileName).getImage(); } 代码不起作用 public Image loadImage(String fileName) { return new ImageIcon(getClass().getResource(fileName).getImage(); }

访问JAR资源

我有一个jar文件,其中包含我要分发的资源(主要是缓存,日志记录等配置)。 我对这些资源的相对路径有问题,所以我做了我在另一个stackoverflow问题中发现的问题,该问题说这是一种有效的方法: ClassInTheSamePackageOfTheResource.class.getResourceAsStream(‘resource.xml’); 可悲的是,这不起作用。 有任何想法吗? 谢谢! PS:显然我不能使用绝对路径,如果可能的话我想避免使用环境变量