0

I am using Poi Apache to generate an excel file, I have to add a picture to my file but the problems is when I export my project to a Runnable Jar, it is not working.

InputStream is = ExcelTools.class.getClassLoader().getResourceAsStream( "./ensao/pfa/opendelib/resources/LogoOpen.jpg" );

    byte[] bytes = IOUtils.toByteArray(is);
    int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
    is.close();

    Drawing drawing = sheet.createDrawingPatriarch();
    HSSFClientAnchor anchor = new HSSFClientAnchor(40, 10, 65, 20,
            (short) 0, 0, (short) 0, 0);
    anchor.setAnchorType(1);

    Picture pict = drawing.createPicture(anchor, pictureIdx);
    pict.resize();

the problem is launched from this line :

byte[] bytes = IOUtils.toByteArray(is);
centic
  • 15,565
  • 9
  • 68
  • 125
oussama.elhadri
  • 738
  • 3
  • 11
  • 27
  • 1
    what do you mean by "not working"? the excel file not having the image or is the code throwing exception? if yes, please attach the stack trace – user2507946 Jul 24 '13 at 04:39
  • How are you building the jar-file? getResourceAsStream() returns null if the resource is not found, which is probably what happens here. – centic Jul 24 '13 at 05:50

1 Answers1

1

Problem in bad path to image. Your IDE configured environment ($CLASSPATH, file path, etc) in one manner, but real running operate other environment. For find your working directory do print debug pwd analog.

This links may be useful: http://www.java-forums.org/new-java/434-how-can-i-get-current-directory.html http://www.mkyong.com/java/how-to-get-the-current-working-directory-in-java/ Getting the Current Working Directory in Java

Community
  • 1
  • 1
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
  • just a notification it's works on eclipse but when i export the runnable jar it does not work . – oussama.elhadri Jul 24 '13 at 23:27
  • eclipse configure relative paths and working directory on its manner. It differ from real paths and real working directory. Just print your workin directoty and after it correct path to image. – Michael Kazarian Jul 25 '13 at 05:04