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?
-
I've seen a few assignment submission systems that only allow a single file to be uploaded (accompanied by a text description) - this might be the reason for the one-.class-file limitation. If this is indeed the case, you would probably be fine uploading a single .jar file with an accompanying note.– Anon.Commented Jul 4, 2010 at 21:26
-
Like Bozho said, you can't compile multiple classes into one class file. I would ask the TA for some leniency as was already suggested, but at the same time try to refactor it so that it compiles into a single class file as a backup. Might be able to get it done in time, who knows.– JeffCommented Jul 4, 2010 at 21:30
-
2For the assignments I had to do, I always had to submit the source code.– user85421Commented Jul 5, 2010 at 0:27
4 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.
-
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. Commented Jul 4, 2010 at 21:28
-
well, Java can also be used in a procedural way. Especially if it is an algorithm purely.– BozhoCommented Jul 4, 2010 at 21:42
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.
-
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.– reletCommented Jul 4, 2010 at 21:36
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!
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.