wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Copy files within JAR to disk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Copy files within JAR to disk" Watch "Copy files within JAR to disk" New topic
Author

Copy files within JAR to disk

Jos Vermeulen
Greenhorn

Joined: Aug 31, 2010
Posts: 1
Hi. I'm a novice trying to learn some Java. Currently working on a small application. A feature in my application is that it holds some files (resources) and copies it to the hard disk.

Running it from Eclipse, it works 100% fine (!!). Unfortunately, when I export my app to a JAR and run it, files from within my application that need to be copied to the hard disk, aren't getting copied...

Here's some code that handles the copying from jar to hard disk:



I'm pretty sure the problem lies in the this.getClass().getResource() part. But not sure on what to fix.

There's probably more efficient code for copying files, but I only need a 'quick fix' for now. Can someone please help me?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Don't know. Why are you not using simpler classes? For text files you can use Scanner and Formatter.

That looks too difficult for "beginning Java", so I shall move the discussion.

And welcome to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

You can't use java.io.File, java.io.FileInputStream, java.io.FileOutputStream, java.io.FileReader, java.io.FileWriter and java.nio.channels.FileChannel with files inside JAR files. That's because technically speaking they are no longer files - they are JAR file entries. You should switch to Class.getResourceAsStream() instead, then use old fashioned stream-based copying.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy files within JAR to disk