tony stew tony stew - 1 year ago 156
Java Question

My jar resource files in intellij are read only and I need to edit them

I have tried unsuccessfully for a few hours now to edit the java files in a jar I am using as a library. I have marked the resource as a content root and as a source root but I am still unable to edit the code in the jars. The project compiles and runs correctly but I need to make an adjustment to a resource file and cannot; I have tried every project structure I could think of. Is it just impossible? All help is appreciated.

Answer Source

It is not recommended to edit JAR files. From the prespect of build reproducibility, it is better to:

  1. Get hold of the source tree for the library
  2. Check it into your version control (or fork it on Github)
  3. Modify and build it
  4. Use the resulting JAR instead of the original JAR

Another approach is to "overlay" the changes you want to make by creating a another JAR with the alternative version of the resources and placing it earlier in the application classpath.

But if neither of those works for you, you can use the jar command to modify a JAR file:

  1. Use jar -x ... to extract the files to a temporary directory tree
  2. Modify the tree
  3. Use jar -c .... to create a new JAR.

Obviously, signing the new JAR with the original keys would be an issue if you are not the original signer, but I doubt that that is relevant to you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download