Skip to content

Instantly share code, notes, and snippets.

@drhuffman12
Last active August 29, 2015 14:12
Show Gist options
  • Save drhuffman12/a8c0fbc1cfb4f9864792 to your computer and use it in GitHub Desktop.
Save drhuffman12/a8c0fbc1cfb4f9864792 to your computer and use it in GitHub Desktop.
Get password from console via Java and JRuby
# GIST: "pw_from_console.rb" under "https://gist.github.com/drhuffman12"
# If you're dealing with a Java character array (such as password characters that you read from the console), you can convert it to a JRuby string with the following Ruby code:
jconsole = Java::java.lang.System.console()
password = jconsole.readPassword()
ruby_string = ''
password.to_a.each {|c| ruby_string << c.chr}
# .. do something with 'password' variable ..
puts "password_chars: #{password_chars.inspect}"
puts "password_string: #{password_string}"
# See also "http://stackoverflow.com/a/27628738/4390019", and "http://stackoverflow.com/a/27628756/4390019"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment