• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]


 
Rob Spoor
Sheriff
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


I tend to do that a lot. Just ask Campbell
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22806
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79716
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
He does not suffer fools gladly. But this tiny ad does:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic