使用骆驼创建数据源时找不到包

Package not found when create data source using camel

我尝试复制以下 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;
    }
}

我在我的 class 路径中包含了 camel-jdbc-3.0.1.jar 和我的数据库特定的 jar 文件。

当我尝试使用以下命令编译代码时

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

我收到以下错误。

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;

我哪里错了?我尝试将 camel-core 添加到 class 路径,但它没有帮助。

请让我知道您的想法,提前致谢。

你通过将 camel-core 添加到你的类路径中做得很好,但是 camel-core 和 camel-jdbc 还不够,你还应该添加以下依赖项:

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

添加camel-main依赖

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

添加commons-dbcp依赖

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

添加 camel-core 依赖项

有了这些和 camel-jdbc 依赖项,你就可以开始了。

如果可以的话,我建议您使用 maven 来处理您的依赖项(以及更多)...如果您之前没有使用过它,five minutes quickstart 会轻轻地向您介绍它。

这是一个示例pom.xml,它正确地解决了所有这些依赖关系

<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>