.Jar file runs well from NeBeans but not from /dist lib

apatia

I am a bit new at Java and Netbeans but I've heard that you are a great and helpful community, so could you give me an advice how is it possible that a source code (after compiled) works fine if I hit the green triangle in NetBeans, but if I open the contain of the DIST/jar file (outside from NetBeans) it is not?

More precisely the application runs well but one specific function doesn't run when I click a button (PDF generation with iText plugin).

Here is a part of code of the not working method (as I mentioned the button works, but only if I start my App from NetBeans)

If I remove the image reference, the Application runs well from outside (total commander, /dist/myapp.jar, Enter), but I really would like to use the image in the PDF file. The path is good (since it is working from netbeans)

So I am 100% sure that the .jar does not find the logo (if it works without it,) but then how could it find if it is started from NetBeans? What could I rewrite at the path? (I tried "/", "@", "/src" while the .png can be found at /src just like the other pictures (which are loaded perfectly) )

 public void pdfGenerate(String title, String text) {

  ............................
        document.open();
        Image image1 = Image.getInstance("src/logo.png");
        image1.scaleToFit(200, 86);
        image1.setAbsolutePosition(200f, 750f);
        document.add(image1);

        document.add(new Paragraph("\n\n\n\n\n" + text, FontFactory.getFont("font", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)));


    } catch (Exception e) {
        e.printStackTrace();
    }
    document.close();
  ...............................
  }
James_D

When you package your application as a jar file, the resources should be packaged there too. The method you are calling interprets the string as a filename; that file will not be present on the system when the jar file is created and deployed.

What you need to do is specify a URL for the resource. You can do this using getClass().getResource(...). If you specify a resource path here starting /, it will be interpreted relative to the classpath; otherwise it is relative to the current class.

Note that there is an overloaded form of Image.getInstance(...) taking a URL.

So, it looks like you need

Image image1 = Image.getInstance(getClass().getResource("/logo.png"));

You can also check the contents of the jar file from the command line with

jar -tf yourfile.jar

to make sure the image file is in there.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to add Maven dependency jar file from the lib folder

From Dev

why eclipse not detect the jar file from lib folder

From Dev

Read property file resides in /ear/war from ear/lib/*.jar

From Dev

Set jar file to remove lib from classpath using NetBeans

From Dev

Set jar file to remove lib from classpath using NetBeans

From Dev

Exclude obfuscated jar from lib folder with ProGuard

From Dev

How to include JSP from jar lib

From Dev

Removing file from jar

From Dev

File from Jar by paths

From Dev

Redirect input from file, but show in stdout as well?

From Dev

Access to file from jar archive

From Dev

Load file dynamically from jar

From Dev

Make jar file from folder

From Dev

Reading a Text File from a .jar

From Dev

downloading jar file from github?

From Dev

Create jar file from maven

From Dev

Using functions from JAR file

From Dev

Gradle: Adding sources.jar file within /lib folder of published dist.zip along with all my other dependencies

From Dev

Batch manually runs well. But When ran from Task scheduler gives error

From Dev

Remove symbol from lib file on windows

From Dev

Gradle - get jar and native lib from Maven (snapshots) repository

From Dev

Getting class from ear/war in ear/lib/jar returns ClassNotFoundException

From Dev

Jar maven dependency disappears from my WEB-INF/lib

From Dev

php exec file runs from terminal, not from browser

From Dev

'mpsyt' command runs from terminal but not from .desktop file?

From Dev

How to get source packages from dir in /usr/lib/python2.7/dist-packages/

From Dev

Run a jar file from a java file

From Dev

automake: exclude intermediary source file from "dist" tarballs

From Dev

automake: exclude intermediary source file from "dist" tarballs

Related Related

  1. 1

    How to add Maven dependency jar file from the lib folder

  2. 2

    why eclipse not detect the jar file from lib folder

  3. 3

    Read property file resides in /ear/war from ear/lib/*.jar

  4. 4

    Set jar file to remove lib from classpath using NetBeans

  5. 5

    Set jar file to remove lib from classpath using NetBeans

  6. 6

    Exclude obfuscated jar from lib folder with ProGuard

  7. 7

    How to include JSP from jar lib

  8. 8

    Removing file from jar

  9. 9

    File from Jar by paths

  10. 10

    Redirect input from file, but show in stdout as well?

  11. 11

    Access to file from jar archive

  12. 12

    Load file dynamically from jar

  13. 13

    Make jar file from folder

  14. 14

    Reading a Text File from a .jar

  15. 15

    downloading jar file from github?

  16. 16

    Create jar file from maven

  17. 17

    Using functions from JAR file

  18. 18

    Gradle: Adding sources.jar file within /lib folder of published dist.zip along with all my other dependencies

  19. 19

    Batch manually runs well. But When ran from Task scheduler gives error

  20. 20

    Remove symbol from lib file on windows

  21. 21

    Gradle - get jar and native lib from Maven (snapshots) repository

  22. 22

    Getting class from ear/war in ear/lib/jar returns ClassNotFoundException

  23. 23

    Jar maven dependency disappears from my WEB-INF/lib

  24. 24

    php exec file runs from terminal, not from browser

  25. 25

    'mpsyt' command runs from terminal but not from .desktop file?

  26. 26

    How to get source packages from dir in /usr/lib/python2.7/dist-packages/

  27. 27

    Run a jar file from a java file

  28. 28

    automake: exclude intermediary source file from "dist" tarballs

  29. 29

    automake: exclude intermediary source file from "dist" tarballs

HotTag

Archive