How to create/edit .class file codes

允我心安 提交于 2020-01-07 04:10:41

问题


I would like to know if there is a way to edit the codes in a class file? Because I dont seem to be able to compile a java file into a class file with the use of cmd as it will always detect errors . The Jar file that I am using already have its own existing class files and I would like to update one of the the .class file with a few lines of codes. But when I copied the codes from the class file and put it into a java file together with my added lines of codes, and then try compiling it using cmd, the cmd will generate error whenever I tried to compile it to generate the class file.

Does that mean that I will need to de-compile the whole jar file and make it into a java file and then recompile it into a class file then make it into a new jar file again,? In order to achieve what I am trying to do?

Right now I am using this JCIFS jar file and I would like to add in some lines of codes into one of the class file. I am refering to this "https://code.google.com/p/android-smb-streaming/" as a guide and I discovered that this person is able to customize / add a new class file with the same lines of codes from the existing class in the jar file together with his own added lines of codes.

May I know how do I achieve this? Thank you.


回答1:


What you have to do here is a common situation: you found a bug/feature request for an open source project.

Instead of hacking it you could try to contact the author of the library, submit your contribution and wait until it gets a new release so it has your new cool feature.

On the other hand, you can use the exposed API to customize it for your needs, e.g., create a specific subclass that overrides the required method containing that few lines of code and use the API in a way that it uses your implementation.

If you want to go the hacky way, there's no need to manipulate the classfiles directly (it is far more complicated): the easy way is to download the project, read the docs how to set it up (so you can compile it with cmd w/o errors), add your patch, compile it, then you can update the used JAR file with the new class file (or files: note that there might be multiple files generated if you're using inner classes or lambdas). Just don't forget to mention that somewhere in the docs, because otherwise no one will ever know that the library is slightly modified...



来源:https://stackoverflow.com/questions/26228911/how-to-create-edit-class-file-codes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!