Archived

This topic is now archived and is closed to further replies.

StrategicAlliance

[java] Copy file from JAR

Recommended Posts

Hey, This has got me stumped for quite a few days now : I have a JAR which contains my application. In this JAR I have included a .JPG which should be copied to a directory the user specifies (when running the application). Now, applets have getCodeBase() to obtain the location from which they are running but applications have not. So provided that I don''t know at what location in the filesystem my JAR is running, how can I obtain the path of the .JPG which is in my JAR? Simply
 File f = new File("x.jpg")  
doesn''t work. It''s probably some kind of method I keep looking over, but I''d appreciate it if someone could show me. Thanks! ****************************** StrategicAlliance On the day we create intelligence and consciousness, mankind becomes God. On the day we create intelligence and consciousness, mankind becomes obsolete... ******************************

Share this post


Link to post
Share on other sites
Your code looks for the image in the current directory, but it can only be found by following the classpath since it is located inside a JAR. I think the getResourceAsStream() or getSystemResourceAsStream() methods in java.lang.ClassLoader will let you read the raw bytes of the image in order to copy it.

Edited by - HenryApe on February 22, 2002 4:20:48 AM

Share this post


Link to post
Share on other sites
Example:

String name1 = "com/operecitato/gkw/utils/image/fighter.tga";
URL urlfighter = null;
urlfighter = PrimReader.class.getClassLoader().getResource(name1);


With URL you can do just about anything, like getting the full path name for a file object.

E-
Forgot to mention that PrimReader is another class in the same jar as fighter.tga. You should find a class in the same jar and use that class in place of PrimReader. I don't know if that is actually necessary but you can experiment.
-E

The fanatic is incorruptible: if he kills for an idea, he can just as well get himself killed for one; in either case, tyrant or martyr, he is a monster.
--EM Cioran

Opere Citato

Edited by - GKW on February 22, 2002 2:40:47 PM

Share this post


Link to post
Share on other sites