Kev Jackson
6/10/2007 3:00:00 AM
Hi,
> What effect were you expecting to see?
This may seem fairly trivial (and it is), but I'm writing a jdk/java
switcher app as I spend a lot of time working on open source java and
I need to test on multiple jdks. Now previously I've always opened vi
and made the changes manually, but I decided that a tiny gui program
would give me a chance to do a bit of ruby for a change, and I could
get some more experience with Gtk.
So my code essentially builds a menu of all the different jdks
installed and then when the user picks an option (eg java-gcj), the
code writes the correct JAVA_HOME out to .bash_profile ready for use
The final step was to source the .bash_profile so that I can just
choose a jdk and be immediately ready to use it
Attached the code (it's not OO at all, just a simple script):
#!/usr/bin/ruby
=begin
jdk_switcher.rb - Ruby/Gnome2 JDK switching tool
Copyright (c) 2007 Kevin Jackson
This program is licenced under the Apache Software Licence 2.0
=end
require 'gtk2'
def set_java_home(java_version)
puts "#{java_version} selected"
`head -n-2 $HOME/.bash_profile > $HOME/.bash_profile.new`
`echo '#Modified by
jdk_switcher\nJAVA_HOME=/usr/lib/jvm/#{java_version};export
JAVA_HOME;PATH=$JAVA_HOME/bin:$PATH' >> $HOME/.bash_profile.new`
`mv $HOME/.bash_profile.new $HOME/.bash_profile`
exec "/bin/bash -c 'source ~/.bash_profile;'"
%x{/bin/bash -c ". ~/.bash_profile;"}
end
window = Gtk::Window.new
window.signal_connect("delete_event") {
false
}
window.signal_connect("destroy") {
Gtk.main_quit
}
option_menu = Gtk::OptionMenu.new
menu = Gtk::Menu.new
Dir.foreach("/usr/lib/jvm") do |e|
unless e[0,1] == "."
mi = Gtk::MenuItem.new(e)
mi.signal_connect("activate") {
set_java_home(e)
}
menu.append(mi)
end
end
menu.show_all
option_menu.menu=menu
window.border_width = 10
window.add(option_menu)
window.show_all
Gtk.main