i've used ResorceLoder class to get input stream but how can i get outputStream, is this possible to modify data which is in that package?(i need to change the image files which are present in that package using java class which is present in the same jar file)
-
1I think your question is very abstract. Can you make it clearer ? Maybe a sample code fragment would give more context to your question ? – thotheolh Sep 10 '13 at 05:49
-
if it's really necessary to modify your own jar you can try [http://stackoverflow.com/questions/2463785/modify-executing-jar-file] – Frank M. Sep 10 '13 at 06:07
3 Answers
You cannot directly change the resources of your jar-file at runtime. The ClassLoader (which is giving you those InputStreams does not provide an OutputStream to rewrite the files within a jar-file).
If you want to manipulate images or resources like that at runtime, don't put them in your jar file, but retrieve them from file system.
-
thank u for your reply, do u have any idea how can we give absalute path for synth xml(to change look and feel) file? – user2763652 Sep 10 '13 at 06:23
-
@user2763652 Have a look here, there is also an example on how to set the L&F http://en.wikipedia.org/wiki/Synth_Look_and_Feel – Matthias Sep 10 '13 at 06:25
Changing classpath resources is either difficult or impossible ... depending on the classloader and the classpath.
If the classpath resource is in filesystem directory, you should be able to replace it. However, it is possible that the file is locked, and there is no guarantee that the updated resource will be visible until you restart the JVM.
If the classpath resource is in a JAR or ZIP in the local filesystem, you have a couple of additional problems:
You cannot do an in-place update of a JAR or ZIP file. You would have to create a new ZIP / JAR and copy the existing content to it.
A ZIP or JAR file on the classpath will be locked.
If the classpath resource is in a ZIP or JAR that was downloaded, it won't be possible (in general) to update the file at source ... so your changes may not "stick".
If you are using a custom classloader, all bets are off ...
In short, it is better to find a different way to do this; e.g. copy / install the resources into a writeable directory in the filesystem and access them from there.

- 698,415
- 94
- 811
- 1,216
Ok.. If I understand your question correctly, you wish to change the resource in the class path through a program file located at the same location. I think, it should not be an issue , if you understand the below limitation:
Classloader will never give you a handle of a resource (located inside a jar) as a file with directory path and all. It is because a Jar is usually not exploded. You need to have your jar in exploded form in order to get the resource as file.
When you are running your program, your current working directory may not be the location of a resource/jar in classpath.
You need to figure out the directory of the resource and then you could easily modify or do what you wish.
One way to do that is to declare your resource path somewhere in a configurable file. this is a very simple approach.
Other way is to figure out the path using a logic. One such example is taken from this link
try { //Attempt to get the path of the actual JAR file, because the working directory is frequently not where the file is. //Example: file:/D:/all/Java/TitanWaterworks/TitanWaterworks-en.jar!/TitanWaterworks.class //Another example: /D:/all/Java/TitanWaterworks/TitanWaterworks.class PROGRAM_DIRECTORY = getClass().getClassLoader().getResource("TitanWaterworks.class").getPath(); // Gets the path of the class or jar. //Find the last ! and cut it off at that location. If this isn't being run from a jar, there is no !, so it'll cause an exception, which is fine. try { PROGRAM_DIRECTORY = PROGRAM_DIRECTORY.substring(0, PROGRAM_DIRECTORY.lastIndexOf('!')); } catch (Exception e) { } //Find the last / and cut it off at that location. PROGRAM_DIRECTORY = PROGRAM_DIRECTORY.substring(0, PROGRAM_DIRECTORY.lastIndexOf('/') + 1); //If it starts with /, cut it off. if (PROGRAM_DIRECTORY.startsWith("/")) PROGRAM_DIRECTORY = PROGRAM_DIRECTORY.substring(1, PROGRAM_DIRECTORY.length()); //If it starts with file:/, cut that off, too. if (PROGRAM_DIRECTORY.startsWith("file:/")) PROGRAM_DIRECTORY = PROGRAM_DIRECTORY.substring(6, PROGRAM_DIRECTORY.length()); } catch (Exception e) { PROGRAM_DIRECTORY = ""; //Current working directory instead. }

- 1
- 1

- 5,511
- 2
- 27
- 53