wood burning stoves 2.0*
The moose likes Java in General and the fly likes a file in your Jar, How do I access it in code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "a file in your Jar, How do I access it in code?" Watch "a file in your Jar, How do I access it in code?" New topic
Author

a file in your Jar, How do I access it in code?

Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
I am running a Jar and the very first thing it needs to do is import (if I may use that term?) this script file as a 'File' in code. The problem is the script file is located INSIDE the Jar. Thus, a direct path (absolute) will not work. I think is has something to do with:



The problem is, it returns a URL. I am missing the connect. How do I use the URL to import the file into a File variable in the code from my Jar?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
i think it should look like this..

if your jar has the following structure
/folder1/file.txt

then you should be able to access it like this:


JDBCSupport - An easy to use, light-weight JDBC framework -
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
I need the file that is within the Jar to be brought in as a 'File' and not a string. Like this:



Wait... your string is named 's' right? Could I do this?

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
try this... i am not sure whether or not this is working

File f = new File(this.getClas().getResourceAsStream().getFile());

getFile() returns the fully qualified path + file as a string
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

A resouce is not necessarily a file. It only is a file if it's not in a JAR / ZIP file.

If you really need it to be a file, the only safe way is create a temporary file, and copy the resource's contents to it. Check out File.createTempFile.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Ok, I have this code below. I believe it is of sound reasoning but it does not work. Basically, I still do not have access to the file "test_method.rb" inside the jar. Would someone read the code below and point out my error in thinking? Thank you in advance.



Brightmatter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

http://faq.javaranch.com/java/AvailableDoesntDoWhatYouThinkItDoes

In short, change the copying code:
Note that I have used bw.newLine() - br.readLine() strips off the line separators (line breaks) so you'll need to readd them.

Also, the call to getCanonicalPath is not really necessary. It will still point to the same folder, which is the current one. Also, the following is a better way of constructing a File in a specific folder:
Also, move this statement to before you create your BufferedWriter, then use it there as well:

Finally, keep in mind that BufferedReader and BufferedWriter are for text files. If test_method.rb is a binary file, use BufferedInputStream and BufferedOutputStream instead.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There are several problems, all fixable. First, you're not closing the BufferedWriter, so likely it contains incomplete output (or no output.) Close it after writing to it.

Second, you're reading by lines, and then writing the lines into the new file without newlines between them. That will affect your Ruby syntax!

Third, this is a lot of work:

String tmpDir = new java.io.File(".").getCanonicalPath();
bw = new BufferedWriter(new FileWriter(tmpDir + "\\test_method.rb"));

when this does exactly the same thing:

bw = new BufferedWriter(new FileWriter("test_method.rb"));

Fourth, you might consider not using Readers and Writers, but rather Streams. It would save you dealing with the newline problem. Just take the inputstream from the URL, and open a FileOutputStream, allocate a byte[] for a buffer, and loop:



[ Edit: Rob beat me to it ]



[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Ernest Friedman-Hill wrote:[ Edit: Rob beat me to it ]

I tend to do that a lot. Just ask Campbell
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
OH!! It works! It really works! I even used a temporary file as suggested above. I don't know why but it is not very temporary (i.e. it does not delete on exit as advertised).

I can't believe how much help I get from this one forum and none of the others. It is really night and day difference.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

jake benn wrote:I don't know why but it is not very temporary (i.e. it does not delete on exit as advertised).

Usually a file deletion does not work because the file is still in use. It's not caused bw because you close that. Well, unless you get an IOException during copying. That's why a try-finally block is usually better:
Now I see that you're creating something called Stream_Listeners around the temporary file. Is that perhaps keeping a stream, reader or writer open?
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Try - Finally... I have never heard of that... I feel like I have learned 15 new things today. Awesome! Thanks guys... or maybe gals?
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Rob Prime wrote:
Now I see that you're creating something called Stream_Listeners around the temporary file. Is that perhaps keeping a stream, reader or writer open?


You are exactly right. Right on the nose my friend. That is just plain spooky that you knew that. I am going to step away now before you get my credit card numbers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Too late. You won't mind that new laptop I bought with it, do you?

But seriously, it was just logical. file is a local variable. That means that any streams opened using it must be opened by this method, or a method / constructor called by this method. Since you close all streams opened in this method, and the JTextPane.setText methods don't open streams, there was just one possibility.

jake benn wrote:Try - Finally... I have never heard of that...

Try reading this for a second.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39868
    
  28
Rob Prime wrote:
Ernest Friedman-Hill wrote:[ Edit: Rob beat me to it ]

I tend to do that a lot. Just ask Campbell
Yes, it's true.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a file in your Jar, How do I access it in code?