Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compile a class along with all of its inner classes to a single class file?

I've been working on a fairly simple project for a class. I knew it was supposed to be written in Java, and I read enough of the Assignment description to have an idea what I was supposed to be doing, so I set about creating a nice, object-oriented solution ('cause it's Java, right?). When I finally get to reading the nitty-gritty details of the assignment, I come upon this little gem: The whole thing is supposed to be submitted as a single class file. It's too late to rewrite the whole thing now, so I tried to work around it by making all my classes static inner classes of the primary class. To my chagrin, I discovered that eclipse, at least by default, compiles the inner classes to separate class files still. I unfortunately don't know much about Java compiler settings, but I'm hoping theres a way to get them all compiled to one .class file. Is is it possible, or must I simply turn in what I've got with a note and take whatever my TA decides to dock me for it?

like image 532
Gabriel Burns Avatar asked Jul 04 '10 21:07

Gabriel Burns


2 Answers

I'm afraid there is no such option. Each class is defined in its own class file. Even anonymous classes are defined in ParentClass$1.class

What I would suggest is to put a huge comment/documentation on why you think it is not good to put everything in one class. Of course it depends on the person "on the other end".

If one file, rather than one class is required, simply make a jar file.

like image 173
Bozho Avatar answered Sep 30 '22 20:09

Bozho


If you are feeling brave you could create a jar for your application, encode it as a string in a your toplevelclass which extends a classloader and use this classloader to load the classes from the decoded jar file.

This is so crazy and shows so much knowledge of the Java platform it has to be worth extra credits.

like image 26
Peter Tillemans Avatar answered Sep 30 '22 18:09

Peter Tillemans