2

Given a Java class file (ClassName.class) with bytecode version X is there a general way to convert this class file from being represented in bytecode version X to being represented in bytecode version Y?

Assumptions:

  • The source code is not available. The class file is the only available representation of the class.
  • The class file is heavily obfuscated, so decompiling the class with say jad or similar program and then recompiling it with "-target ..." does not work.

Updates after initial post:

  • Update #1: Futhermore, assume that bytecode version X and bytecode version Y are sufficiently close so that all instructions used by the class (currently in bytecode version X) also exists in version Y.
knorv
  • 49,059
  • 74
  • 210
  • 294

4 Answers4

3

For downgrading you can have a look at various methods to get Java 5/6 code running in Java 1.3/1.4. See my anwser to related question Backport Java 5/6 features to Java 1.4?

Community
  • 1
  • 1
Peter Kofler
  • 9,252
  • 8
  • 51
  • 79
  • Almost all of these tools are (5/6) -> (something earlier). I just want 6->5. – nick Jul 08 '11 at 21:36
  • Retrotranslator should be able to do 6->5. I tried it with HSqlDB 2. Unfortunately it does not handle the new java.sql classes, so it failed. But if you do not use any Java 6 API, give it a try. – Peter Kofler Jul 11 '11 at 19:38
2

You could use Apache BCEL

The Byte Code Engineering Library is intended to give users a convenient possibility to analyze, create, and manipulate (binary) Java class files

BCEL gives you the possibility of reading in a class file of a given version, manipulating it, generating a new class file stream, and then loading that into the VM using the low-level ClassLoader API. Very fiddly, no doubt, and I doubt this will let you downgrade the version as easily as you could prograde.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    ASM would be a better option if support is required for more recent features like annotations ( http://asm.ow2.org/ ). Otherwise, this approach should work. – McDowell Oct 01 '09 at 09:01
  • 1
    Ah, OK. I haven't really been keeping up with this stuff, it scares me. – skaffman Oct 01 '09 at 09:11
1

No. While later versions of Java will be able to execute that bytecode, you can't upgrade it: Later versions of the class files have a different format.

You can't downgrade it either because there is no way to replace the missing bytecodes by other constructs in older versions of Java.

[EDIT] IIRC, Sun added at least a single new bytecode instruction for every major version of Java. This is usually the reason for major Java releases: Changes to the bytecode.

That said, just try your luck and change the major version of the class file and see if your newer VM will load it. I doubt it will work for any complex example but you might be lucky.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    Even though a program may or may not exist, in theory it should be possible though. Just like a compiler transforms sourcecode into a bytecode, in theory a compiler could also exists which translates different bytecode versions to eachother. – Toad Oct 01 '09 at 07:44
  • 1
    reinier: I'm not convinced it's practical. See this document about the Java 6 verifier: https://jdk.dev.java.net/verifier.html Can you build a StackMapTable from bytecode alone? – Aaron Digulla Oct 01 '09 at 07:55
  • Aaron: Please see "Update 1". Does that clarification alter your answer? – knorv Oct 01 '09 at 08:42
  • @knorv: Not really. What are you up to anyway? Why do you need this? – Aaron Digulla Oct 01 '09 at 09:28
  • Aaron: Long story short; I'm stuck with an old version of Java in a production environment. – knorv Oct 01 '09 at 11:28
  • Aaron: With regards to "Sun added at least a single new bytecode instruction for every major version of Java" - true, but usually only a subset of all available bytecode instructions are used in a given Java class. – knorv Oct 01 '09 at 13:12
  • @knorv: Well ... try to reset the major version and see which exceptions the verifyer throws and chew through them. – Aaron Digulla Oct 01 '09 at 14:00
  • Aaron: Yepps, will do :-) Thanks for your answers+comments – knorv Oct 01 '09 at 14:09
1

Use retroweaver

Mike B
  • 1,600
  • 1
  • 12
  • 8