问题
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:
- I've unpacked the ATU Jar file and the de-complied the
ATUReportsListener.class
. - I've the
ATUReportsListener.java
file now and I made some changes to it. - Now I want to covert to
.class
executable file again and the pack in to the JAR. But as theATUReportsListener.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 :
- UnWrap the JAR using de-compiler (http://jd.benow.ca/)
- Get the Source of the decompiled files (You'll get the pack(all .java files))
- Now Make changes where you wish
- Create a JAVA project in the Eclipse and import everything.
- Now just Save it
- You can see the .class files in the bin folder.
- Add the .class file in the original executable .class file sets.
- 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