Skip to content

Commit 5f820fe

Browse files
authored
Merge pull request #257 from shuebnersr/fact1754_java_version_fails_when_JAVA_TOOL_OPTIONS_is_used
(FACT-1754) search for matching line with java version
2 parents 231db77 + b7bdf96 commit 5f820fe

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/facter/java_version.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
# positive matches, so this needs to be done manually in setcode.
2323
setcode do
2424
unless [ 'openbsd', 'darwin' ].include? Facter.value(:operatingsystem).downcase
25+
version = nil
2526
if Facter::Util::Resolution.which('java')
26-
Facter::Util::Resolution.exec('java -Xmx12m -version 2>&1').lines.first.split(/"/)[1].strip
27+
Facter::Util::Resolution.exec('java -Xmx12m -version 2>&1').lines.each { |line| version = $~[1] if /^.+ version \"(.+)\"$/ =~ line }
2728
end
29+
version
2830
end
2931
end
3032
end
@@ -34,9 +36,11 @@
3436
has_weight 100
3537
setcode do
3638
Facter::Util::Resolution.with_env("PATH" => '/usr/local/jdk-1.7.0/jre/bin:/usr/local/jre-1.7.0/bin') do
39+
version = nil
3740
if Facter::Util::Resolution.which('java')
38-
Facter::Util::Resolution.exec('java -Xmx12m -version 2>&1').lines.first.split(/"/)[1].strip
41+
Facter::Util::Resolution.exec('java -Xmx12m -version 2>&1').lines.each { |line| version = $~[1] if /^.+ version \"(.+)\"$/ =~ line }
3942
end
43+
version
4044
end
4145
end
4246
end
@@ -46,7 +50,9 @@
4650
has_weight 100
4751
setcode do
4852
unless /Unable to find any JVMs matching version/ =~ Facter::Util::Resolution.exec('/usr/libexec/java_home --failfast 2>&1')
49-
Facter::Util::Resolution.exec('java -Xmx12m -version 2>&1').lines.first.split(/"/)[1].strip
53+
version = nil
54+
Facter::Util::Resolution.exec('java -Xmx12m -version 2>&1').lines.each { |line| version = $~[1] if /^.+ version \"(.+)\"$/ =~ line }
55+
version
5056
end
5157
end
5258
end

spec/unit/facter/java_version_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
let(:facts) { {:operatingsystem => 'OpenBSD'} }
1515
it do
1616
java_version_output = <<-EOS
17+
Picked up JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true
1718
openjdk version "1.7.0_71"
1819
OpenJDK Runtime Environment (build 1.7.0_71-b14)
1920
OpenJDK 64-Bit Server VM (build 24.71-b01, mixed mode)
@@ -30,6 +31,7 @@
3031
let(:facts) { {:operatingsystem => 'Darwin'} }
3132
it do
3233
java_version_output = <<-EOS
34+
Picked up JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true
3335
java version "1.7.0_71"
3436
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
3537
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
@@ -46,6 +48,7 @@
4648
let(:facts) { {:operatingsystem => 'MyOS'} }
4749
it do
4850
java_version_output = <<-EOS
51+
Picked up JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true
4952
java version "1.7.0_71"
5053
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
5154
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

0 commit comments

Comments
 (0)