Will Sherwood
Will Sherwood

Reputation: 1484

Compiling a Java file out of context

I have a large pre-compiled project with lots of packages and class files. I have extracted one of the class files and decompiled it and edited some of the code inside. Now I would like to compile the changed code and re-insert it back into the original pre-compiled project, but unfortunately the code keeps many references to Objects in the pre-compiled project so I cannot compile without having it be already in the project which creates a rather large paradox. is there any for me to do what I am trying to accomplish?

Upvotes: 0

Views: 68

Answers (2)

Alexandre Santos
Alexandre Santos

Reputation: 8338

I would recompile the whole thing to avoid problems, but if you MUST, try this and let me know if they work for you:

Instead of loading the class on your original project, load it using classForName http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html

Remember that you cannot change the signature of the methods as this would indicate a different object since it wouldn't follow the same contract (interface).

Also keep in mind the serialVersionId What is a serialVersionUID and why should I use it?

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500625

Just compile it with a classpath which refers to the existing class files (or the jar file that contains those class files). There should be no problem.

However, note that if you change any constants in the file, those changes won't be reflected in any other code that refers to those constants.

It would generally be a much better idea to recompile from the complete source code. It would also be a better idea to use the original source code than just the result of decompilation - do you not have access to the original source code? (If you don't, are you sure that what you're doing is even legal in your country? I'm not a lawyer, but you should at least check...)

Upvotes: 2

Related Questions