How to comment lines of a .class file?












0















I want to comment out a few lines of a .class file. I do not have the source file, but when I decompiled that file, I found that it is trying to import non-existing classes and this leads to errors and exception. So, I wanted to comment out those troublesome lines to remove the cause of the errors. As far as I know comments are just removed in the .class file. How can I remove a command line in the .class file?










share|improve this question



























    0















    I want to comment out a few lines of a .class file. I do not have the source file, but when I decompiled that file, I found that it is trying to import non-existing classes and this leads to errors and exception. So, I wanted to comment out those troublesome lines to remove the cause of the errors. As far as I know comments are just removed in the .class file. How can I remove a command line in the .class file?










    share|improve this question

























      0












      0








      0








      I want to comment out a few lines of a .class file. I do not have the source file, but when I decompiled that file, I found that it is trying to import non-existing classes and this leads to errors and exception. So, I wanted to comment out those troublesome lines to remove the cause of the errors. As far as I know comments are just removed in the .class file. How can I remove a command line in the .class file?










      share|improve this question














      I want to comment out a few lines of a .class file. I do not have the source file, but when I decompiled that file, I found that it is trying to import non-existing classes and this leads to errors and exception. So, I wanted to comment out those troublesome lines to remove the cause of the errors. As far as I know comments are just removed in the .class file. How can I remove a command line in the .class file?







      comments .class-file






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 13:19









      Donia ZaelaDonia Zaela

      4318




      4318
























          1 Answer
          1






          active

          oldest

          votes


















          1














          A classfile is a binary file format. There is no notion of comments at the classfile level. For that matter, there is no notion of imports either. You can just reference any class you want and the JVM will load them as needed.



          It sounds like what you are actually trying to do is to edit a classfile for which you do not have the source, and you are failing because the decompiled code results in compiler errors. Apart from the issue of not having the source for the dependencies, this is not unexpected because compilation and decompilation are both lossy processes. You can't expect to be able to roundtrip classes through a decompiler in general.



          One option is to edit the classfile directly at the bytecode level, using an assembler/disassembler pair such as Krakatau. Krakatau can roundtrip any valid classfile to a human readable assembly format and back. This lets you edit classfiles without having to worry about compilation errors or missing dependencies, or any other similar issues. The downside is that it requires you to understand Java bytecode and the classfile format.






          share|improve this answer
























          • Thank @Antimony for the answer. It explained why I couldn't identify any import in the classfile when opening it just with an editor. As far as I can tell it started with the code right away. You explained it now. So, i am willing to go down that path as the .class file I want to edit to comment a few lines of code is relatively small, I want to comment only 5 lines of code. But, how can I get to understand Java bytecode and the classfile format. Do you have a good reference to it?

            – Donia Zaela
            Nov 22 '18 at 19:06











          • Also, I tried to open Krakatau and it only has a UNIX executable. Is there anything I can use on Mac or Windows. Thanks for your patience, I am a newbie with all of that.

            – Donia Zaela
            Nov 22 '18 at 19:08











          • @Donia It's a Python script. It works on any platform.

            – Antimony
            Nov 22 '18 at 19:34






          • 1





            @DoniaZaela the primary literature is The Java® Virtual Machine Specification. Chapter 4 describes the class file format, chapter 6 the instruction set, though it’s worth skimming through the other chapters as well. The internet search engine of your choice may find easier-to-read introductions or tutorials, but since those are sometimes a bit sloppy, it’s always recommended to keep in mind that there’s the JVM specification which is the authoritative source to consult when in doubt.

            – Holger
            Nov 27 '18 at 9:16











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431906%2fhow-to-comment-lines-of-a-class-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          A classfile is a binary file format. There is no notion of comments at the classfile level. For that matter, there is no notion of imports either. You can just reference any class you want and the JVM will load them as needed.



          It sounds like what you are actually trying to do is to edit a classfile for which you do not have the source, and you are failing because the decompiled code results in compiler errors. Apart from the issue of not having the source for the dependencies, this is not unexpected because compilation and decompilation are both lossy processes. You can't expect to be able to roundtrip classes through a decompiler in general.



          One option is to edit the classfile directly at the bytecode level, using an assembler/disassembler pair such as Krakatau. Krakatau can roundtrip any valid classfile to a human readable assembly format and back. This lets you edit classfiles without having to worry about compilation errors or missing dependencies, or any other similar issues. The downside is that it requires you to understand Java bytecode and the classfile format.






          share|improve this answer
























          • Thank @Antimony for the answer. It explained why I couldn't identify any import in the classfile when opening it just with an editor. As far as I can tell it started with the code right away. You explained it now. So, i am willing to go down that path as the .class file I want to edit to comment a few lines of code is relatively small, I want to comment only 5 lines of code. But, how can I get to understand Java bytecode and the classfile format. Do you have a good reference to it?

            – Donia Zaela
            Nov 22 '18 at 19:06











          • Also, I tried to open Krakatau and it only has a UNIX executable. Is there anything I can use on Mac or Windows. Thanks for your patience, I am a newbie with all of that.

            – Donia Zaela
            Nov 22 '18 at 19:08











          • @Donia It's a Python script. It works on any platform.

            – Antimony
            Nov 22 '18 at 19:34






          • 1





            @DoniaZaela the primary literature is The Java® Virtual Machine Specification. Chapter 4 describes the class file format, chapter 6 the instruction set, though it’s worth skimming through the other chapters as well. The internet search engine of your choice may find easier-to-read introductions or tutorials, but since those are sometimes a bit sloppy, it’s always recommended to keep in mind that there’s the JVM specification which is the authoritative source to consult when in doubt.

            – Holger
            Nov 27 '18 at 9:16
















          1














          A classfile is a binary file format. There is no notion of comments at the classfile level. For that matter, there is no notion of imports either. You can just reference any class you want and the JVM will load them as needed.



          It sounds like what you are actually trying to do is to edit a classfile for which you do not have the source, and you are failing because the decompiled code results in compiler errors. Apart from the issue of not having the source for the dependencies, this is not unexpected because compilation and decompilation are both lossy processes. You can't expect to be able to roundtrip classes through a decompiler in general.



          One option is to edit the classfile directly at the bytecode level, using an assembler/disassembler pair such as Krakatau. Krakatau can roundtrip any valid classfile to a human readable assembly format and back. This lets you edit classfiles without having to worry about compilation errors or missing dependencies, or any other similar issues. The downside is that it requires you to understand Java bytecode and the classfile format.






          share|improve this answer
























          • Thank @Antimony for the answer. It explained why I couldn't identify any import in the classfile when opening it just with an editor. As far as I can tell it started with the code right away. You explained it now. So, i am willing to go down that path as the .class file I want to edit to comment a few lines of code is relatively small, I want to comment only 5 lines of code. But, how can I get to understand Java bytecode and the classfile format. Do you have a good reference to it?

            – Donia Zaela
            Nov 22 '18 at 19:06











          • Also, I tried to open Krakatau and it only has a UNIX executable. Is there anything I can use on Mac or Windows. Thanks for your patience, I am a newbie with all of that.

            – Donia Zaela
            Nov 22 '18 at 19:08











          • @Donia It's a Python script. It works on any platform.

            – Antimony
            Nov 22 '18 at 19:34






          • 1





            @DoniaZaela the primary literature is The Java® Virtual Machine Specification. Chapter 4 describes the class file format, chapter 6 the instruction set, though it’s worth skimming through the other chapters as well. The internet search engine of your choice may find easier-to-read introductions or tutorials, but since those are sometimes a bit sloppy, it’s always recommended to keep in mind that there’s the JVM specification which is the authoritative source to consult when in doubt.

            – Holger
            Nov 27 '18 at 9:16














          1












          1








          1







          A classfile is a binary file format. There is no notion of comments at the classfile level. For that matter, there is no notion of imports either. You can just reference any class you want and the JVM will load them as needed.



          It sounds like what you are actually trying to do is to edit a classfile for which you do not have the source, and you are failing because the decompiled code results in compiler errors. Apart from the issue of not having the source for the dependencies, this is not unexpected because compilation and decompilation are both lossy processes. You can't expect to be able to roundtrip classes through a decompiler in general.



          One option is to edit the classfile directly at the bytecode level, using an assembler/disassembler pair such as Krakatau. Krakatau can roundtrip any valid classfile to a human readable assembly format and back. This lets you edit classfiles without having to worry about compilation errors or missing dependencies, or any other similar issues. The downside is that it requires you to understand Java bytecode and the classfile format.






          share|improve this answer













          A classfile is a binary file format. There is no notion of comments at the classfile level. For that matter, there is no notion of imports either. You can just reference any class you want and the JVM will load them as needed.



          It sounds like what you are actually trying to do is to edit a classfile for which you do not have the source, and you are failing because the decompiled code results in compiler errors. Apart from the issue of not having the source for the dependencies, this is not unexpected because compilation and decompilation are both lossy processes. You can't expect to be able to roundtrip classes through a decompiler in general.



          One option is to edit the classfile directly at the bytecode level, using an assembler/disassembler pair such as Krakatau. Krakatau can roundtrip any valid classfile to a human readable assembly format and back. This lets you edit classfiles without having to worry about compilation errors or missing dependencies, or any other similar issues. The downside is that it requires you to understand Java bytecode and the classfile format.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 15:40









          AntimonyAntimony

          26.9k66780




          26.9k66780













          • Thank @Antimony for the answer. It explained why I couldn't identify any import in the classfile when opening it just with an editor. As far as I can tell it started with the code right away. You explained it now. So, i am willing to go down that path as the .class file I want to edit to comment a few lines of code is relatively small, I want to comment only 5 lines of code. But, how can I get to understand Java bytecode and the classfile format. Do you have a good reference to it?

            – Donia Zaela
            Nov 22 '18 at 19:06











          • Also, I tried to open Krakatau and it only has a UNIX executable. Is there anything I can use on Mac or Windows. Thanks for your patience, I am a newbie with all of that.

            – Donia Zaela
            Nov 22 '18 at 19:08











          • @Donia It's a Python script. It works on any platform.

            – Antimony
            Nov 22 '18 at 19:34






          • 1





            @DoniaZaela the primary literature is The Java® Virtual Machine Specification. Chapter 4 describes the class file format, chapter 6 the instruction set, though it’s worth skimming through the other chapters as well. The internet search engine of your choice may find easier-to-read introductions or tutorials, but since those are sometimes a bit sloppy, it’s always recommended to keep in mind that there’s the JVM specification which is the authoritative source to consult when in doubt.

            – Holger
            Nov 27 '18 at 9:16



















          • Thank @Antimony for the answer. It explained why I couldn't identify any import in the classfile when opening it just with an editor. As far as I can tell it started with the code right away. You explained it now. So, i am willing to go down that path as the .class file I want to edit to comment a few lines of code is relatively small, I want to comment only 5 lines of code. But, how can I get to understand Java bytecode and the classfile format. Do you have a good reference to it?

            – Donia Zaela
            Nov 22 '18 at 19:06











          • Also, I tried to open Krakatau and it only has a UNIX executable. Is there anything I can use on Mac or Windows. Thanks for your patience, I am a newbie with all of that.

            – Donia Zaela
            Nov 22 '18 at 19:08











          • @Donia It's a Python script. It works on any platform.

            – Antimony
            Nov 22 '18 at 19:34






          • 1





            @DoniaZaela the primary literature is The Java® Virtual Machine Specification. Chapter 4 describes the class file format, chapter 6 the instruction set, though it’s worth skimming through the other chapters as well. The internet search engine of your choice may find easier-to-read introductions or tutorials, but since those are sometimes a bit sloppy, it’s always recommended to keep in mind that there’s the JVM specification which is the authoritative source to consult when in doubt.

            – Holger
            Nov 27 '18 at 9:16

















          Thank @Antimony for the answer. It explained why I couldn't identify any import in the classfile when opening it just with an editor. As far as I can tell it started with the code right away. You explained it now. So, i am willing to go down that path as the .class file I want to edit to comment a few lines of code is relatively small, I want to comment only 5 lines of code. But, how can I get to understand Java bytecode and the classfile format. Do you have a good reference to it?

          – Donia Zaela
          Nov 22 '18 at 19:06





          Thank @Antimony for the answer. It explained why I couldn't identify any import in the classfile when opening it just with an editor. As far as I can tell it started with the code right away. You explained it now. So, i am willing to go down that path as the .class file I want to edit to comment a few lines of code is relatively small, I want to comment only 5 lines of code. But, how can I get to understand Java bytecode and the classfile format. Do you have a good reference to it?

          – Donia Zaela
          Nov 22 '18 at 19:06













          Also, I tried to open Krakatau and it only has a UNIX executable. Is there anything I can use on Mac or Windows. Thanks for your patience, I am a newbie with all of that.

          – Donia Zaela
          Nov 22 '18 at 19:08





          Also, I tried to open Krakatau and it only has a UNIX executable. Is there anything I can use on Mac or Windows. Thanks for your patience, I am a newbie with all of that.

          – Donia Zaela
          Nov 22 '18 at 19:08













          @Donia It's a Python script. It works on any platform.

          – Antimony
          Nov 22 '18 at 19:34





          @Donia It's a Python script. It works on any platform.

          – Antimony
          Nov 22 '18 at 19:34




          1




          1





          @DoniaZaela the primary literature is The Java® Virtual Machine Specification. Chapter 4 describes the class file format, chapter 6 the instruction set, though it’s worth skimming through the other chapters as well. The internet search engine of your choice may find easier-to-read introductions or tutorials, but since those are sometimes a bit sloppy, it’s always recommended to keep in mind that there’s the JVM specification which is the authoritative source to consult when in doubt.

          – Holger
          Nov 27 '18 at 9:16





          @DoniaZaela the primary literature is The Java® Virtual Machine Specification. Chapter 4 describes the class file format, chapter 6 the instruction set, though it’s worth skimming through the other chapters as well. The internet search engine of your choice may find easier-to-read introductions or tutorials, but since those are sometimes a bit sloppy, it’s always recommended to keep in mind that there’s the JVM specification which is the authoritative source to consult when in doubt.

          – Holger
          Nov 27 '18 at 9:16


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431906%2fhow-to-comment-lines-of-a-class-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin