Contact

Java, Scala and JRuby Hello World in one Project.

See the code first, then we will get into the tutorial on how to set up your environment. I will clean up the style of this tutorial later, I just wanted to get it up.



code



jruby.java

   import org.jruby.embed.ScriptingContainer;
   public class JRuby {                                              
	
	public static void main(String[] args) 
	{                     
	System.out.println("Java, Scala and Ruby using the JRE.\n"); 
	
	ScriptingContainer container = new ScriptingContainer();
    
    container.runScriptlet("puts 'This is JRuby code in .java file!'");
    
	new ScallaHello().hello(); 
	
	System.out.println("This is standard Java Code in .java file!"); 
	
	}
   }

ScalaHello.scala

   class ScallaHello {
  
    def hello() { 
	  println("This is a Scala Method in .scala file, object set in .java file")
    }
  
    System.out.println("This is Java Code in .scala file!")
    println("This is Scalla Code in .scala file!")
   }


Results...

Java, Scala and Ruby using the JRE.
This is JRuby code in .java file!
This is Java Code in .scala file!
This is Scalla Code in .scala file!
This is a Scala Method in .scala file, object set in .java file
This is standard Java Code in .java file!



If you already have Eclipse, you should be able to use that version, or follow these steps and install another Eclipse to keep this environment separate.

  1. Download Eclipse 3.7 Indigo from >>http://www.eclipse.org/downloads/ and choose the Eclipse IDE for Java Developers. Unzip in a folder easy for you to find. If not familiar with Eclipse, it does not automatically load into your program files. I placed mine in a documents folder. Once unzipped it is fully operational. You can pin the exceptional file to your task bar if easier for you.
  2. Download scala-2.9.1.final.zip file for Eclipse at >>http://www.scala-lang.org/downloads  

Follow instructions. At the time of this install, it involves pasting the download page/link into your Eclipse “Install New Software” section under the Help Tab in Eclipse.

  1. After pasting the link and naming your saved location, you should see the items…Scala IDE

Make sure you expand these and see all boxes, select ‘Select All’ then select Next>, select Next> again to install. Accept terms and select Finish. Make sure you go through terms that need to select and see all windows for this are selected. I ran into an issue downloading until I went through the checkboxes and “Accept Terms” more thoroughly. Once everything installs properly it should restart or ask for a restart.
Now Eclipse and Scala should be installed, but it still needs to be configured to work properly.

  1. After Eclipse restarts, it should ask you if you want to check if plug-in settings are correct, select yes.
  2. JDT Weaving should be enabled. Select recommended default settings and select OK. Scala JDT
  3. Now you should be able to select new project File>>New>Project… then select Scala Wizard>>Scala Project. Name your project then select next. Create Scala Project

Now we are going to take a break from Scala and download JRuby. This step can be skipped if you don’t want Ruby, or you can use this a the only step to just add JRuby to a Java project.
Download JRuby

  1. Go to http://jruby.org/download and download JRuby 1.6.6 Complete .jar
  2. Place file in location you will remember as you will have to access it in Eclipse in a couple steps from now.
  3. Right click your Scala Package in the Package Explorer... Scala Project Explorer
  4. Select Build Path, then select Add External Archives… Find your JRuby jar you just downloaded (placed in a safe folder before this step) and select it. Eclipse will load this to your project along with your other Libraries. There might be a better way to include this with the JRE System Library as well that would eliminate this step.

Back to configuring your project in Eclipse…

  1. Right click Scala Package again in the Package Explorer and select Build Path then select Configure Build Path…. Go to Order and Export tab and check mark all Libraries, then select OK. Java Build Path
  2. Time To Build your project:  Right Click Scala Package>>New>>Other…
  3. Java>>Class, Select Next
  4. Name it JRuby and check the boxes as this and select Finish…JRuby Class
  5. Next create a Scala Class: Right Click Scala Package>>New>>Scala Class
  6. Name it ScallaHello, select Finish.
  7. Paste in the code above and run!
  8. If it asks you if you want to configure Classpaths, select Yes.

That should cover everything for your Hello World of Java, Scala and Ruby. If you have any issues, please ask and I will be happy to help.