I am trying to read a folder which contains 2 types of files, JSON and XML.
I need to read the file content and then copy these files to different target folders depending upon the file content. For example, in a JSON file, if person.city == London, then I need to copy these file in target/city/London. Similarly, in an XML file, if person.city == Mumbai then I need to copy the file in target/city/Mumbai.
Code to compile and execute the file:
mvn clean package
mvn camel:run -Dcamel.main.durationMaxMessages=2
I am trying to write this logic, but sometimes it does not get compile, sometime throws an error and I am not able to move forward.
My POM file content:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0" xmlns:xsi=";
xsi:schemaLocation=".0.0 .xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.camel.learn</groupId>
<artifactId>first-camel-integration</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>A Camel Route</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<log4j2-version>2.13.3</log4j2-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bom</artifactId>
<version>3.18.4</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
<version>3.20.0</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
<version>${log4j2-version}</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jsonpath</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>3.20.0</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Allows the example to be run via 'mvn camel:run' -->
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>3.20.0</version>
<configuration>
<logClasspath>true</logClasspath>
<mainClass>org.apache.camel.learn.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
My MainApp.java
package org.apache.camel.learn;
import org.apache.camel.main.Main;
public class MainApp {
/**
* A main() so we can easily run these routing rules in our IDE
*/
public static void main(String... args) throws Exception {
Main main = new Main();
main.configure().addRoutesBuilder(new MyRouteBuilder());
main.run(args);
}
}
MyRoutebuilder.java
package org.apache.camel.learn;
import org.apache.camel.builder.RouteBuilder;
public class MyRouteBuilder extends RouteBuilder {
/**
* Let's configure the Camel routing rules using Java code...
*/
public void configure() {
// here is a sample which processes the input files
// (leaving them in place - see the 'noop' flag)
// then performs content based routing on the message using XPath
from("file:src/data?noop=true")
.choice()
.when(simple("${file:ext} == 'xml'"))
.choice()
.when(xpath("/person/city = 'London'"))
.log("UK message")
.to("file:target/messages/uk")
.when(xpath("/person/city = 'Mumbai'"))
.log("India message")
.to("file:target/messages/india")
.otherwise()
.log("Other message")
.to("file:target/messages/others")
.end()
.when(simple("${file:ext} == 'json'"))
.unmarshal().json(JsonLibrary.Jackson) // Convert JSON to Java Object
.choice()
.when().jsonpath("$.person.city == 'London'")
.log("UK message")
.to("file:target/messages/uk")
.when().jsonpath("$.person.city == 'Mumbai'")
.log("India message")
.to("file:target/messages/india")
.otherwise()
.log("Other message")
.to("file:target/messages/others")
.end()
.otherwise()
.log("Unsupported file type")
.to("file:target/messages/others")
.end();
}
}
Error I get:
[ERROR] *************************************
[ERROR] Error occurred while running main from: org.apache.camel.learn.MainApp
[ERROR]
java.lang.reflect.InvocationTargetException
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:118)
at java.lang.reflect.Method.invoke (Method.java:580)
at org.apache.camel.maven.RunMojo$1.run (RunMojo.java:412)
at java.lang.Thread.run (Thread.java:1583)
Caused by: java.lang.NoClassDefFoundError: org/apache/camel/vault/HashicorpVaultConfiguration
at org.apache.camel.learn.MainApp.main (MainApp.java:14)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
at java.lang.reflect.Method.invoke (Method.java:580)
at org.apache.camel.maven.RunMojo$1.run (RunMojo.java:412)
at java.lang.Thread.run (Thread.java:1583)
Caused by: java.lang.ClassNotFoundException: org.apache.camel.vault.HashicorpVaultConfiguration
at java.URLClassLoader.findClass (URLClassLoader.java:445)
at java.lang.ClassLoader.loadClass (ClassLoader.java:593)
at java.lang.ClassLoader.loadClass (ClassLoader.java:526)
at org.apache.camel.learn.MainApp.main (MainApp.java:14)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
at java.lang.reflect.Method.invoke (Method.java:580)
at org.apache.camel.maven.RunMojo$1.run (RunMojo.java:412)
at java.lang.Thread.run (Thread.java:1583)
[ERROR] *************************************
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.774 s
[INFO] Finished at: 2025-02-06T16:19:56+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.camel:camel-maven-plugin:3.20.0:run (default-cli) on project first-camel-integration: null: MojoExecutionException: InvocationTargetException: org/apache/camel/vault/HashicorpVaultConfiguration: org.apache.camel.vault.HashicorpVaultConfiguration -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1]
I am trying to read a folder which contains 2 types of files, JSON and XML.
I need to read the file content and then copy these files to different target folders depending upon the file content. For example, in a JSON file, if person.city == London, then I need to copy these file in target/city/London. Similarly, in an XML file, if person.city == Mumbai then I need to copy the file in target/city/Mumbai.
Code to compile and execute the file:
mvn clean package
mvn camel:run -Dcamel.main.durationMaxMessages=2
I am trying to write this logic, but sometimes it does not get compile, sometime throws an error and I am not able to move forward.
My POM file content:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.apache.camel.learn</groupId>
<artifactId>first-camel-integration</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>A Camel Route</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<log4j2-version>2.13.3</log4j2-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bom</artifactId>
<version>3.18.4</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
<version>3.20.0</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
<version>${log4j2-version}</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jsonpath</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>3.20.0</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Allows the example to be run via 'mvn camel:run' -->
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>3.20.0</version>
<configuration>
<logClasspath>true</logClasspath>
<mainClass>org.apache.camel.learn.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
My MainApp.java
package org.apache.camel.learn;
import org.apache.camel.main.Main;
public class MainApp {
/**
* A main() so we can easily run these routing rules in our IDE
*/
public static void main(String... args) throws Exception {
Main main = new Main();
main.configure().addRoutesBuilder(new MyRouteBuilder());
main.run(args);
}
}
MyRoutebuilder.java
package org.apache.camel.learn;
import org.apache.camel.builder.RouteBuilder;
public class MyRouteBuilder extends RouteBuilder {
/**
* Let's configure the Camel routing rules using Java code...
*/
public void configure() {
// here is a sample which processes the input files
// (leaving them in place - see the 'noop' flag)
// then performs content based routing on the message using XPath
from("file:src/data?noop=true")
.choice()
.when(simple("${file:ext} == 'xml'"))
.choice()
.when(xpath("/person/city = 'London'"))
.log("UK message")
.to("file:target/messages/uk")
.when(xpath("/person/city = 'Mumbai'"))
.log("India message")
.to("file:target/messages/india")
.otherwise()
.log("Other message")
.to("file:target/messages/others")
.end()
.when(simple("${file:ext} == 'json'"))
.unmarshal().json(JsonLibrary.Jackson) // Convert JSON to Java Object
.choice()
.when().jsonpath("$.person.city == 'London'")
.log("UK message")
.to("file:target/messages/uk")
.when().jsonpath("$.person.city == 'Mumbai'")
.log("India message")
.to("file:target/messages/india")
.otherwise()
.log("Other message")
.to("file:target/messages/others")
.end()
.otherwise()
.log("Unsupported file type")
.to("file:target/messages/others")
.end();
}
}
Error I get:
[ERROR] *************************************
[ERROR] Error occurred while running main from: org.apache.camel.learn.MainApp
[ERROR]
java.lang.reflect.InvocationTargetException
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:118)
at java.lang.reflect.Method.invoke (Method.java:580)
at org.apache.camel.maven.RunMojo$1.run (RunMojo.java:412)
at java.lang.Thread.run (Thread.java:1583)
Caused by: java.lang.NoClassDefFoundError: org/apache/camel/vault/HashicorpVaultConfiguration
at org.apache.camel.learn.MainApp.main (MainApp.java:14)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
at java.lang.reflect.Method.invoke (Method.java:580)
at org.apache.camel.maven.RunMojo$1.run (RunMojo.java:412)
at java.lang.Thread.run (Thread.java:1583)
Caused by: java.lang.ClassNotFoundException: org.apache.camel.vault.HashicorpVaultConfiguration
at java.net.URLClassLoader.findClass (URLClassLoader.java:445)
at java.lang.ClassLoader.loadClass (ClassLoader.java:593)
at java.lang.ClassLoader.loadClass (ClassLoader.java:526)
at org.apache.camel.learn.MainApp.main (MainApp.java:14)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
at java.lang.reflect.Method.invoke (Method.java:580)
at org.apache.camel.maven.RunMojo$1.run (RunMojo.java:412)
at java.lang.Thread.run (Thread.java:1583)
[ERROR] *************************************
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.774 s
[INFO] Finished at: 2025-02-06T16:19:56+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.camel:camel-maven-plugin:3.20.0:run (default-cli) on project first-camel-integration: null: MojoExecutionException: InvocationTargetException: org/apache/camel/vault/HashicorpVaultConfiguration: org.apache.camel.vault.HashicorpVaultConfiguration -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Share
Improve this question
edited Feb 6 at 13:43
Mark Rotteveel
109k226 gold badges155 silver badges219 bronze badges
asked Feb 6 at 8:53
D555D555
1,8406 gold badges28 silver badges51 bronze badges
4
- this is a compilation error, it is not on "running your code". Have you checked all your scopes for your dependencies? – Stultuske Commented Feb 6 at 8:55
- I meant to say, if I change the code then code gets comiled but breaks on runtime – D555 Commented Feb 6 at 8:57
- then you should show us those changes, and show the error you get on runtime – Stultuske Commented Feb 6 at 9:04
- Changed title now accordingly – D555 Commented Feb 6 at 9:10
2 Answers
Reset to default 0For the first error (cannot find symbol ... JsonLibrary) you are missing an import:
import org.apache.camel.model.dataformat.JsonLibrary;
UPDATE:
For the second error: after an end()
you cannot continue with a when()
, switch to a endChoice()
instead of the end()
calls wherever you continue with when()
or otherwise()
.
you need to use the same version of Camel dependencies. In your case, you have a bom, no need to override the dependency version after.
Another note is that you are using Camel 3.x which is End of life, see https://camel.apache.org/blog/2024/12/camel3-eol/ You should start using Camel 4.x