Search code examples
javajarcompilationcompatibility.class-file

Compile isolated java file


I need to patch an old version of some Java software. The problem is that reconstructing the whole package at the old version would be very difficult because it is a very complex program.

I have the corrected source file. Is there any way to compile just this one file to a class file and then insert it into the jar, or do I have to have every single source file?

UPDATE - How to do a Java Patch

Using Elliott Frisch's answer I was able to make the patch. Here is the command line:

javac -target 1.5 -source 1.5 -cp original.jar;otherlib.jar CorrectedSource.java

So, to sum up. You specify the target version of Java and set the source to match, include the original jar (and any libs) as the classpath, then finally name the source code you want to compile. You can then take the resultant class files and insert them into the jar, replacing the old class files.

To find out the target version of your class file


Solution

  • It should work if you recompile just that one file as long as

    1. You use the same -target <release>, per javac -help that will Generate class files for specific VM version and I believe you need to match the original version.
    2. You replace the version of the old class in the jar file with the new class file you generated in 1. Please, backup the original Jar file first.