1

Hi I need to execute a JAR when user clicks a button from rails application then get a string represenation of some data executed by some method in the jar and show it on the view of a web page. I see a lot of answers here and there but nothing concrete...

  1. How is the output of the JAR is going to be handled? Writing to stdout example using System.out.print("data things"+"and another data"+"and some 3rd data") is a viable option ?

  2. If so how does rails-ruby pick up this stream of data ? Please provide a rails script for total noob...

user3172824
  • 301
  • 1
  • 2
  • 8

2 Answers2

1

This is more of a ruby question than a rails question: everything here is straight ruby.

You can call any command in the shell with backticks, and you get the stdout result in a string as the returned value. eg, in my console.

irb(main):035:0> myvar = `ls -l`
=> "total 688\ndrwxr-xr-x  6 max max   4096 Jul 26  2011 app\n-rw-r--r--  1 max max    173 Aug 17  2011 Capfile\ndrwxr-xr-x  9 max max   4096 Jun 10 14:35 config\ndrwxr-xr-x  4 max max   4096 May  8 09:37 db\n-rw-r--r--  1 max max   2914 May  8 09:37 default.gems\ndrwxr-xr-x  5 max max   4096 May 16  2012 doc\n-rw-r--r--  1 max max    271 Apr 22 16:04 elearning.tm_properties\n-rw-r--r--  1 max max      0 Aug 17  2011 favicon.ico\ndrwxr-xr-x  8 max max   4096 Nov 28  2013 features\ndrwxr-xr-x  8 max max   4096 Nov 23  2012 index\n-rw-r--r--  1 max max 614404 May  8 09:37 less\ndrwxr-xr-x  6 max max   4096 Jun 10 16:02 lib\ndrwxr-xr-x  3 max max   4096 May 29  2013 log\ndrwxr-xr-x 31 max max   4096 Jun  5 13:14 public\n-rw-r--r--  1 max max    307 Jan 22  2013 Rakefile\n-rw-r--r--  1 max max      4 May  8 09:37 README\n-rw-r--r--  1 max max    188 Aug 17  2011 README_JAY\ndrwxr-xr-x 10 max max   4096 May  9 12:51 script\ndrwxr-xr-x  6 max max   4096 Apr 22 16:04 spec\ndrwxr-xr-x  2 max max   4096 Aug 17  2011 stories\ndrwxr-xr-x  5 max max   4096 Nov 22  2013 test\ndrwxr-xr-x  8 max max   4096 May 29 11:04 tmp\ndrwxr-xr-x  4 max max   4096 Jul 26  2011 vendor\n"
irb(main):036:0> puts myvar
total 688
drwxr-xr-x  6 max max   4096 Jul 26  2011 app
-rw-r--r--  1 max max    173 Aug 17  2011 Capfile
drwxr-xr-x  9 max max   4096 Jun 10 14:35 config
drwxr-xr-x  4 max max   4096 May  8 09:37 db
...etc

So, you would run your jar by working out what you would put into the terminal to run the jar, then just putting this in backticks in your app. If running the jar outputs to stdout you will get that back as the result of running the command.

I can't give you an example using your "data things" stuff as i don't understand what you're trying to do there.

EDIT: by the way, you can use the string evaluation syntax #{} in the backticks text, which allows you to build dynamically generated shell commands. eg

folder = "#{ENV['HOME']}/Downloads"
my_downloads = `ls #{folder}`.split("\n")
Max Williams
  • 32,435
  • 31
  • 130
  • 197
1

I had a project I needed to work on that involved this exact thing not too long ago. The way we handled communication between the two was using HTTPClient. Rails is entirely server side and the jar file will most likely be executed locally so you have to have an intermediary between the two, which is where HTTPClient comes in.

If you don't want to use HTTPClient (which might be easier) you can also do what these guys did which was use java.net.HttpURLConnection and run with that.

Hope that helps :)

Community
  • 1
  • 1
MCBama
  • 1,432
  • 10
  • 18
  • I don't see anything in the question suggesting that the jar file isn't on the same box as the rails app. – Max Williams Jun 10 '14 at 15:16
  • Fair enough. I made an assumption that the jar file would be executed locally. Either way though, the HTTPClient would work as would HttpURLConnection. Just be different URLs – MCBama Jun 10 '14 at 15:21
  • Why use http at all though if you just want to run a jar file that's sitting in your filesystem somewhere? – Max Williams Jun 10 '14 at 15:23
  • Depends on what the overall situation is. For example my project did have the JAR file executing locally (god awful old code that I wasn't allowed to change. Who uses JApplets anymore?) and I had to make it work. If my assumption was wrong then my answer probably isn't the "best" so he'll chose yours, simple as that. – MCBama Jun 10 '14 at 15:27