First, you want to find the main method, the file basename is the class name

 CLASS=$(basename -s .java $(grep -l 'public static void main' *.java))

Then, extract all the imports, sort them, and use `uniq`. Assuming there are no wildcard imports.

 sed -n '/^[ \t]*import /p' *.java | sort | uniq > $CLASS.java.output

Then process Java files, extracting imports and packages:

 grep -hv '^[ \t]*import \|^[ \t]*package' | sed -e 's/public class/class/g;s/public interface/interface/g' >>$CLASS.java.output

Then you want to revert your public class

`sed -i "s/class $CLASS/public class $CLASS/g" $CLASS.java.output`