Switching between bytecode versions for a Java class file

Considering that a Java class file (ClassName.class) with byte version X has a general way of converting that class file from represented in bytecode version X to 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 very confusing, so decompiling the class with jad or a similar program and then recompiling with "-target ..." does not work.

Updates after the initial post:

  • Update # 1: Futhermore, suppose bytecode version X and bytecode version Y are close enough so that all instructions used by the class (currently in bytecode version X) also exist in version Y.
+2


source to share


4 answers


For downgrading, you can take a look at various methods to get Java 5/6 code running in Java 1.3 / 1.4. See My question to a related question What is Java 5/6 Backport Features for Java 1.4?



+3


source


You can use Apache BCEL

The engineering byte code library is designed to provide users with a convenient ability to parse, create and manipulate (binary) Java class files



BCEL gives you the ability to read in a given version's class file, manipulate it, generate a new stream of class files, and then load it into a virtual machine using a low-level API ClassLoader

. Very inconvenient, no doubt about it, and I doubt it would allow you to downgrade as easily as you could program.

+2


source


Not. Although later versions of Java will be able to execute this bytecode, you cannot update it: later versions of class files have a different format.

You cannot downgrade it either because there is no way to replace missing bytecodes with other constructs in older versions of Java.

[EDIT] IIRC, Sun has added at least one new bytecode instruction for every major Java release. This is usually the reason for major Java releases: changes in bytecode.

However, try your luck and change the base version of the class file and see if your new VM loads. I doubt this will work for any complex example, but you might be in luck.

+1


source


+1


source







All Articles