Using Java in Rails with JRuby

JRuby is a pretty good Java implementation of Ruby. One of the biggest advantages of JRuby is that you can use Java Classes in your Ruby on Rails App. You can import JARs and take advantages of all the Java Libraries and Frameworks. In that way you don’t have to throw away your old Java Code. You can build a JAR and use it inside your new fancy Ruby App.

I recommend to build a Uber JAR, which contains all dependencies of your Java App. That is more easy to handle. You have to put your Uber JAR into the “lib” directory of your rails app. Than you have to import the JAR in the “application.rb”.

require "lib/your-java-all.jar"

That’s it! Know you can access the Classes inside the JAR. If you want to use a Java Class in a Controller you have to include the class:

include_class Java::org.trophic.graph.service.LocationServiceImpl
include_class Java::org.trophic.graph.factory.LocationFactory

And here is how you can use the Java Classes:

locationService = LocationFactory.locationService
locations = locationService.locations

And here is the full example of the controller:

class PageController < ApplicationController

  include_class Java::org.trophic.graph.service.LocationServiceImpl
  include_class Java::org.trophic.graph.factory.LocationFactory

  def home
      locationService = LocationFactory.locationService
      @locations = locationService.locations
  end

end

It is super easy! Isn’t it?

Groovy and Java in Mixed Mode

It sucks! It just sucks!

javac is generating byte code. groovyc is generating byte code. bothe are generating byte code for the jvm. From the byte code perspective their is no different between java and groovy. And in the IDE, IntelliJ IDEA, you can easily mix Java classes with groovy classes. No Problem. No errors and no warnings. But as soon you try to compile and to execute everything you get trouble. I did some research and found this article here:

http://groovy.codehaus.org/Mixed+Java+and+Groovy+Applications

It basically says that if you are mixing java and groovy and you have dependencies between them, than it does not work. Because your code at first get compiled with javac and than with groovyc. You have to use interfaces to resolve the dependencies.

Common! Are you serious? Of course their are dependencies if you are mixing java with groovy! And I don’t want write for every single PoJo a fu**ing Interface. That is the Enterprise Overhead approach I try to avoid with a scripting language.

And why it is not possible to compile groovy with javac? I mean their is JSR for groovy. It is standard. It is from the same vendor. And in the end it is anyway just byte code for the JVM. It would absolute make sense! And in that way the groovy + java mix mode problem could be resolved easily.

Hilton WiFi

Nearly everywhere in the USA WiFi is for free. In every Startbucks, in over 10000 McDonalds, on Airports and even in some Safeway’s. But not in the  Hilton Hotels. At least not in Austin. You pay 200 Bugs for a night and if you want to have internet you should pay $90 extra.

Isn’t that ridiculous? In Mariott the WiFi is free!

Thank God that Teathering on Android Phones is free.

 

JRuby

I am familiar with Ruby on Rails. I think that is the best Framework for Web Development. Yesterday I installed JRuby. That is the Ruby language implemented in Java. It is pretty good. Here is the Homepage:

http://www.jruby.org

On the Download Page there is a bundle for Mac OS X, too.

http://www.jruby.org/download

The installation worked pretty smooth. After the installation you can use in the command line like that:

jruby -S --version

You can install the rails framework like that.

jruby -S gem install rails

and create a new project like that

jruby -S rails new blog

It works pretty good. And with JRuby you can directly access Java classes. You can import JAR files and take advantage of the whole Java world.

JRuby + Spring + AspectJ + Neo4J

I am playing around with JRuby and Neo4j as Graph Engine. My set up on the backend side contains a Java Application with Spring Annotations, AspectJ and Neo4J. In the frontend I have a Rails App with JRuby.

Any time I try to access a class that have an annotation like “@NodeEntity”, I get an Exception. It seems that Spring and AspectJ is to much in this chain. I should get rid of Spring and AspectJ.

Uber JAR with Maven

By default maven is generating a small JAR file, which just contains your compiled classes and other project files. If you want to have a single JAR file which also includes all dependent JAR files, you have to create a uber JAR. You can do that with the maven shade plugin. Just put this lines of xml into your pom.xml.

</pre>
<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
	<version>1.4</version>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
			<configuration>
				<shadedArtifactAttached>true</shadedArtifactAttached>
				<shadedClassifierName>all</shadedClassifierName>
			</configuration>
		</execution>
	</executions>
      </plugin>
   </plugins>
 </build>
<pre>

In Maven you can solve every problem. You just need enough XML code for that. If you still have problems, than probably your pom.xml is to short. :-)

 

custom_require>:29:in `require’: no such file to load — mkmf (LoadError)

Today I installed Ruby 1.9.X on a Debian Linux Server. Worked pretty smoothy with “apt-get install ruby1.9.X”. After “gem1.9.1 install rails” I got this Exception:

custom_require>:29:in `require': no such file to load -- mkmf (LoadError)

If you get this error you have to install two more things:

apt-get install ruby1.9.1-dev
apt-get install make

After this update I could install rails.

Install bash-completion

If you are working with Debian Linux you have to the package manager “apt-get” to install packages. What is pretty cool. 1 mil. times better the the shitty RPM System from Red Had. If you want to have autocompletion for apt-get install the package “bash-completion”.

apt-get install bash-completion

and than add this lines to your “/root/.bashrc” file:

if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi

PSQLException: The column index is out of range: 1, number of columns: 0

I tried to write a insert statement with Spring JDB Template for an PostgreSQL Database. My insert string looked like this:

final String sql = "INSERT INTO products (prod_name, prod_key, artifact_id, group_id, link, src, prod_type) VALUES ('?','?','?','?','?','?','?')";

And I got this Exception:

PreparedStatementCallback; SQL []; The column index is out of range: 1, number of columns: 0.; nested exception is org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.

The Problem was that I used ” ‘ ” inside of the sql string close to the “?”. After I removed the ” ‘ ” strings it worked perfectly.

Finding the right Name

If you want to create a new Product, you need a name for it. Assume you have a list with 5 possible names. You are asking a couple of people which name they would choose. Some of the people will ask you what your product is about. But that is not so important.

What have Yahoo or Google to do with Search? Of course everybody is associating Yahoo and Google with Search. But that is just because of the branding in the last years.

And what’s about Audi and Mercedes. The same like with Google and Yahoo. And what to hell have Heroku to do with an AppEngine. Just from the name, nothing. But every Ruby on Rails Developer in Silicon Valley nows Heroku. It’s just because of the Branding.

Installing Ruby on Mac OS X

On Mac OS X Ruby 1.8.X is already pre-installed. But if you want to have the newest version 1.9.X you have to install it again. I assume that you installed already XCode and MacPorts on your machine. Than you have update your MacPorts

sudo port selfupdate

All right. Now you can install the newest version.

sudo port install ruby19

That’s it. Now you have to versions of Ruby on your computer. Check the version.

ruby1.9 --version

You should see something like this here:

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11]

GigaOM Mobilize

Last Monday and Tuesday I went to the GigaOM Mobilize Conf. Here are my notes from the conference:

Adds go over to recommandations, reviews, comments
The user is the OS
Privacy is the currency
Mashup needs Orchestrators
Fjord, small products.

Pandora -> HTML 5 -> No Flash
Iheart radio
80% of usa takes their infos from radio
Spotify -> Facebook

Nielsen
60% usage of apps. 9% browser
Primetime is afternoon, a little bit in the night.
Tablets used in prof. Areas.
 Mostly used for watching tv.
 Most people with iPhone and iPad are using iPhone during the day. In the evening, at home, they are using much more the iPad. 

New formats of advertisements
Advertisements should focus on locationbased services.
Push marketing
Adds as usefull information.
Relationship with user is very important.
Critical mass is important for adds. Specially local users.

Voice just 4% of net traffic
Voice as api in Apps
8 billion USD for roaming, every year, worldwide

http://lemon.com/

Intuit 15 mill. Customers.
Google valet

FlixLab.com
Localmind.com
Mobiflock.com
Parse.com
Quantance.com
RightScript.com
Tackable.com
Voicebeam.com
lumoback.com (Eric schmidt)

The arduino platform. The Linux for Hardware devices.
Tingle (mobile local dating), buy Karma! :-) 

Git add, commit, push, pull

If you make changes on your local repository you can add all your changes to your local history with

git add .

With the . you add all new files to the local history. With this command you commit everything to your local git repository.

git commit -m "new changes"

You can do multiple commit. If you feel you are done and everything is good enough to share it with your co-workers you can push all your commits to the server.

git push

If you want the updates from your co-workers you have to pull the changes.

git pull

Git pull is similar to svn update.