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.
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?
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.
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:
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:
It is not recommended to edit JAR files. From the perspective of reproducibility1, it is better to:
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:
jar -x ...
to extract the files to a temporary directory treejar -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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With