In my current Java project, I have written a Java program that copies some Java files from package “fr.inria.arles.pankesh.gen” to package “fr.inria.arles.pankesh.impl”.
After copying Java files from package “fr.inria.arles.pankesh.gen” to package “fr.inria.arles.pankesh.impl” , my java files are giving errors , due to package Information change of Java file.
My question is “Is there any way to change package information ( thru java code, not manually) in header , when my program copies Java files from package “fr.inria.arles.pankesh.gen” to package “fr.inria.arles.pankesh.impl”.
If you copy java source files, you need to
packagekeyword.Note that this will correctly “rebase” a file’s package; but it will do nothing to “rebase” other files which might expect to find that file in the “old” package. To fix those other files (if they exist, perhaps your project is designed to not have so many reference, or you intend to copy all the files), you need to open up each file in your project looking for the “moved” class in an
importstatement. If you find it, you need to not copy the import but copy in the correct replacement.When working with a file that doesn’t really “move” but needs to be changed, the best strategy is to write a temporary file, and then to replace the original with the temporary file after the temporary file is completely written. Like so:
This allows you to not be constrained by a number of nasty items, like writing in the file past where you need to read from (which will destroy the information you need before you read it).
The packages
java.ioand the classjava.lang.Stringhave all the utilities to allow you to write these tools, but to go into detail would be far too long a post. Try writing a little bit, search the web for (how do I read lines from a file with java, how do I write lines to a file with java) when you get stuck, post the code for how far you have gotten and your specific error or misunderstanding, and plenty of people will be happy to help.If you want to do this for already-compiled files, you need to either read up a bit on the java class file format, and find the class name constant string, and alter it by a similar procedure above (except you will be writing binary bytes), or you could use a library like ASM to do the same procedure as above (but perhaps save you the mistakes you might make when doing it by hand).