This blog post is based on a experimentation I did to integrate Java Swing file chooser on a JRuby on Rails backed web application. Let me warn you, this a dirty hack, wanted to see if the idea would work, not at all recommended for production use. Did this out of curiosity!
Security feature implemented in the web browsers denies the access of real local path of files selected by HTML file upload control. A fake path will be returned instead of the accurate path. This was the motivation to try something out of the box.
Started out by creating a simple file chooser written in JRuby. Invoked this program using Ajax call. Bingo, the file chooser appeared up on the screen. Using this method were able to retrieve the local file path.
2. The FolderChooser.rb
Create a JRuby program, import JFrame and JFileChooser classes from the javax.swing package. Define a method named initUI, inside it make use of the JFrame methods to display the file chooser on top of the window. Place this program inside the lib directory of the Rails application.
3. Method to handle Ajax request
Inside the controller class create a method which handles the Ajax request and invokes the above JRuby program.
4. Route the Ajax request to the controller method
Define a route in the config/routes.rb file to match invoke_chooser path to the invoke_folder_chooser action of PsrOpController.
5. Ajax call to invoke the program
6. Add a button on the view
In this post we explored a way to integrate Java Swing to a Jruby on Rails app to access the local file path.
The full implementation of this post can be found in the GitHub repository.