简体   繁体   English

访问.jar中的数据

[英]Accessing Data inside of a .jar

I am currently putting a program into a .jar, and have difficulties telling it where to get its data from. 我目前正在将一个程序放入.jar中,并且难以告诉它从哪里获取数据。 The data was inside of a file in the project, and I am sure that it is located in the jar as well. 数据位于项目中的文件内部,我确信它也位于jar中。 But I have no clue on how to get a path into a jar. 但我不知道如何获得一个罐子的路径。

I found the getClass().getClassLoader().getResourceAsStream() method online to get an input stream into the jar, but since I used FileReaders all the time, I dont know what to do with it as well.. 我在网上找到了getClass().getClassLoader().getResourceAsStream()方法来获取输入流到jar中,但由于我一直使用FileReaders ,所以我也不知道该如何处理它。

I`d be very thankful for any help. 我非常感谢任何帮助。

Edit: 编辑:

Here is a picture of how the directory is organized: 以下是目录组织方式的图片: 控制台显示解决方案,因为一切实际运行

My command window shows what happens if I run the .jar. 我的命令窗口显示如果我运行.jar会发生什么。 Nullpointer in line 30. I tried it with and without .getClassLoader(), it just wont find it. 第30行中的Nullpointer。我尝试使用和不使用.getClassLoader(),它只是找不到它。 Here is the inside of the jar: 这是jar的内部: again, app is where the class files are in. Hence, via class.getResource.. I should be able to search in DataPackeg. 再次,app是类文件所在的位置。因此,通过class.getResource ..我应该能够在DataPackeg中搜索。 Man, this is wearing me out. 伙计,这是我的意思。

A key concept to understand is that files don't exist inside of jars . 要理解的一个关键概念是jar文件中不存在文件 You must instead get your data as a read-only resource, and you will need to use a path that is relative to path of your class files. 您必须将数据作为只读资源获取,并且需要使用与类文件路径相关的路径。

If you're still stuck, you may need to tell us more specifics about your current program, its structure, what type of data you're trying to get, where it's located in the jar file, and how you're trying to use it. 如果你仍然卡住,你可能需要告诉我们更多关于你当前程序,它的结构,你想要获得的数据类型,它在jar文件中的位置以及你如何使用它的细节它。

For instance, say your package structure looked like this: 例如,假设您的包结构如下所示:

在此输入图像描述

So the class file is located in the codePackage package (this is Eclipse so the class files live in a universe parallel to the java files), and the resource's location is in the codePackage.images package, but relative to the class file it is the images directory, you could use the resource like so: 所以类文件位于codePackage包中(这是Eclipse所以类文件存在于与java文件并行的Universe中),并且资源的位置在codePackage.images包中,但相对于类文件,它是images目录,您可以像这样使用资源:

package codePackage;

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class ClassUsesResources {
   private JLabel label = new JLabel();

   public ClassUsesResources() {
      try {
         BufferedImage img = ImageIO.read(getClass().getResourceAsStream(
               "images/img001s.jpg"));
         ImageIcon icon = new ImageIcon(img);
         label.setIcon(icon);

         JOptionPane.showMessageDialog(null, label);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }
   }

   public static void main(String[] args) {
      new ClassUsesResources();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM