-2

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.

Ram Patro
  • 121
  • 4
  • 10
  • No it is not possible! – Jens Feb 09 '16 at 09:47
  • 3
    The process of converting a `.java` to a `.class` file is called compiling no matter how you do it and what tools you use. – SpaceTrucker Feb 09 '16 at 09:48
  • 1
    Yup. Write the byte code by hand - there's a [spec](https://docs.oracle.com/javase/specs/jvms/se8/html/) so you can get cracking. In all seriousness though - no, there is not. What did you expect? – Boris the Spider Feb 09 '16 at 09:49
  • 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 ???? Coverting to .class file . Please give your valuable input – Ram Patro Feb 09 '16 at 11:52

6 Answers6

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

Zubair Nabi
  • 1,016
  • 7
  • 28
0

.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.

A-Ace
  • 51
  • 2
  • 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 ???? Coverting to .class file .Please give your valuable input – Ram Patro Feb 09 '16 at 11:56
0

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.

António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
  • 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 ???? Coverting to .class file Please give your valuable input – Ram Patro Feb 09 '16 at 11:51
  • In order to correctly compile your `ATUReportsListener.java`, and generate the intended `.class`, you should also provide all the class dependencies and those dependencies' dependencies and so on. You only have access to the JAR, no source code? – António Ribeiro Feb 09 '16 at 15:06
  • Yes ,I've the JAR and i de-compiled and got the .java files of complete code. also i made my intended changes. now i have to compile the whole thing. Can you help me out ? – Ram Patro Feb 10 '16 at 04:57
0

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.

  • Add more details and references to your answer – NSNoob Feb 09 '16 at 10:00
  • Complete jar can be de-compiled as http://stackoverflow.com/questions/647116/how-to-decompile-a-whole-jar-file . Now do the changes in source code as you want and create the jar file again. – Laxman Singh Feb 09 '16 at 11:31
  • Hi Laxman Singh , But decompiler doesn't allow you to edit the code right ?? I can only view the code . and if i want to compile to get the .class again i've to add all the .java files from the JAR in to a new project. Is there any alternate option ??? – Ram Patro Feb 09 '16 at 11:38
  • I've extracted the all .java files in the JAR and then replaced the file. How can i compile all the .java files now ??? – Ram Patro Feb 09 '16 at 12:49
0

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.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Hi @Antimony , could you please give me some reference ??? so that i can follow and make my work done. Thanks in Advance! – Ram Patro Feb 09 '16 at 17:46
  • @ramnarayan Have you tried downloading BCV? https://github.com/Konloch/bytecode-viewer. As for learning bytecode, the best reference is the JVM specification. https://docs.oracle.com/javase/specs/jvms/se8/html/index.html – Antimony Feb 09 '16 at 18:21
0

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.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
Ram Patro
  • 121
  • 4
  • 10