Java 在目标文件夹内运行jar与在项目文件夹内运行jar

Java 在目标文件夹内运行jar与在项目文件夹内运行jar,java,apache-camel,spring-camel,Java,Apache Camel,Spring Camel,我有个奇怪的问题。当我从项目文件夹运行jar时,如下所示: java-jar./target/project.jar 一切正常,路径读取正确 1107 [main] INFO org.apache.camel.core.xml.AbstractCamelContextFactoryBean - JMXAgent enabled: CamelJMXAgent[usePlatformMBeanServer=true, createConnector=true, registryPort=10098,

我有个奇怪的问题。当我从项目文件夹运行jar时,如下所示:

java-jar./target/project.jar

一切正常,路径读取正确

1107 [main] INFO org.apache.camel.core.xml.AbstractCamelContextFactoryBean - JMXAgent enabled: CamelJMXAgent[usePlatformMBeanServer=true, createConnector=true, registryPort=10098, serviceUrlPath=/, statisticsLevel=All, onlyRegisterProcessorWithCustomId=false, registerAlways=true, registerNewRoutes=true, mask=false]
2657 [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded 179 type converters
2853 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext: data-feed-camel) is starting
2853 [main] INFO org.apache.camel.management.ManagedManagementStrategy - JMX is enabled
3078 [Camel Thread #1 - Camel Thread #0 - JMXConnector: service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/] INFO org.apache.camel.management.DefaultManagementAgent - JMX Connector thread started and listening at: service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/f

3089 [main] INFO org.apache.camel.spring.SpringCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
3107 [main] INFO org.apache.camel.spring.SpringCamelContext - Total 0 routes, of which 0 is started.
3128 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext: data-feed-camel) started in 0.255 seconds


org.apache.camel.component.file.FileEndpoint - Endpoint is configured with noop=true so forcing endpoint to be idempotent as well
    59471 [RMI TCP Connection(5)-10.88.55.167] INFO org.apache.camel.spring.SpringCamelContext - Route: flt_data_for_0543 started and consuming from: Endpoint[file://src/main/resources/view/flight/following/default/3648/flt_data?idempotentRepository=%23repo-flt_data_for_0543&noop=true&readLock=none]
    60552 [Camel (data-feed-camel) thread #4 -
但当我在目标文件夹中运行jar时

1149 [main] INFO org.apache.camel.core.xml.AbstractCamelContextFactoryBean - JMXAgent enabled: CamelJMXAgent[usePlatformMBeanServer=true, createConnector=true, registryPort=10098, serviceUrlPath=/, statisticsLevel=All, onlyRegisterProcessorWithCustomId=false, registerAlways=true, registerNewRoutes=true, mask=false]
2688 [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded 179 type converters
2796 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext: data-feed-camel) is starting
2796 [main] INFO org.apache.camel.management.ManagedManagementStrategy - JMX is enabled
2975 [Camel Thread #1 - Camel Thread #0 - JMXConnector: service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/] INFO org.apache.camel.management.DefaultManagementAgent - JMX Connector thread started and listening at: service:jmx:rmi:///jndi/rmi://pjanik-pc:10098/
2981 [main] INFO org.apache.camel.spring.SpringCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2989 [main] INFO org.apache.camel.spring.SpringCamelContext - Total 0 routes, of which 0 is started.
2991 [main] INFO org.apache.camel.spring.SpringCamelContext - Apache Camel 2.12.2 (CamelContext: data-feed-camel) started in 0.194 seconds
21055 [RMI TCP Connection(4)-10.88.55.167] INFO org.apache.camel.component.file.FileEndpoint - Endpoint is configured with noop=true so forcing endpoint to be idempotent as well
21588 [RMI TCP Connection(4)-10.88.55.167] INFO org.apache.camel.spring.SpringCamelContext - Route: flt_data_for_0432 started and consuming from: Endpoint[file://src/main/resources/view/flight/following/default/3648/flt_data?idempotentRepository=%23repo-flt_data_for_0432&noop=true&readLock=none]
它停滞不前,没有进一步的进展

路径始终相同:

private static final String DEFAULT_DATA_PATH = "view/flight/following/default/3648/";

有什么不对劲?如何避免这种情况?

您的骆驼路线使用端点
file://src/main/resources/view/flight/following/default/3648/flt_data
这是作为相对路径给出的(我想是在您的项目中)。因此,无论您是否从
target
目录执行jar,这都会有所不同。这是因为您直接从文件系统而不是从类路径读取文件

为了避免这种情况,有(至少)两种方法:

>P>您将文件视为应用程序所提供的数据:然后将路径作为应用程序的参数传递给文件,并将其传递到您的路由中,您当前使用的文件名的常数可能变得无用。

> p>将文件视为资源(即不可变数据):然后可以为资源创建<代码>输入流< /代码>,并从中读取字节。在您的情况下,您可以这样写:

 InputStream resourceStream = getClass().getResourceAsStream(DEFAULT_FLT_DATA_PATH);
 // Read bytes from resourceStream
有关
getResourceAsStream
的文档,请参阅


如果必须使用Camel路由,建议您可以使用Camel路由(它还建议使用
InputStream
,但不太详细)。然而,它没有提到如何,我也不知道。我建议您就这个答案进行澄清。

区别在于您的jar直接在文件系统上访问文件,而不是在jar本身中。这个问题解决了您所寻找的问题:嗯,您可能需要一个约定,即您的路由应该始终存储在同一个文件夹中,相对于您的工作目录(因此您执行
java
的目录,而不是jar的位置),例如,如果您的端点定义为
file://routes/flt_data
,则jar将始终读取位于工作目录的子目录
routes
中的文件
flt\u data
,与工作目录的实际位置无关。不一定,这取决于您正在使用的文件的实际性质。如果它确实是一个资源,即该文件包含不可变的数据,则将其保留在
src/main/resources
中,然后继续。另一方面,如果您的文件实际上包含应用程序的输入,那么您可以遵循我的上述建议。据我所知:“Java API”意味着使用
getClass().getResourceAsStream(“/pl/azimuthit/view/flight/following/default/3648/flt_data”)返回的
InputStream
并直接从中读取字节,以便为处理器提供数据;“流组件”是指使用。