0

I have a jar file that I want to launch from a bash script. This jar includes references to an external folder that contains images.

When I am running the jar from command line with the absolute path to the jar, all works OK. The problem appears when I run it from a bash script. Apparently the folder that contains the images is not found.

Launching from command line:

java -Djava.library.path=/opt/opencv/build/lib -Xmx1g -jar /home/version4/Podo.jar

Bash script:

#! /bin/bash -x
cmd="java -Djava.library.path=/opt/opencv/build/lib -Xmx1g -jar /home/version4/Podo.jar"
eval $cmd

The directory where are my images are is: /home/version4/img

The Java code for accesing the images:

 String img_header="./img/HEADER.png";
 String img_body="./img/BODY.png";
 BufferedImage header,body;
 header=ImageIO.read(new File(img_header));
 body=ImageIO.read(new File(img_body));

The output error:

   javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)

Can anyone tell me what I am doing wrong? Thank you.

Amrida D
  • 329
  • 1
  • 5
  • 17

2 Answers2

0

Try giving the absolute path of the image (in your java code) if you are running the bash script from a location other than your jar location.

RuntimeException
  • 1,593
  • 2
  • 22
  • 31
0

You should run your bash script from the same directory as you run your jar file. Still, it can be located in another place, the only meaning detail is your working directory. The better way you have is to store the images inside jar, see Getting a BufferedImage as a resource so it will work in JAR file

In any case, you should not use absolute paths to retrieve the images as this solution just destroys any portability of your jar.

Community
  • 1
  • 1
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
  • Thanks! The problem is that I cannot store the inside the jar, because they will be replaced from time to time by the user, and a process of update images has to be done. So the user has to have the access to this images... – Amrida D Apr 01 '15 at 10:16
  • Then you may use some settings, which user can change. – Dmitry Ginzburg Apr 01 '15 at 10:19
  • Yes, like an XML file where is the path for my images. – Amrida D Apr 01 '15 at 10:20