javaapache-camelcamel-jdbc

Package not found when create data source using camel


I tried to replicate the same example given in the following question.

import javax.sql.DataSource;    
import org.apache.camel.main.Main;
import org.apache.camel.builder.RouteBuilder;
import org.apache.commons.dbcp.BasicDataSource;

public class JDBCExample {

    private Main main;

    public static void main(String[] args) throws Exception {
        JDBCExample example = new JDBCExample();
        example.boot();
    }

    public void boot() throws Exception {
        // create a Main instance
        main = new Main();
        // enable hangup support so you can press ctrl + c to terminate the JVM
        main.enableHangupSupport();

        String url = "jdbc:oracle:thin:@MYSERVER:1521:myDB";
        DataSource dataSource = setupDataSource(url);

        // bind dataSource into the registery
        main.bind("myDataSource", dataSource);

        // add routes
        main.addRouteBuilder(new MyRouteBuilder());

        // run until you terminate the JVM
        System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
        main.run();
    }

    class MyRouteBuilder extends RouteBuilder {
        public void configure() {
            String dst = "C:/Local Disk E/TestData/Destination";
            from("direct:myTable")
               .setBody(constant("select * from myTable"))
               .to("jdbc:myDataSource")
                .to("file:" + dst);
        }
    }

    private DataSource setupDataSource(String connectURI) {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        ds.setUsername("sa");
        ds.setPassword("devon1");
        ds.setUrl(connectURI);
        return ds;
    }
}

I have included the camel-jdbc-3.0.1.jar and my db specific jar file in my class path.

When I try to compile the code using the following command

javac -cp .;D:\Code\bin JDBCExample.java

I am getting the following error.

JDBCExample.java:2: error: package org.apache.camel.main does not exist
import org.apache.camel.main.Main;
                            ^
JDBCExample.java:3: error: package org.apache.camel.builder does not exist
import org.apache.camel.builder.RouteBuilder;
                               ^
JDBCExample.java:4: error: package org.apache.commons.dbcp does not exist
import org.apache.commons.dbcp.BasicDataSource;

Where am I going wrong? I tried adding camel-core to the classpath, but it didn't help.

Kindly let me know your thoughts, thanks in advance.


Solution

  • You did well by adding camel-core to your classpath, but camel-core and camel-jdbc do not suffice, you should also add the following dependencies:

    JDBCExample.java:2: error: package org.apache.camel.main does not exist import org.apache.camel.main.Main;

    Add camel-main dependency

    JDBCExample.java:4: error: package org.apache.commons.dbcp does not exist import org.apache.commons.dbcp.BasicDataSource;

    Add commons-dbcp dependency

    JDBCExample.java:3: error: package org.apache.camel.builder does not exist import org.apache.camel.builder.RouteBuilder;

    Add camel-core dependency

    With these and the camel-jdbc dependency, you are good to go.

    I suggest that you use maven to handle your dependencies (and much more) if you can... If you have not used it before this five minutes quickstart will gently introduce you to it.

    Here is a sample pom.xml that resolves all these dependencies correctly

    <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">
      <modelVersion>4.0.0</modelVersion>
      <groupId>demo</groupId>
      <artifactId>camel-jdbc-demo</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>camel-jdbc-demo</name>
      <url>http://maven.apache.org</url>
      <dependencies>
    
        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-main -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-main</artifactId>
            <version>3.0.1</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>3.0.1</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jdbc -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jdbc</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>