如何将资源添加到jar文件中 [英] How to add resources into jar file

查看:124
本文介绍了如何将资源添加到jar文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的jar中添加一个exel文件,因此它是可移植的。我知道答案是使用getClass()。getResource ....但我不知道如何使用这段代码。

I need to add an exel file into my jar so it's portable. I know the answer is using getClass().getResource.... but i don't have a clue on how to go about using this code.

我有使用我的类文件在src文件夹中的exel文件,它在netbeans中工作,但是当我在另一台PC上打开jar文件时它不工作。我得到filenotfound异常

I have the exel file in the src folder with my class files, it works in netbeans but not when i open the jar file on another PC. i get filenotfound exception

这里是我使用的代码:

public class WindageLogic {

  //....
  public void GetValues() throws Exception
    {


    File exel=new File("src/Calculator/table1.xls");

     Workbook w1; //reads from file

     //reads from file
     w1 = Workbook.getWorkbook(exel);






有人可以给我代码来实现哪个会允许我从jar应用程序访问这个文件,我花了整个周末在互联网上查找东西,但我不明白我如何使用这些getresource技术。


Can someone give me the code to implement which will allow me to access this file from the jar application, I've spent all weekend looking up stuff on the internet but i don't understand how i can use these getresource techniques.

非常感谢

推荐答案

如果您的Excel文件是资源,那么您不能使用文件操纵它而不是资源,我相信它是只读的 - 你确定这是你想做的吗?您可以改为加载一个命令行String,告诉程序从哪里开始查找文件,或创建一个属性文件,告诉他们首先要查看的位置。

If your excel file is a resource, then you can't use File to manipulate it but rather resources, and I believe that it is read-only -- are you sure that this is what you want to do? You could instead load a command line String that tells the program where to start looking for the file, or create a property file that tells where to first look.

编辑1

如果您使用的是JExcel,那么您可以调用 Workbook.getWorkbook(java.io.InputStream is)并获取您的资源通过类的 getResourceAsStream(...)方法作为流。

Edit 1
If you are using JExcel then you can call Workbook.getWorkbook(java.io.InputStream is) and get your resource as a Stream via Class's getResourceAsStream(...) method.

例如,

  public void GetValues() throws Exception   {
     Workbook w1; //reads from file
     w1 = Workbook.getWorkbook(WindageLogic.class.
          getResourceAsStream("/Calculator/table1.xls") );
     //...

这篇关于如何将资源添加到jar文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆