views:

102

answers:

4

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?

+6  A: 

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.

Bozho
well I think he for some reason must have thought it would be instructive to try to solve the problem procedurally, but why do it in Java, then? Anyway, thanks for the quick answer.
Gabriel Burns
well, Java can also be used in a procedural way. Especially if it is an algorithm purely.
Bozho
A: 

That said, should this be a single file, or single class file. If the former you can of course ZIP/JAR it, if the latter a little chat with the TA would be the way to go.

+3  A: 

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.

Peter Tillemans
Yes, that is crazy! :)
Jeff
The easier way is actually to just append your jar file to the class file. The main class should then extend ClassLoader to read the jar data from its own file. That's how you could write single file installation (or just runnable) programs before jar.
relet
+1  A: 

As a TA, if a student send me a single java file, with an object-oriented design and nested classes, I would love it!

If the TA wanted the simplest solution to the problem and you over-engineered it, than it's of course another story.

Note that if the TA does not like nested classes and think they are bad, point him to NewSpeak and Gilad Bracha's posts. He's been involved in the Java Language Specification, he is an authority in the field and came up with a language entirely based on class nesting!

ewernli