Recommended Setup for Working in Java

Environments

Java environments vary somewhat from one to another.   For example, IBM's VisualAge for Java has its own source repository that is mostly transparent to you, and that does a lot of the organization for you, without you having to worry about it yourself.   Unfortunately, most other environments force you to do more work to set things up (although they all have some kind of convention that you can follow if you wish).   Most environments follow the same conventions that JavaSoft set up with its JDK, which is to say that they use directories and subdirectories to organize the various files you use in your programming projects.

Directories

It is important to follow a sensible convention when it comes to doing Java programming.  You should keep each project's source code in its own set of distinct directories.  You should also cause the output of your compiles (the .class files) to be placed in their own set of distinct directories.   Whether you're using the bare JDK from a command line, or some kind of Interactive Development Environment (IDE), you need to learn how to set yourself up to follow these good practices.  Following these suggestions will make things easier for you in the long run.

Overall Structure

Let's take the assignments as an example.  Here's what I strongly suggest you do:

  1. Create, somewhere on one of your disks (not a floppy, because they are very slow!) a directory -- called, let's say CS585_Assignments.  Or, you could use a directory CS585, with a subdirectory below that called Assignments. The main point is that you want to separate your files from others on your disk, so you can find them easily.

  2. For each individual assignment:

    1. Create a subdirectory below the one created in the step 1, called Assignment1, for the first assignment, Assignment2 for the second one, etc.

    2. Below the subdirectory created in step 2a, create two subdirectories, called sources and classes.

    3. Create your java sources in subdirectories below the sources subdirectory;  the names of each such subdirectory will be the same as the package name of the classes that belong to that package.  You will have to create each subdirectory manually.

    4. When you compile (using the JDK javac command, or from within your IDE), tell the compiler to place its output in the classes subdirectory created in step 2b.  The compiler will automatically place the .class files that it creates into the appropriate subdirectory below this, creating the subdirectory if necessary.
      You will also have to tell the compiler where to find the .class files that your program refers to;  this means setting up the CLASSPATH to include your classes subdirectory, and all the other entries that are required for the compiler to find the classes from the JDK.
      For the case of an IDE, you will have to find out how to do this, since every IDE sets this up differently.

    5. When you run such a Java program (using the JDK java command, or from within your IDE), tell it that your classes are to be found in the classes subdirectory you created in step 2b.  You do this by including your classes subdirectory in the CLASSPATH. 
      For the case of an IDE, you will have to find out how to do this, since every IDE sets this up differently.

For example, here's the structure for assignment 2:

CS585
  Assignments
    Assignment2
      classes
        timer	<- contains Timer.class, Time.class
        names	<- contains Name.class, NamesTest.class
      sources
        timer	<- contains Timer.java
        names	<- contains Name.java, NamesTest.java

Note that the directories and files in green are ones that you create manually, while the directories and files in blue are ones that the javac compiler creates for you, assuming that you have specified the outputs correctly.

As you can see, we've explicitly separated out both the sources and the classes for each project.  Preventing such mixing is usually very helpful, because it can prevent you encountering some annoying bugs that are hard to detect and/or fix.

This may seem a mite overly structured, but, believe me, following these recommendations can make life much easier for you, once you have the hang of it.

 

This page was last modified on 02 October, 2007