Time to build a simple environment capable of compiling and running Java applications to access Cassandra. .


  1. Create a top-level folder and several sub folders for this project:
    $ mkdir ~/cassenv
       $ cd ~/cassenv
       $ mkdir src/{java,test}
       $ mkdir lib
  2. Copy JAR files from your Cassandra distribution into the lib directory:
    $ cp <cassandra-home>/lib/*.jar ~/cassenv/lib 

    From the JUnit installation, copy the junit.jar into your library path. Java applications can use JUnit tests for better code coverage:

    $ cp <junit-home>/junit*.jar ~/cassenv/lib 
  3. build.xml file will be required for use with Ant.The properties that represent critical paths to the build are typically specified at the top of the file:
    <project name="alicass" default="dist" basedir="."> 
      <property name="src" location="src/java"/> 
      <property name="test.src" location="src/test"/> 
      <property name="build" location="build"/> 
      <property name="build.classes" location="build/classes"/> 
      <property name="test.build" location="build/test"/> 
      <property name="dist"  location="dist"/> 
      <property name="lib" location="lib"/>

    Ant has tags that help build paths. This is useful for a project that requires multiple JAR files in its classpath to run:

      <path id="alicass.classpath"> 
        <pathelement location="${build.classes}"/> 
        <fileset dir="${lib}" includes="*.jar"/> 

    Exclude test cases classes from the final JAR and create a separate source and build path for the test cases:

      <path id="alicass.test.classpath"> 
        <pathelement location="${test.build}"/> 
        <path refid="alicass.classpath"/> 

    The init target creates directories used in parts of the build:

      <target name="init"> 
        <mkdir dir="${build}"/> 
        <mkdir dir="${build.classes}"/> 
        <mkdir dir="${test.build}"/> 

    syntax errors are highlighted in this section:

      <target name="compile" depends="init"> 
        <javac srcdir="${src}" destdir="${build.classes}"> 
           <classpath refid="alicass.classpath"/> 
      <target name="compile-test" depends="init"> 
        <javac srcdir="${test.src}" destdir="${test.build}"> 
          <classpath refid="alicass.test.classpath"/> 

    The test target looks for filenames that match certain naming conventions and executes them as a batch of JUnit tests. In this case, the convention is any file that starts with Test and ends in .class:

      <target name="test" depends="compile-test,compile" > 
        <junit printsummary="yes" showoutput="true" > 
          <classpath refid="alicass.test.classpath" /> 
            <fileset dir="${test.build}" includes="**/Test*.class" /> 

    If the build step succeeds, the dist target creates a final JAR alicass.jar:

      <target name="dist" depends="compile" > 
        <mkdir dir="${dist}/lib"/> 
        <jar jarfile="${dist}/lib/alicass.jar" basedir="${build.classes}"/> 

    The run target will allow us to execute classes we build:

     <target name="run" depends="dist"> 
        <java classname="${classToRun}" > 
          <classpath refid="alicass.classpath"/> 

    The clean target is used to remove files left behind from older builds:

      <target name="clean" > 
        <delete dir="${build}"/> 
        <delete dir="${dist}"/> 

    Now that the build.xml file is constructed, we must verify it works as expected. Create small Java applications in both the build and test source paths. The first is a JUnit test in src/test/Test.java:

    import junit.framework.*; 
    public class Test extends TestCase { 
        public void test() { 
            assertEquals( "Equality Test", 0, 0 ); 
  4. Next, write a simple "Hello cassandra" program cassenv/src/java/Hello.java:
    public class Hello { 
      public static void main(String [] args){ 
        System.out.println("yo cassandra"); 
  5. Call the test target:
    $ ant test 
    Buildfile: /home/alinafe.matenda/cassenv/build.xml 
        [junit] Running Test 
        [junit] Tests run: 1, Failures: 0, Errors: 0, 
    Time elapsed: 0.012 sec 
    Total time: 5 seconds
  6. Call the dist target. This will compile source code and build a JAR file:
    $ ant dist 
     [jar] Building jar: /home/alinafe.matneda/cassenv/dist/lib/alicass.jar 
    Total time: 3 seconds

    The jar command will build empty JAR files with no indication that you had specified the wrong path. You can use the -tf arguments to verify that the JAR file holds the content you believe it should:

    $ jar -tf /home/alinafe.matenda/cassenv/dist/lib/alicass.jar 
  7. Use the run target to run the A class:
    $ ant -DclassToRun=Hello run
         [java] Hello cassandra
    Total time: 2 seconds