运行 目标文件夹内的 jar 与项目文件夹内的 运行 jar

Running jar inside target folder vs running jar inside project folder

我有一个奇怪的问题。当我从项目文件夹中 运行ning 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/";

有什么问题吗?我怎样才能避免这种情况?

您的 Camel 路由从端点 file://src/main/resources/view/flight/following/default/3648/flt_data 消耗,该端点作为 相对 路径给出(我猜在您的项目中)。因此,是否从 target 目录执行 jar 会有所不同。这是因为您直接从文件系统而不是类路径读取文件。

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

  1. 您将文件视为您的应用程序使用的已提供数据:然后将文件的路径作为应用程序的参数传递给您的路由,即您当前使用的文件名常量使用然后可能变得无用。

  2. 您将文件视为一种资源(即某种不可变数据):然后您可以为您的资源创建一个 InputStream 并从中读取字节。在你的情况下,你会写这样的东西:

     InputStream resourceStream = getClass().getResourceAsStream(DEFAULT_FLT_DATA_PATH);
     // Read bytes from resourceStream
    

有关 getResourceAsStream 的文档,请参阅 here

如果您使用 Camel 路线, suggests that you can use Camel's Stream component(它还建议使用 InputStream 的上述方法,但细节较少)。但是,它没有提到如何,我也不知道。我建议要求澄清该答案。