# Chiamare Java con JRuby

**URL:** <https://www.ruby-forum.com/t/chiamare-java-con-jruby/167037>\
**Category:** IT\
**Created:** [May 2, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/chiamare-java-con-jruby/167037 "2009-05-02T14:50:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dynamic84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@dynamic84](https://www.ruby-forum.com/u/dynamic84)\
**Post date:** [May 2, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/chiamare-java-con-jruby/167037/1 "2009-05-02T14:50:53Z")

</div>

Buon giorno,  
in questi mesi sono impegnato nella gestione di un progetto  
universitario per la Creazione di un sito di gestione annunci…

Ho qualche problema nel comprendere se la mia impostazione Ã¨ corretta  
per l’esecuzione delle varie operazioni.

Dopo aver scritto il codice Java, ed esportando il codice Jar creo il  
mio progetto JRuby.

Utilizzo la versione agg. Jruby --version 1.8.6  
Rails – version 2.3.2  
mongrel Versions: 1.1.5  
jdbc-mysql Version: 5.0.4

La mia idea Ã¨ quella di utilizzare la parte java per realizzare ogni  
operazione con JRuby.

Il progetto in Java Ã¨ stato realizzato con la libreria  
Driver JDBC Mysql -mysql- connector-java-5.1.6-bin.jar  
E quÃ¬ sorgono i dubbi… le due versioni devono esser uguali ???  
la mia risposta: che per me non serve quell’impostazione visto che  
utilizzo le varie classi che funzionano in Java.

Realizzato un PostManagement dove al suo interno ha il metodo addPost()  
Questo metodo richiede un’instanza di PostInformation che Ã¨ un’altra  
classe con tutta la sfilza di set per il Post, si connette ed aggiunge a  
Db\_mysql

Ho settato il file enviroment.rb del contenuto della folder Controller  
require ‘rubygems’  
gem ‘activerecord-jdbc-adapter’

# require ‘jdbc\_adapter’, perchÃ¨ lo esegue il Java

include Java

config.frameworks -= [:active\_record]

Dir["#{RAILS\_ROOT}/lib/\*\*/\*.jar"].each do |jarfile|  
require jarfile  
end

Poi ho creato il controller\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

def PostController \< ApplicationController

def create  
@post\_info = PostInformation.new

# inserimento dati manualmente

```
post_info.setTitle(params[:title])
post_info.setDescription(params[:description])

```

@post\_mgm = PostManagement.new  
post\_mgm.addPost(post\_info)

```
respond_to do |format|

  if post_mgm.addPost(post_info)
    flash[:notice] = 'Post creato con successo.'
    format.html { redirect_to(@post) }
    format.xml { render :xml => @post, :status => :created,

```

:location =\> @post }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @post.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

def new  
@post = post\_mgm.addPost(post\_info)  
respond\_to do |format|  
format.html # new.html.erb  
format.xml { render :xml =\> @post }  
end  
end

* * *

Model

class Post

# questo serve o Ã¨ giusto per comunicare con la view

def title  
@post\_info = PostInformation.new  
@title = post\_info.setTitle(" ")  
end

end

* * *

view

# New post

\<% form\_tag :controller =\> ‘posts’, :active =\> ‘create’ do %\>

\<%= label\_tag :title %\>  
 \<%= text\_field\_tag :title %\>

\<%= label\_tag :description %\>  
 \<%= text\_area\_tag :description %\>

\<%= submit\_tag 'Create' %\>

\<% end %\>

\<%= link\_to ‘Back’, posts\_path %\>

Grazie Mille, Ã¨ corretto questo inserimento  
Daniel
