Skip to content
Advertisement

Running jar inside target folder vs running jar inside project folder

I have weird problem. When I’m running jar from project folder which goes:

java -jar ./target/project.jar

everything works fine, path is read correctly.

JavaScript

but when I run jar inside target folder

JavaScript

it stucks and nothing is doing further

The path is all the time the same:

JavaScript

What can be wrong? How can I avoid such a situation?

Advertisement

Answer

You Camel route consumes from endpoint file://src/main/resources/view/flight/following/default/3648/flt_data which is given as a relative path (in your project I guess). Therefore, it makes a difference when you execute your jar from target directory or not. This is because you are reading the file directly from the file system and not from your classpath.

In order to avoid this, there are (at least) 2 approaches:

  1. You consider the file as provided data consumed by your application: then pass the path to the file as an argument of your application and pass it to your route, the constants for file names you are currently using then become probably useless.

  2. You consider the file as a resource (i.e. sort of immutable data): then you can create an InputStream for your resource and read the bytes from it. In your case, you would write something like this:

    JavaScript

See here for the documentation of getResourceAsStream.

If you have to use a Camel route, this answer suggests that you can use Camel’s Stream component (it also suggests the above approach using an InputStream, but in lesser detail). However, it does not mention how and I don’t know either. I would advice to ask for clarification on that answer.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement