title: Unable to instantiate a GenericRecord in a Kafka Producer for streaming avro to a topic
body: <p>I am trying to create a Java producer that streams Avro to a kafka topic.
  I've tried to reproduce the example that is presented in <a href="https://docs.confluent.io/current/schema-registry/docs/serializer-formatter.html" rel="nofollow noreferrer">Confluent's official documentation</a>  </p>
  
  <p>but GenericRecord class -which is used as shown below- cannot be found. </p>
  
  <pre><code>import org.apache.avro.Schema;
  import org.apache.avro.generic.GenericData;
  import org.apache.avro.generic.GenericRecord;
  
  Schema.Parser parser = new Schema.Parser();
  Schema schema = parser.parse(userSchema);
  GenericRecord avroRecord = new GenericData.Record(schema);
  </code></pre>
  
  <p>And here's the error reported: </p>
  
  <pre><code>Error: A JNI error has occurred, please check your installation and try again
  Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/avro/generic/GenericRecord
      at java.lang.Class.getDeclaredMethods0(Native Method)
      at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
      at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
      at java.lang.Class.getMethod0(Class.java:3018)
      at java.lang.Class.getMethod(Class.java:1784)
      at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
      at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
  Caused by: java.lang.ClassNotFoundException: org.apache.avro.generic.GenericRecord
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      ... 7 more
  </code></pre>
  
  <p>Here's my <code>pom.xml</code> file</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.giorgos.currencies&lt;/groupId&gt;
    &lt;artifactId&gt;giorgos-fx_currencies&lt;/artifactId&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;george-fx_currencies&lt;/name&gt;
    &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;apache-repo&lt;/id&gt;
              &lt;name&gt;Apache Repository&lt;/name&gt;
              &lt;url&gt;https://repository.apache.org/content/repositories/releases&lt;/url&gt;
              &lt;releases&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/releases&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;false&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/repository&gt;
          &lt;repository&gt;
              &lt;id&gt;confluent&lt;/id&gt;
              &lt;url&gt;http://packages.confluent.io/maven/&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
  
      &lt;properties&gt;
          &lt;kafka.version&gt;0.8.2.1&lt;/kafka.version&gt;
          &lt;kafka.scala.version&gt;2.10&lt;/kafka.scala.version&gt;
          &lt;confluent.version&gt;4.0.0&lt;/confluent.version&gt;
          &lt;avro.version&gt;1.7.6&lt;/avro.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
  
     &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.avro&lt;/groupId&gt;
              &lt;artifactId&gt;avro&lt;/artifactId&gt;
              &lt;version&gt;${avro.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.avro&lt;/groupId&gt;
              &lt;artifactId&gt;avro-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;${avro.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.confluent&lt;/groupId&gt;
              &lt;artifactId&gt;kafka-avro-serializer&lt;/artifactId&gt;
              &lt;version&gt;${confluent.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.kafka&lt;/groupId&gt;
              &lt;artifactId&gt;kafka-clients&lt;/artifactId&gt;
              &lt;version&gt;${kafka.version}&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.avro&lt;/groupId&gt;
                  &lt;artifactId&gt;avro-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;${avro.version}&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;generate-sources&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;schema&lt;/goal&gt;
                              &lt;goal&gt;protocol&lt;/goal&gt;
                              &lt;goal&gt;idl-protocol&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;sourceDirectory&gt;${project.basedir}/src/main/resources/avro&lt;/sourceDirectory&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;shade&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
                  &lt;configuration&gt;
                      &lt;finalName&gt;uber-${project.artifactId}-${project.version}&lt;/finalName&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
  &lt;/project&gt;
  </code></pre>
  
  <p>I was wondering whether wrong versions of avro/kafka/confluent are defined in the the <code>pom.xml</code> but I was not able to find the error. Also note that <code>mvn package</code> leads to a successful build without any errors reported. </p>
  
  <p><strong>EDIT:</strong>
  I've tried to comment out these lines but a similar behaviour is observed for the following line: </p>
  
  <pre><code>KafkaProducer producer = new KafkaProducer(props);
  </code></pre>
  
  <p>that gives the following error: </p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/kafka/clients/producer/KafkaProducer
      at com.giorgos.currencies.TestFX.main(TestFX.java:103)
  Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.producer.KafkaProducer
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      ... 1 more
  </code></pre>
  
  <p>This is how I run the program:</p>
  
  <pre><code>java -cp target/giorgos-fx_currencies-1.0-SNAPSHOT.jar com.giorgos.currencies.TestFX command-line-argument
  </code></pre>