简体   繁体   English

引用JAR中的文件

[英]Referencing files inside a JAR

I am creating a Java app for some serial port communication and within the app's folder I have two resources that need to be present at run-time. 我正在创建用于某些串行端口通信的Java应用程序,并且在该应用程序的文件夹中,有两个资源需要在运行时提供。 The first is an image that is being used as a splash screen and the other is a configuration file that needs to be read as the program starts. 第一个是用作启动屏幕的图像,另一个是在程序启动时需要读取的配置文件。

Here is an abridged version of the output from the "tree" command in Linux, the two files that I need to be referencing are "commandSet.config" and "splash-screen2.png" 这是Linux中“ tree”命令输出的简化版本,我需要引用的两个文件是“ commandSet.config”和“ splash-screen2.png”

.
├── bin
│   ├──...
├── commandSet.config
├── app-manifest.txt
├── splash-screen2.png
└── src
    ├── events
    │   └── InterfaceEvents.java
    ├── models
    │   ├── Command.java
    │   └── Phone.java
    ├── operations
    │   ├── Application.java
    │   ├── ...
    └── views
        ├── CallDialog.java
        ├── SplashScreen.java
        └── Window.java

I am currently referencing in the following lines: 我目前在以下几行中进行引用:

BufferedReader in = new BufferedReader(new FileReader("commandSet.config"));

and

JLabel image = new JLabel(new ImageIcon("splash-screen2.png"));

This works fine when I am running it from Eclipse, but as soon as I export to a runnable JAR the files are not moved and therefore aren't correctly referenced. 当我从Eclipse运行该文件时,此方法工作正常,但是,一旦导出到可运行的JAR,文件就不会移动,因此没有正确引用。 I have tried moving them into the "src" folder, which resulted in them being archived within the JAR, but I still couldn't reference them. 我曾尝试将它们移到“ src”文件夹中,这导致它们被保存在JAR中,但是我仍然无法引用它们。

I'm pretty new to the concept of exporting Java projects, so maybe I have missed something obvious. 我对导出Java项目的概念还很陌生,所以也许我错过了一些显而易见的事情。 If someone could show me the best way to do this and the best approach for future project file systems, I would be very grateful. 如果有人可以向我展示最佳方法以及将来项目文件系统的最佳方法,我将不胜感激。 Cheers! 干杯!

 InputStream in = this.getClass().getClassLoader().getResourceAsStream("splash-screen2.png");  
byte[] buffer = new byte[in.available()];
in.read(buffer);  
ImageIcon icon = new ImageIcon(buffer);

You can't access that file the same way you would if it were in the file system. 您无法以与文件系统中文件相同的方式访问该文件。

Use getResourceAsStream() from your context to read it from the CLASSPATH using the class loader. 在上下文中使用getResourceAsStream()可以使用类加载器从CLASSPATH中读取它。

Ok, I mananged to crack this and thought I'd drop an answer for others in the future. 好的,我设法破解了这个问题,并认为以后我会为其他人放弃答案。 I basically followed "duffymo's" guidelines, here's what I did: 我基本上遵循“ duffymo的”准则,这是我所做的:

  • Created a JAR with my resources in. 使用我的资源创建了一个JAR。
  • Put that JAR in the root of the project. 将那个JAR放在项目的根目录中。
  • Used Eclipse to add that JAR to the build path. 使用Eclipse将该JAR添加到构建路径。
  • Used getResourceAsStream(" /nameOfFile.ext ") to get the streams for my resources. 使用getResourceAsStream(“ /nameOfFile.ext ”)来获取我的资源的流。

Cheers! 干杯!

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

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