Re: How to print out the currently used JAR file path to verify itis using right JAR?

Discussion in 'Java' started by Arne Vajhøj, Nov 6, 2011.

  1. Arne Vajhøj

    Arne Vajhøj Guest

    On 10/7/2011 9:56 AM, www wrote:
    > My Java program running on linux machines fails on the code which is
    > using 3rd party JAR file. The error message suggest that it is
    > probably using an old version JAR file which I am not aware where it
    > is. If it is using the NEW jar file, the program should pass.
    >
    > I hope to add some printing statements in the code to show the JAR
    > file name and path right before the code crashes. Is there a way to do
    > it?


    The classpath should be explicit (no CLASSPATH env var) and so simple
    or at least structured that it should not be a problem to know
    where classes are loaded from.

    There are several ways of getting the location of the
    Foobar class:

    Foobar.class.getProtectionDomain().getCodeSource().getLocation().getPath()

    Foobar.class.getClassLoader().getResource("Path.class").getPath()

    But to avoid future problems may I suggest that you in the
    startup code find the jar for some of the critical libs
    and check the version and if it is too old then you
    print location and version and exit.

    JarFile jar = new JarFile(fullfnm);
    Manifest mf = jar.getManifest();
    String ver = mf.getEntries().get(name).getValue("Implementation-Version");

    should get a version number from the jar file for a
    properly build jar file.

    Arne
     
    Arne Vajhøj, Nov 6, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    329
    Laurent Bugnion [MVP]
    Jan 17, 2007
  2. Replies:
    0
    Views:
    734
  3. Tim Golden
    Replies:
    0
    Views:
    816
    Tim Golden
    Oct 11, 2006
  4. harryos
    Replies:
    2
    Views:
    446
    harryos
    Mar 11, 2011
  5. markspace
    Replies:
    2
    Views:
    497
    markspace
    Oct 7, 2011
Loading...

Share This Page