You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by jean-frederic clere <jf...@fujitsu-siemens.com> on 2002/09/26 11:53:55 UTC

Getting JVM version

Hi,

I am looking for a reliable way to find the JVM version of installed Java.
I have tried java -version but that seems to return weird strings.
Reading java.version via System.getProperties looks better.

Has someone a better solution or an idea?

Cheers

Jean-frederic


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Stefan Bodewig <bo...@apache.org> writes:

> On Thu, 26 Sep 2002, Steve Downey <st...@netfolio.com> wrote:
> 
> > The 'right' way to do it is with System properties.
> 
> Not really.  Most of the properties you list will simply not exist for
> some JVMs and others have formats that are not predictable for non-Sun
> JVMs.

Not that it matters in the trenches, but aren't those properties part
of the JLS?

-- 

Daniel Rall <dl...@finemaltcoding.com>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 26 Sep 2002, Steve Downey <st...@netfolio.com> wrote:

> The 'right' way to do it is with System properties.

Not really.  Most of the properties you list will simply not exist for
some JVMs and others have formats that are not predictable for non-Sun
JVMs.

What I was trying to say is that you're better of to check for the
feature you need via reflection or loading classes.

> It really depends on what you're trying to discover.

I think we agree here.

Stefan

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by Steve Downey <st...@netfolio.com>.
That is just evil. It reminds me of the hacks for browser detection. And is as 
reliable.

The 'right' way to do it is with System properties. You might need to query a 
few to get at what you're looking for, though. 
The on point ones are:
java.specification.version
java.vendor
java.version
java.vm.specification.version
java.vm.version

It really depends on what you're trying to discover.

On the other hand, if you're trying to find out if Timer is available, then 
what you've got is appropriate. It's when you extrapolate from that to other 
properties of the system that you will get into trouble.

On Thursday 26 September 2002 08:17 am, jean-frederic clere wrote:
> Stefan Bodewig wrote:
> > On Thu, 26 Sep 2002, jean-frederic clere
> >
> > <jf...@fujitsu-siemens.com> wrote:
> >>Has someone a better solution or an idea?
> >
> > Try to load a class and catch the ClassNotFoundException - properties
> > have not been reliable so far.  But then again be careful with the
> > class you chose, Kaffe will include ThreadLocal (and therefore look
> > like 1.2) but doesn't include Swing for example.
>
> Something like the following to check that it is at least 1.3:
> +++
> import java.util.Timer;
> public class Version {
>    static public void main(String[] args) {
>      try {
>        Timer time = new Timer();
>        time.cancel();
>      }
>      catch (Exception ex) {
>          System.out.println("Exception " + ex );
>          ex.printStackTrace();
>      }
>    }
> }
> +++
>
> > Stefan
> >
> > --
> > To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> > For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 26 Sep 2002, Steve Downey <st...@netfolio.com> wrote:

> That happens to fail for Kaffe. It has ThreadLocal, but not
> Swing. So it's not really 1.2, for all purposes.

That's the example I used in my first post, yes 8-)

Kaffe is JDK 1.2 for Ant's purposes (it supports context classloaders,
ThreadLocal and so on).

Stefan

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by Steve Downey <st...@netfolio.com>.
That happens to fail for Kaffe. It has ThreadLocal, but not Swing. So it's not 
really 1.2, for all purposes.

On Thursday 26 September 2002 10:59 am, jean-frederic clere wrote:
> Stefan Bodewig wrote:
> > On Thu, 26 Sep 2002, jean-frederic clere
> >
> > <jf...@fujitsu-siemens.com> wrote:
> >>Something like the following to check that it is at least 1.3:
> >
> > much simpler:
> >
> > Class.forName("java.util.Timer");
> >
> > take a look at Ant's JavaVersionHelper (in org.apache.tools.ant.util).
> >
> > Stefan
> >
> > --
> > To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> > For additional commands, e-mail: <ma...@jakarta.apache.org>
>
> In src/main/org/apache/tools/ant/util/JavaEnvUtils.java:
> +++
>          try {
>              javaVersion = JAVA_1_0;
>              javaVersionNumber=10;
>              Class.forName("java.lang.Void");
>              javaVersion = JAVA_1_1;
>              javaVersionNumber++;
>              Class.forName("java.lang.ThreadLocal");
>              javaVersion = JAVA_1_2;
>              javaVersionNumber++;
>              Class.forName("java.lang.StrictMath");
>              javaVersion = JAVA_1_3;
>              javaVersionNumber++;
>              Class.forName("java.lang.CharSequence");
>              javaVersion = JAVA_1_4;
>              javaVersionNumber++;
>          } catch (ClassNotFoundException cnfe) {
> +++
> Exactly what I was looking for!
>
> Thanks
>
> Jean-frederic


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by jean-frederic clere <jf...@fujitsu-siemens.com>.
Stefan Bodewig wrote:
> On Thu, 26 Sep 2002, jean-frederic clere
> <jf...@fujitsu-siemens.com> wrote:
> 
> 
>>Something like the following to check that it is at least 1.3:
> 
> 
> much simpler:
> 
> Class.forName("java.util.Timer");
> 
> take a look at Ant's JavaVersionHelper (in org.apache.tools.ant.util).
> 
> Stefan
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 

In src/main/org/apache/tools/ant/util/JavaEnvUtils.java:
+++
         try {
             javaVersion = JAVA_1_0;
             javaVersionNumber=10;
             Class.forName("java.lang.Void");
             javaVersion = JAVA_1_1;
             javaVersionNumber++;
             Class.forName("java.lang.ThreadLocal");
             javaVersion = JAVA_1_2;
             javaVersionNumber++;
             Class.forName("java.lang.StrictMath");
             javaVersion = JAVA_1_3;
             javaVersionNumber++;
             Class.forName("java.lang.CharSequence");
             javaVersion = JAVA_1_4;
             javaVersionNumber++;
         } catch (ClassNotFoundException cnfe) {
+++
Exactly what I was looking for!

Thanks

Jean-frederic


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 26 Sep 2002, jean-frederic clere
<jf...@fujitsu-siemens.com> wrote:

> Something like the following to check that it is at least 1.3:

much simpler:

Class.forName("java.util.Timer");

take a look at Ant's JavaVersionHelper (in org.apache.tools.ant.util).

Stefan

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by jean-frederic clere <jf...@fujitsu-siemens.com>.
Stefan Bodewig wrote:
> On Thu, 26 Sep 2002, jean-frederic clere
> <jf...@fujitsu-siemens.com> wrote:
> 
> 
>>Has someone a better solution or an idea?
> 
> 
> Try to load a class and catch the ClassNotFoundException - properties
> have not been reliable so far.  But then again be careful with the
> class you chose, Kaffe will include ThreadLocal (and therefore look
> like 1.2) but doesn't include Swing for example.

Something like the following to check that it is at least 1.3:
+++
import java.util.Timer;
public class Version {
   static public void main(String[] args) {
     try {
       Timer time = new Timer();
       time.cancel();
     }
     catch (Exception ex) {
         System.out.println("Exception " + ex );
         ex.printStackTrace();
     }
   }
}
+++

> 
> Stefan
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Getting JVM version

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 26 Sep 2002, jean-frederic clere
<jf...@fujitsu-siemens.com> wrote:

> Has someone a better solution or an idea?

Try to load a class and catch the ClassNotFoundException - properties
have not been reliable so far.  But then again be careful with the
class you chose, Kaffe will include ThreadLocal (and therefore look
like 1.2) but doesn't include Swing for example.

Stefan

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>