Avro - Getting Started (also a good review on using classpath in Java)

Quick note: I downloaeded the php version of avro and that worked, but I had to install  the php5-gmp package.

 

Here's what I did when following the 'getting started' example for using avro (http://avro.apache.org/docs/current/gettingstartedjava.html)

Reminder before getting started: in my research I remembered reading somewhere that if you are using a .jar you must specifify it explicitly in the classpath (you can't just put the directory where your jar files reside and have java search that dir. You need to specifiy each individual .jar in the classpath). Finally, another side note: the difference between -sourcpath and -classpath when compiling java code is that the sourcepath is the path to files you are compiling, while the classpath is the files that are linked to the code you are compiling.

 

1. downloaded avro-1.7.4.jar and avro-tools-1.7.4.jar and put them in C:\avro (download page)

2. downloaded jackson-core-asl-1.9.12.jar and jackson-mapper-asl-1.9.12.jar and put them in C:\avro (download page)

3. copied the user.avsc file from the getting started page and placed it in C:\avro.

4. generated the code file the user.avcs by doing the following command from C:\avro\ (don't forget the trailing period - tells the jar where to put the output):

 

java -jar avro-tools-1.7.4.jar compile schema user.avsc .

 

This created the package structure example/avro/User.class (and a few other class files)

 

5. Created a little sample class to use the new User class that was generated (I put it in c:\avro):

 

import example.avro.*;

public class Test{

public static void main(String[] args){
User u = new User("john", 7, "red");
System.out.println(u.getName());
}

}

 

 

6. compiled the class like this (note that you don't need the c:\avro\ prefix if you are compiling, running the program from that dir). ALSO, note that when I had spaces after the semi-colons in the classpath, it did not compile.

 

C:\avro>javac -cp c:\avro; c:\avro\avro-1.7.4.jar;jackson-core-asl-1.9.12.jar;jackson-mapper-asl-1.9.12.jar; Test.java

here's how I did it on linux (note the sourcepath - I'm not sure why I needed to include the current dir in the sourcepath or classpath):
javac -cp
avro-1.7.4.jar:jackson-core-asl-1.9.12.jar:jackson-mapper-asl-1.9.12.jar -sourcepath ./ Test.java

this also works on linux (adding the current directory to the classpath):
javac -cp ./:avro-1.7.4.jar:jackson-core-asl-1.9.12.jar:jackson-mapper-asl-1.9.12.jar Test.java

 

 

7. Ran the program like this:

 

C:\avro>java -cp avro-1.7.4.jar; jackson-core-asl-1.9.12.jar; jackson-mapper-asl-1.9.12.jar; Test

here's how I did it on linux (note that I had to add the current directory to the classpath):
java -cp ./:avro-1.7.4.jar:jackson-core-asl-1.9.12.jar:jackson-mapper-asl-1.9.12.jar Test