Every once in a while, someone will come on IRC asking for help setting up his or her project for some homework assignment. Paul and I usually have our hands full here (not that they’re never not full) because the teachers seem to enjoy giving out .class files to the students and it’s not always immediately obvious how to set things up properly with JDT. I hope that this blog entry will streamline the setup process for my fellow students.
Before we get going, make sure you are in the ‘Java’ perspective. You can switch to it by going ‘Window > Open Perspective > Other… > Java’ and then clicking ‘OK’.
Now, the first thing you have to do in Eclipse before you start hacking on your Java files is to create a Java project. I too had difficulties accepting this back when I started using Eclipse in 2005 for my own homework assignments. For those of you that are reading this, well, it’s time to accept it too and just get over it, I really don’t know what else to say. :O I got over it and so can you!
To create a new ‘Java Project’, you should go ‘File > New > Java Project’ and then click the ‘Next’ button. Enter in a name for your project and then click the ‘Finish’ button, the defaults shall suffice.
Now I have my fancy ‘StackAssignment’ project in my ‘Package Explorer’ view!
Alright, let’s start writing that stack implementation right away then. Select the ’src’ folder in the ‘Package Explorer’, and then go ‘File > New > Class’. Type in a name for your stack implementation and then click ‘Finish’.
Okay, now we have an editor open. Let’s start coding then! Since our teacher told us to implement his Stack interface, let’s get right to it, shall we?
Woah, what’s this? “Stack cannot be resolved to a type”? It seems I can’t compile my code. What’s a “type” anyway? I don’t think I had this problem when I was using ‘javac’ on the command line. Hm, could this be because the teacher’s Stack.class file isn’t in my project? Let’s try pasting it into the project then!
Well, it’s there now but it doesn’t seem to be taking effect. I still see that nasty red X mark on my beautiful StackImpl.java file! :( Why isn’t the compiler picking it up? The problem here is that the Stack.class file is not in this project’s Java classpath. If the term Java classpath doesn’t make any sense to you, now would be a good time to ask your teacher. It’s okay, I’ll wait.
Or I won’t, because we will now fast forward to five minutes before the assignment is due so I’ll quickly explain how to add the Stack.class file to your project’s classpath so our assignment starts compiling properly. Select your project and then right-click (or left-click where applicable for the left-handed mouse users out there) on it, and select ‘Properties’ at the bottom. Go to ‘Java Build Path > Libraries > Add Class Folder…’.
I’d suggest clicking the ‘Create New Folder…’ button and entering in a name of your choice.
Click ‘OK’ to close the dialog and then ‘OK’ again to close the properties dialog. You should now see that the ‘Package Explorer’ view has been updated with our latest changes. Drag your class files and then drop it into your created class folder.
Notice now that the “Stack cannot be resolved to a type” error has now disappeared! In its place, we are now told to implement the methods that our teacher told us to implement.
Alright, let’s do this!
Well, not really, since I can’t do your homework for you. I’m a student too, so I know how it feels. ;)
Before I sign off, I’ll do a plug here for Wayne and encourage both teachers and students to take a gander at the IDE4EDU project. Thanks for reading and good luck with those assignments.