Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 392
tony stew Avatar asked Nov 19 '16 23:11

tony stew


People also ask

How do I open a JAR file in IntelliJ?

Can you open a jar file in IntelliJ? From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and click Artifacts. , point to JAR, and select From modules with dependencies. and select the main class in the dialog that opens (for example, HelloWorld (com. example. How do I decompile a jar file and edit it?

Why is my project read-only in IntelliJ?

Just a cent: For one reason or another, when opening a project something bad happened inside of IntelliJ (with an error pop-up) and it marked the project as read-only. After removing the .idea-folder and reopening the project all was fine. To toggle read-only attribute of a file, open file in the editor, or select it in the Project tool window.

What is location-independent access to resources in IntelliJ IDEA?

For more information about these methods, refer to Location-Independent Access to Resources. When building an application, IntelliJ IDEA copies all resources into the output directory, preserving the directory structure of the resources relative to the source path. The following file types are recognized as resources by default:

How to change file permissions in IntelliJ IDEA?

If IntelliJ IDEA is unable to change the file permissions using File | File Properties | Make File Writable, then perhaps another user is owning that file, e.g. root (use this command to verify: ls -hal /path/to/File.java) If someone else owns it, for whatever reason, you can change the permissions yourself using the chown command in the terminal:


1 Answers

It is not recommended to edit JAR files. From the perspective of reproducibility1, 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 from the command line to modify a JAR file:

  1. Use jar -x ... to extract the files to a temporary directory tree
  2. Apply what ever changes need to be made to the tree
  3. Use jar -c .... to create a new JAR from the tree.

Read the manual entry for the jar command for more details. 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.


1 - The point is that the next guy maintaining your code needs to know what you did to the library JAR that you "edited", in case he needs to do the same procedure with another version of the JAR. If you do it by hand, he has no choice but to do a forensic comparison of the differences between the original and your edited version. And that assumes that the original JAR can still be obtained. Note that "the next guy" could be you ... in a couple of months or years time, when you have forgotten exactly what you did.

like image 193
Stephen C Avatar answered Sep 21 '22 00:09

Stephen C