Wednesday, November 12, 2014

Project Organization Part 1: Directory Layout

In our first post we set up a simplified project called HelloWorld.  That is of course, a very standard first program in virtually any language, designed to provide a minimal functional program to provide a bit of user output.  That's all well and good, but in the interest of getting something built I glossed over or just plain ignored some aspects of project setup.

First and foremost, we allowed the source code and the compiled .class files to sit in the same location.  I was tempted to avoid that even for the first post, but eventually decided it was best to just get something done and tackle that in a separate post.  So here we are.

As a general rule, I greatly prefer to set up my projects with two main directory trees, one for source files and one for build products.  Typically I'll use the names src and build for these directories.  You will find that once you start using an IDE this structure will tend to be built automatically for you, which will make it transparent and natural.

So let's take HelloWorld and convert it to a better structure:



Now we've got a couple of buckets for our files.  For now we'll ignore packaging and just rearrange the files.  Since class files can be regenerated at any time, we'll just get rid of HelloWorld.class.  We will then move HelloWorld.java to its new home.


Now let's rebuild our class file to make sure we can still operate.  Go into the build directory and run javac, but this time you'll have to enter a relative path in order for the system to find it.  After that you can, if you so choose, do a test run to make sure it still works correctly.




I mentioned earlier that we're not going to discuss packaging, but I do want to quickly mention that we still have not gone far enough with moving our files around.  The reason for this involves the way that Java loads classes, and has no real implications until you start assembling slightly larger programs.  Suffice it to say that we're not done yet, but we can wait a while before we get into that.  Next time around, we'll talk about functions, which will be a key concept you must understand before we get around to creating objects.

No comments:

Post a Comment