Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8623 > unrolled thread

How to print out the currently used JAR file path to verify it is using right JAR?

Started bywww <xsli2@yahoo.com>
First post2011-10-07 06:56 -0700
Last post2011-11-06 17:42 -0500
Articles 12 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How to print out the currently used JAR file path to verify it is using right JAR? www <xsli2@yahoo.com> - 2011-10-07 06:56 -0700
    Re: How to print out the currently used JAR file path to verify it is using right JAR? www <xsli2@yahoo.com> - 2011-10-07 07:25 -0700
      Re: How to print out the currently used JAR file path to verify it is using right JAR? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-10-07 10:18 -0500
        Re: How to print out the currently used JAR file path to verify it is using right JAR? www <xsli2@yahoo.com> - 2011-10-07 08:30 -0700
      Re: How to print out the currently used JAR file path to verify it is using right JAR? markspace <-@.> - 2011-10-07 08:39 -0700
        Re: How to print out the currently used JAR file path to verify it is using right JAR? Lew <lewbloch@gmail.com> - 2011-10-07 08:47 -0700
        Re: How to print out the currently used JAR file path to verify it is using right JAR? www <xsli2@yahoo.com> - 2011-10-07 09:37 -0700
          Re: How to print out the currently used JAR file path to verify it is using right JAR? markspace <-@.> - 2011-10-07 11:17 -0700
            Re: How to print out the currently used JAR file path to verify it is using right JAR? www <xsli2@yahoo.com> - 2011-10-07 11:37 -0700
    Re: How to print out the currently used JAR file path to verify it is using right JAR? Roedy Green <see_website@mindprod.com.invalid> - 2011-10-07 10:50 -0700
      Re: How to print out the currently used JAR file path to verify it is using right JAR? www <xsli2@yahoo.com> - 2011-10-07 11:00 -0700
    Re: How to print out the currently used JAR file path to verify it is using right JAR? Arne Vajhøj <arne@vajhoej.dk> - 2011-11-06 17:42 -0500

#8623 — How to print out the currently used JAR file path to verify it is using right JAR?

Fromwww <xsli2@yahoo.com>
Date2011-10-07 06:56 -0700
SubjectHow to print out the currently used JAR file path to verify it is using right JAR?
Message-ID<4d7daeb4-7da7-47c0-a4cf-d3c5d1be2ef0@gd10g2000vbb.googlegroups.com>
Hi,

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?

Thank you very much.

[toc] | [next] | [standalone]


#8626

Fromwww <xsli2@yahoo.com>
Date2011-10-07 07:25 -0700
Message-ID<13afa738-1198-4569-84be-9fc6627c52d6@t16g2000yqm.googlegroups.com>
In reply to#8623
Just to add more details about my question:

In my code, I am using class Abc, which is from 3rd party JAR
file(MyJAR.jar). I hope to print out Abc's class path to see it is
from a/b/MyJAR.old.jar or from c/d/MyJAR.new.jar

I understand the correct way is to check the classpath and which JAR
is front is the one to be used. However, we run into some strange
things here and that method didn't solve the problem.

Thank you.

[toc] | [prev] | [next] | [standalone]


#8627

FromJoshua Cranmer <Pidgeot18@verizon.invalid>
Date2011-10-07 10:18 -0500
Message-ID<j6n57d$s6$1@dont-email.me>
In reply to#8626
On 10/7/2011 9:25 AM, www wrote:
> Just to add more details about my question:
>
> In my code, I am using class Abc, which is from 3rd party JAR
> file(MyJAR.jar). I hope to print out Abc's class path to see it is
> from a/b/MyJAR.old.jar or from c/d/MyJAR.new.jar
>
> I understand the correct way is to check the classpath and which JAR
> is front is the one to be used. However, we run into some strange
> things here and that method didn't solve the problem.

One way, which I don't know if it would work, would be:
System.out.println(Abc.class.getClassLoader().findResource(
   "com/foo/package/Abc.class").toString());

-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth

[toc] | [prev] | [next] | [standalone]


#8628

Fromwww <xsli2@yahoo.com>
Date2011-10-07 08:30 -0700
Message-ID<b8983782-26c0-4c73-a16f-afd4478fc028@q17g2000yqn.googlegroups.com>
In reply to#8627
Thank you for your tip. I didn't try it yet.

I have found one way which is working fine for me on linux:

        try
        {
            final File oddFile = new
File(Abc.class.getProtectionDomain()
                                                                   .getCodeSource()
                                                                   .getLocation()
                                                                   .toURI()
                                                                   .getPath());
            System.out.println(oddFile.getAbsolutePath()); //this will
print out the JAR file which the class Abc is loaded from

        }
        catch(final Exception e)
        {
            System.out.println(e.getMessage());
        }

[toc] | [prev] | [next] | [standalone]


#8629

Frommarkspace <-@.>
Date2011-10-07 08:39 -0700
Message-ID<j6n6ft$9uh$1@dont-email.me>
In reply to#8626
On 10/7/2011 7:25 AM, www wrote:
> Just to add more details about my question:
>
> In my code, I am using class Abc, which is from 3rd party JAR
> file(MyJAR.jar). I hope to print out Abc's class path to see it is
> from a/b/MyJAR.old.jar or from c/d/MyJAR.new.jar
>
> I understand the correct way is to check the classpath


Shouldn't you just be setting this from the command line or in the main 
Jar file?  I'm a little confused by your question.  You should know the 
class path before the program starts executing.

Can you just search the existing class path for occurrences of this 
class "Abc"?


[toc] | [prev] | [next] | [standalone]


#8631

FromLew <lewbloch@gmail.com>
Date2011-10-07 08:47 -0700
Message-ID<3339288.679.1318002451499.JavaMail.geo-discussion-forums@prmi2>
In reply to#8629
markspace wrote:
> www wrote:
>> Just to add more details about my question:
>>
>> In my code, I am using class Abc, which is from 3rd party JAR
>> file(MyJAR.jar). I hope to print out Abc's class path to see it is
>> from a/b/MyJAR.old.jar or from c/d/MyJAR.new.jar
>>
>> I understand the correct way is to check the classpath
> 
> Shouldn't you just be setting this from the command line or in the main 
> Jar file?  I'm a little confused by your question.  You should know the 
> class path before the program starts executing.
> 
> Can you just search the existing class path for occurrences of this 
> class "Abc"?

System.getProperty("java.class.path")
<http://download.oracle.com/javase/7/docs/api/java/lang/System.html#getProperty(java.lang.String)>

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#8632

Fromwww <xsli2@yahoo.com>
Date2011-10-07 09:37 -0700
Message-ID<cd3a9132-42f5-489d-add0-00050a21d579@dm9g2000vbb.googlegroups.com>
In reply to#8629
On Oct 7, 11:39 am, markspace <-@.> wrote:
> On 10/7/2011 7:25 AM, www wrote:
>
> > Just to add more details about my question:
>
> > In my code, I am using class Abc, which is from 3rd party JAR
> > file(MyJAR.jar). I hope to print out Abc's class path to see it is
> > from a/b/MyJAR.old.jar or from c/d/MyJAR.new.jar
>
> > I understand the correct way is to check the classpath
>
> Shouldn't you just be setting this from the command line or in the main
> Jar file?  I'm a little confused by your question.  You should know the
> class path before the program starts executing.
>
> Can you just search the existing class path for occurrences of this
> class "Abc"?

My project is using A.jar, B.jar, C.jar, D.jar etc and they all are in
the classpath. Inside my code, I am using the class Abc. My program
works fine on one linux machine and fails on another almost identical
linux machine. Using the printing method above, I just found out, to
my surprise, both B.jar and F.jar have Abc.class. I was totally
unaware of this. In one machine, Abc is loaded from B.jar and in
another machine, loaded from F.jar.

With the cause being found, now it is easy to fix the problem. Thank
you.

[toc] | [prev] | [next] | [standalone]


#8637

Frommarkspace <-@.>
Date2011-10-07 11:17 -0700
Message-ID<j6nfoa$8q9$1@dont-email.me>
In reply to#8632
On 10/7/2011 9:37 AM, www wrote:
>  In one machine, Abc is loaded from B.jar and in
> another machine, loaded from F.jar.


How are you running this A.jar such that you don't know that F.jar is in 
the path?  I'm honestly curious; diagnosing installation problems like 
this is not my strong suit.

[toc] | [prev] | [next] | [standalone]


#8638

Fromwww <xsli2@yahoo.com>
Date2011-10-07 11:37 -0700
Message-ID<5de7c38c-5be0-4174-8b70-d5f9bc9e7317@z19g2000vby.googlegroups.com>
In reply to#8637
On Oct 7, 2:17 pm, markspace <-@.> wrote:
> On 10/7/2011 9:37 AM, www wrote:
>
> >  In one machine, Abc is loaded from B.jar and in
> > another machine, loaded from F.jar.
>
> How are you running this A.jar such that you don't know that F.jar is in
> the path?  I'm honestly curious; diagnosing installation problems like
> this is not my strong suit.

Mark:

In the classpath, there are A.jar, B.jar, C.jar, .., F.jar etc. I need
to use a class called "Abc" which is in F.jar. My code works in
Eclipse. I delievered it. It works fine on one machine and fails on
another machine. The error message is "NoSuchMethodError:
Abc.methodNewA()". So, Abc class is available(since NoClassDef error
is not shown), but the method methodNewA() is not available. I suspect
that a wrong version Abc class is used. I want to find out where Abc
class is loaded from. This is the reason I posted my initial question.
With this code:

        try
        {
            final File oddFile = new
File(Abc.class.getProtectionDomain()
                                                                   .getCodeSource()
                                                                   .getLocation()
                                                                   .toURI()
                                                                   .getPath());
            System.out.println(oddFile.getAbsolutePath()); //this will
print out the JAR file which the class Abc is loaded from

        }
        catch(final Exception e)
        {
            System.out.println(e.getMessage());
        }

I found out that on the failing machine, Abc class is mistakenly
loaded from B.jar, not F.jar. And my teammate just told me that B.jar
was created at the old time. It has many things in it which were used
in the past. It also has Abc class in it, but apparently is very old
version. After careful check, we found that B.jar actually is
completely un-needed anymore. So the easy fix for us is just to delete
B.jar and let F.jar take the world.

[toc] | [prev] | [next] | [standalone]


#8634

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-10-07 10:50 -0700
Message-ID<lreu87tls368f28lvhl9ilkgcemhfab3jb@4ax.com>
In reply to#8623
On Fri, 7 Oct 2011 06:56:57 -0700 (PDT), www <xsli2@yahoo.com> wrote,
quoted or indirectly quoted someone who said :

>
>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?

I don't have a canned solution, just some notes on implementing one.
See 
http://mindprod.com/project/pathtool.html
http://mindprod.com/project/which.html
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to 
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.

[toc] | [prev] | [next] | [standalone]


#8636

Fromwww <xsli2@yahoo.com>
Date2011-10-07 11:00 -0700
Message-ID<7de77d14-5e39-48d9-a31c-c605144a72a1@d18g2000yql.googlegroups.com>
In reply to#8634
On Oct 7, 1:50 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Fri, 7 Oct 2011 06:56:57 -0700 (PDT), www <xs...@yahoo.com> wrote,
> quoted or indirectly quoted someone who said :
>
>
>
> >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?
>
> I don't have a canned solution, just some notes on implementing one.
> Seehttp://mindprod.com/project/pathtool.htmlhttp://mindprod.com/project/which.html
> --
> Roedy Green Canadian Mind Productshttp://mindprod.com
> It should not be considered an error when the user starts something
> already started or stops something already stopped. This applies
> to browsers, services, editors... It is inexcusable to
> punish the user by requiring some elaborate sequence to atone,
> e.g. open the task editor, find and kill some processes.

JWhich may be the tool:

http://www.fullspan.com/proj/jwhich/index.html

I didn't try it yet. It seems very powerful.

[toc] | [prev] | [next] | [standalone]


#9689

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-11-06 17:42 -0500
Message-ID<4eb70d56$0$287$14726298@news.sunsite.dk>
In reply to#8623
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web