Results 1 to 6 of 6

Thread: Open a file in a jar

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Open a file in a jar

    Hello everyone,
    I am currently developing an eclipse wizard, but my question is a little broader. So my sources are compiled into a wizard.jar, inside of which I have a resources folder, containing a foo.txt. I wish I could read a class file from the jar, but when I read ( "resources / foo.txt"), it does not work. I tried to look on the net an answer to this question, but not found one. Do you know how this can be done. Thank you in advance.

  2. #2
    Join Date
    Jul 2009
    Posts
    127

    Re: Open a file in a jar

    Hello,
    Even I am trying some thing like this. I tried to do this
    InputStream foo = getClass().getResource("resources / foo.txt").openStream();
    But this did not worked. I am really confused with this concept. So, if you guys can help me it would be great. I have even I have tried some other method and ideas but they did not worked for me.

  3. #3
    Join Date
    Dec 2009
    Posts
    292

    Re: Open a file in a jar

    Hello,
    Actually it does not work as I did. The null stream is obtained, probably because he did not find resources / foo.txt ... I'm not sure what path he should put, basically my source is in / src / foo.txt and in / resources /. But I tried to replace ".. / resources / foo.txt", and it does not work better. Any idea about it, how to solve this problem.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Open a file in a jar

    Hello,
    I think you should use the following line of code,
    InputStream foo = test.class.getResourceAsStream("example" + "foo.txt");
    Though I have not tried such example in my code career, but I think this should work for you. I am not saying that the only line will work for you. You have a add some logic to it and then with the logic you have to add this line and try to get the result. Just try it and see if it works.

  5. #5
    Join Date
    Dec 2009
    Posts
    292

    Re: Open a file in a jar

    Hello,
    Well I always have the same problem with my code, these are the methods I have tried till now
    InputStream t1 = WizardXMLPage.class.getClassLoader().getResourceAsStream("../../ resources / patchs.xsd ");
    And this logic
    InputStream t1 = WizardXMLPage.class.getClassLoader().getResourceAsStream("/ resources / patchs.xsd");
    And this too
    InputStream t1 = WizardXMLPage.class.getClassLoader().getResourceAsStream("resources /
    Other than these if you have some methods to use or some different logic then please let me know.

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Open a file in a jar

    Hello,
    InputStream is = getClass().getResourceAsStream("/ resources / foo.txt");
    should work if the file foo.txt is in the package resource if you have something like wizard / resources / foo.txt it is necessary that you leave from the wizard. Otherwise tried to put the same package as the class that operates to a simple
    InputStream is = getClass().getResourceAsStream("foo.txt");
    Try this and if you have any other problem then do post back with the error message , the error message helps us detect exactly what is the error in the code.

Similar Threads

  1. Replies: 5
    Last Post: 10-03-2012, 03:04 AM
  2. Office 2003 won't open via DC but will via File, open
    By krolyat in forum MS Office Support
    Replies: 1
    Last Post: 28-10-2011, 04:26 PM
  3. Replies: 6
    Last Post: 27-06-2011, 07:19 AM
  4. Replies: 4
    Last Post: 07-02-2011, 10:19 AM
  5. Replies: 4
    Last Post: 02-07-2010, 06:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,045,303.54149 seconds with 17 queries