Updating .class file in jar

后端 未结 11 836
南笙
南笙 2020-11-27 05:52

I want to update a .class file in a jar with a new one. What is the easiest way to do it, especially in the Eclipse IDE?

相关标签:
11条回答
  • 2020-11-27 06:22
    1. Use jar -xvf to extract the files to a directory.
    2. Make your changes and replace the classes.
    3. Use jar -cvf to create a new jar file.
    0 讨论(0)
  • 2020-11-27 06:23

    An alternative is not to replace the .class file in the jar file. Instead put it into a new jar file and ensure that it appears earlier on your classpath than the original jar file.

    Not sure I would recommend this for production software but for development it is quick and easy.

    0 讨论(0)
  • 2020-11-27 06:26

    Jar is an archive, you can replace a file in it by yourself in your favourite file manager (Total Commander for example).

    0 讨论(0)
  • 2020-11-27 06:27

    A JAR file is just a .zip in disguise. The zipped folder contains .class files.

    If you're on macOS:

    1. Rename the file to possess the '.zip' extension. e.g. myJar.jar -> myJar.zip.
    2. Decompress the '.zip' (double click on it). A new folder called 'myJar' will appear
    3. Find and replace the .class file with your new .class file.
    4. Select all the contents of the folder 'myJar' and choose 'Compress x items'. DO NOT ZIP THE FOLDER ITSELF, ONLY ITS CONTENTS

    Miscellaneous - Compiling a single .class file, with reference to a original jar, on macOS

    1. Make a file myClass.java, containing your code.
    2. Open terminal from Spotlight.
    3. javac -classpath originalJar.jar myClass.java This will create your compiled class called myClass.class.

    From here, follow the steps above. You can also use Eclipse to compile it, simply reference the original jar by right clicking on the project, 'Build Path' -> 'Add External Archives'. From here you should be able to compile it as a jar, and use the zip technique above to retrieve the class from the jar.

    0 讨论(0)
  • 2020-11-27 06:32

    High-level steps:

    Setup the environment
    Use JD-GUI to peek into the JAR file
    Unpack the JAR file
    Modify the .class file with a Java Bytecode Editor
    Update the modified classes into existing JAR file
    Verify it with JD-GUI
    

    Refer below link for detailed steps and methods to do it,

    https://www.talksinfo.com/how-to-edit-class-file-from-a-jar/

    0 讨论(0)
提交回复
热议问题