Changing the compiled java .class files in JAR

前提是你 提交于 2019-12-13 22:34:30

问题


My requirement: I've unpacked a jar and then changed a .class file after de-compiling in to .java file.
This .java file has few errors as it uses few stuffs like methods and variable from other places which is in the JAR.
Now I want to change the .java file in to .class file and then want to add in the JAR and see my changes are working or not.

Background:

  1. I've unpacked the ATU Jar file and the de-complied the ATUReportsListener.class.
  2. I've the ATUReportsListener.java file now and I made some changes to it.
  3. Now I want to covert to .class executable file again and the pack in to the JAR. But as the ATUReportsListener.java has few stuffs which are dependent on other files of the original ATU JAR.I'm not able to compile it.

How would i do that?
Converting to .class file . Please give your valuable input.


回答1:


.class file is only created after compiling. decompiling and making the changes will not make it with same byte code. you need to sort those errors and compile it again




回答2:


.class generates the byte code for (successfully compiled code i.e "can be run on JVM"). Therefore you must allow the compiler to try and build your code to know the operation will be successful and error free.

Source: https://en.wikipedia.org/wiki/Java_class_file

good luck.




回答3:


As stated before, you simply can't.

A Java class file is a file (with the .class filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is produced by a Java compiler from Java programming language source files (.java files) containing Java classes. If a source file has more than one class, each class is compiled into a separate class file.

Therefore, without compiling your source code you cannot attain a .class file.




回答4:


Nope, You can not convert a java file to class file directly. Solution of your problem is first decompile whole jar file classes and then do changes in source files. Compile it and make jar file.




回答5:


You are asking the wrong question. The right question is "how do I modify code in a compiled Jar".

As you've discovered, decompilation won't help because it is usually impossible to re-compile the results. Instead, you will need to modify the code at the bytecode level. This has the advantage that it will always work, regardless of what the code is like or whether it is obfuscated. The downside of course is that you have to understand Java bytecode, but it's not that hard to learn.

There are several tools you can use to edit bytecode. For example, the Krakatau diassembler and assembler. Konloch's Bytecode Viewer offers a GUI.




回答6:


Here is the solution. Of course we've to compile the .java file to get the .class file.

For the solution of working with JAR :

  1. UnWrap the JAR using de-compiler (http://jd.benow.ca/)
  2. Get the Source of the decompiled files (You'll get the pack(all .java files))
  3. Now Make changes where you wish
  4. Create a JAVA project in the Eclipse and import everything.
  5. Now just Save it
  6. You can see the .class files in the bin folder.
  7. Add the .class file in the original executable .class file sets.
  8. Done, Just Wrap it up in the JAR again.(you can use eclipse and export as a JAR)

Thanks for everybodies response.



来源:https://stackoverflow.com/questions/35288632/changing-the-compiled-java-class-files-in-jar

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