0

Below is the hexdump of the class file in question. I have no idea about why this code doesn't run and it gives a java.lang.ClassFormatError: Code segment has wrong length in class file fibo when running in terminal using java fibo. Does anybody with a expertise in this understand why the code length is incorrect?

  

     //source code as requested
            public class fibo{

            public static void main(String args[]){
                System.out.println(fibonacci(10));
            }

         public static int fibonacci(int n)  {
                if(n == 0)
                    return 0;
                else if(n == 1)
                  return 1;
               else
                  return fibonacci(n - 1) + fibonacci(n - 2);
            }
    
ca fe ba be 00 00 00 34  00 1f 0c 00 19 00 1e 01 
00 16 28 5b 4c 6a 61 76  61 2f 6c 61 6e 67 2f 53  
74 72 69 6e 67 3b 29 56  01 00 10 6a 61 76 61 2f 
6c 61 6e 67 2f 4f 62 6a  65 63 74 01 00 06 3c 69  
6e 69 74 3e 07 00 03 0c  00 04 00 0a 07 00 13 0a  
00 07 00 1c 01 00 09 66  69 62 6f 2e 6a 61 76 61  
01 00 03 28 29 56 07 00  16 01 00 04 43 6f 64 65  
01 00 04 66 69 62 6f 01  00 04 6d 61 69 6e 01 00  
0d 53 74 61 63 6b 4d 61  70 54 61 62 6c 65 09 00 
0b 00 01 01 00 0a 53 6f  75 72 63 65 46 69 6c 65  
01 00 04 28 49 29 56 01  00 13 6a 61 76 61 2f 69  
6f 2f 50 72 69 6e 74 53  74 72 65 61 6d 01 00 07  
70 72 69 6e 74 6c 6e 0a  00 05 00 06 01 00 10 6a  
61 76 61 2f 6c 61 6e 67  2f 53 79 73 74 65 6d 01  
00 04 28 49 29 49 0c 00  1b 00 17 01 00 03 6f 75  
74 07 00 0d 01 00 09 66  69 62 6f 6e 61 63 63 69 
0c 00 14 00 12 0a 00 1a  00 18 01 00 15 4c 6a 61 
76 61 2f 69 6f 2f 50 72  69 6e 74 53 74 72 65 61  
6d 3b 00 21 00 1a 00 05  00 00 00 00 00 03 00 01  
00 04 00 0a 00 01 00 0c  00 00 00 11 00 01 00 01  
00 00 00 05 2a b7 00 15  b1 00 00 00 00 00 09 00  
0e 00 02 00 01 00 0c 00  00 00 18 00 02 00 01 00  
00 00 0c b2 00 10 10 0a  b8 00 1d b6 00 08 b1 00
00 00 00 00 09 00 1b 00  17 00 01 00 0c 00 00 00  
37 00 03 00 01 00 00 00  1b 1a 9a 00 05 03 ac 1a 
04 a0 00 05 04 ac 1a 04  64 b8 00 1d 1a 05 64 b8 
00 1d 60 ac 00 00 00 01  00 0f 00 00 00 04 00 02  
06 06 00 01 00 11 00 00  00 02 00 09 
  • How did you get this class file in the first place? Rather than dumping the result, it would be clearer if you'd provide a [mcve] which *produces* invalid code. – Jon Skeet Mar 31 '17 at 18:42
  • 1
    I would not look at the output; but on the way how you created it. – GhostCat Mar 31 '17 at 18:42
  • @JonSkeet ... might I ask you for a small favor? – GhostCat Mar 31 '17 at 18:43
  • I would like to know what is incorrect about this. Sure I will edit the question and attach the source code. – user3255780 Mar 31 '17 at 18:44
  • 1
    Are you saying this class file was created using a regular Java compiler with that source file? (Well, presumably a complete source file, which that isn't...) Which compiler? – Jon Skeet Mar 31 '17 at 18:53
  • Yes this is compiled using the Java compiler. if I ran the class file using java -noverify it outputs 55 which is correct. However, without "noverify" I get the exception described above – user3255780 Mar 31 '17 at 18:56

2 Answers2

0

Your class file is bad. Copy the source (since you have it) into the IDE of your choice and rebuild it (usually in a menu called Build, Run, or Project).

I took your source code, added the missing brace at the end, compiled it, and ran it using java fibo and it worked just fine. As a side note, Java classes should start with capital letters.

ndm13
  • 1,189
  • 13
  • 19
  • Hi, thanks for responding. Do you know what is bad about it? why the "code segment has wrong length". if you compare mine to the one you produced are you able to tell me what the problem is with mine? – user3255780 Apr 01 '17 at 00:17
  • 1
    If this is a recurring problem you're having, you should reinstall Java and your IDE. There's really no reason to do a byte-for-byte comparison, as it probably wouldn't yield anything useful. I can post the compiled class of you still want it, but I doubt anything of worth will come of it. As far as the specific error, I'd try searching for documentation. – ndm13 Apr 01 '17 at 00:22
0

Well, in the end it’s only one byte that is incorrect. As the exception tells you, there is one Code attribute in the class file that has an incorrect length. The fact that it has an incorrect length can be deduced from its internal structure, which allows to calculate the length, it should have. Without looking at the internal structure, a parser would break after the method, by attempting to interpret the wrong subsequent bytes as class attributes.

Your class has a compiler generated constructor, followed by the main method, finally followed by the fibo method, which is the one having a Code attribute of length 55 (hex: 37), which should be 49 (hex: 31). So in your hex dump

ca fe ba be 00 00 00 34  00 1f 0c 00 19 00 1e 01 
00 16 28 5b 4c 6a 61 76  61 2f 6c 61 6e 67 2f 53  
74 72 69 6e 67 3b 29 56  01 00 10 6a 61 76 61 2f 
6c 61 6e 67 2f 4f 62 6a  65 63 74 01 00 06 3c 69  
6e 69 74 3e 07 00 03 0c  00 04 00 0a 07 00 13 0a  
00 07 00 1c 01 00 09 66  69 62 6f 2e 6a 61 76 61  
01 00 03 28 29 56 07 00  16 01 00 04 43 6f 64 65  
01 00 04 66 69 62 6f 01  00 04 6d 61 69 6e 01 00  
0d 53 74 61 63 6b 4d 61  70 54 61 62 6c 65 09 00 
0b 00 01 01 00 0a 53 6f  75 72 63 65 46 69 6c 65  
01 00 04 28 49 29 56 01  00 13 6a 61 76 61 2f 69  
6f 2f 50 72 69 6e 74 53  74 72 65 61 6d 01 00 07  
70 72 69 6e 74 6c 6e 0a  00 05 00 06 01 00 10 6a  
61 76 61 2f 6c 61 6e 67  2f 53 79 73 74 65 6d 01  
00 04 28 49 29 49 0c 00  1b 00 17 01 00 03 6f 75  
74 07 00 0d 01 00 09 66  69 62 6f 6e 61 63 63 69 
0c 00 14 00 12 0a 00 1a  00 18 01 00 15 4c 6a 61 
76 61 2f 69 6f 2f 50 72  69 6e 74 53 74 72 65 61  
6d 3b 00 21 00 1a 00 05  00 00 00 00 00 03 00 01  
00 04 00 0a 00 01 00 0c  00 00 00 11 00 01 00 01  
00 00 00 05 2a b7 00 15  b1 00 00 00 00 00 09 00  
0e 00 02 00 01 00 0c 00  00 00 18 00 02 00 01 00  
00 00 0c b2 00 10 10 0a  b8 00 1d b6 00 08 b1 00
00 00 00 00 09 00 1b 00  17 00 01 00 0c 00 00 00
 this byte should be 31
 ↓ 
37 00 03 00 01 00 00 00  1b 1a 9a 00 05 03 ac 1a 
04 a0 00 05 04 ac 1a 04  64 b8 00 1d 1a 05 64 b8 
00 1d 60 ac 00 00 00 01  00 0f 00 00 00 04 00 02  
06 06 00 01 00 11 00 00  00 02 00 09 

When changing this byte, the class will work without problems. How this byte became wrong, is impossible to say without context. A compiler should normally produce correct byte code and I never encountered such a kind of error with a normal compiler.

Holger
  • 285,553
  • 42
  • 434
  • 765