How do I find out which version of Java I am using Perl programming and using that execute some jar file based on that?
Asked
Active
Viewed 2,009 times
0
-
1When you are using Perl, you are not using Java. Also, it's possible to have multiple versions of Java installed, but how you determine which is the default is system-dependent. – BlueRaja - Danny Pflughoeft May 10 '10 at 15:30
-
It sounds like he wants to execute a jar file "based on" which Java version he is using.. so I'm thinking he has a few jar files for compatibility with different java versions. I don't know any Perl so I can't help though, but hopefully this will make it clearer to someone who does. – Paul May 10 '10 at 15:37
5 Answers
5
You can capture the Java version from a Perl script using backticks.
$version = `java -version`;
Also see: How can I capture STDERR from an external command? for more alternatives than you can shake a stick at.

Bill the Lizard
- 398,270
- 210
- 566
- 880
-
the `>&1` is to redirect STDERR, but I tried it both ways. In either senario I get **$version = ""** – Eric Fossum May 31 '11 at 17:13
2
in a perl script,
@args = ("java", "-version");
system(@args) == 0
or die "system @args failed: $?"
or rather simply,
system("java -version");

phoenix24
- 2,072
- 2
- 20
- 24
-
1Won't this just print the version out to the console without capturing it? I'm quite rusty at Perl, but I think if he wants to use the result he'll need to capture it with backticks. – Bill the Lizard May 10 '10 at 15:46
-
1@Bill: you are correct. I'm not sure why the OP accepted this answer without testing it first. – Ether May 10 '10 at 16:41
-
@Bill I'm doing the same test and running `$capture = \`java -version>&1\``, but it won't capture anything and I get 0 for $? any help is appreciated – Eric Fossum May 29 '11 at 01:08
-
@Eric: What does the `>&1` at the end do? Can you try it without that and see what you get? (See my answer below.) – Bill the Lizard May 29 '11 at 02:28
0
use IO::Handle;
open OUTPUT, '>', "output.txt" or die $!;
STDERR->fdopen( \*OUTPUT, 'w' ) or die $!;
print `java -version`;

Eddie
- 1
-1
"java -version" will show you the version you are using.
"perl -v" will show you the version of Perl you are using.

decompiled
- 1,853
- 3
- 14
- 19
-
If this is how you do it _using perl_, how would you do it without perl? – Evan Carroll May 10 '10 at 15:52
-1
Use the System command...
system("java -version");
If you are going to export a new Java
system('export PATH=/usr/java1.6/bin:$PATH');

Thalaivar
- 23,282
- 5
- 60
- 71
-
`export` run from a `system()` call will not remain in effect after it ends. – Hasturkun May 10 '10 at 16:01