10

I was able to get all three running a Hello World in a "Scala Project" with a .java file as my main. The problem is that it is pulling from a "Java Project" I am not using, though I have the JRE System Library in my "Scala Project". Here is the code as to what I am doing to help understand...

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!"); 
    }
}

ScallaHello.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!")
}

End results are...

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!

I have the jars organized as the Scala Library, JRE Library and Referenced Libraries for the JRuby Jar. I also have the same in the "Java Project" that I don't want to use. If I close that project, this project, "Scala Project" fails to run. Obviously this is not an important project, but I am wanting to better understand how this works.

Shane
  • 1,629
  • 3
  • 23
  • 50
  • Ok, I have got it working in one single project, but still trying to figure out how the paths work. Same code obviously, but now trying to optimize. – Shane Jan 22 '12 at 23:32
  • 2
    Some obvious questions: Did you have a dependency on the other project at one point in your Scala project? And if so, did you try doing a clean? – Ed Staub Feb 15 '12 at 01:34
  • 1
    What do you mean fails to run? What error are you getting? Also, if you've made progress, can you edit the question and explain what you did and what is your current issue? – Alex Florescu Feb 17 '12 at 16:20
  • @EdStaub See answer, I have elaborated on the project. – Shane Feb 20 '12 at 20:00
  • @anothem See answer, I have elaborated on the project. – Shane Feb 20 '12 at 20:01

3 Answers3

3

There should be no problems using Java code inside of a Scala project. You must have inadvertently declared a dependency from the scala project to the Java project, but without more information, I can't tell.

Please attach your .classpath and .project files for your scala project.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
  • It was exactly that, I have included a more detailed tutorial on this project and duplicated my build on another computer for this. All is working well. – Shane Feb 19 '12 at 08:48
1

I ended up getting the issue corrected within the first week. This involved a dependency issue. I built a new project and it worked. This question was inactive for a while until om-nom-nom offered +50 for the answer. I am sorry if anyone felt there was still an answer needed. To make this answer resourceful to others, I have started a blog at http://shaneofalltrades.com/java.html with details on full set-up. Here is copy of that tutorial...

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.
  3. After pasting the link and naming your saved location, you should see the items… 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.
  4. After Eclipse restarts, it should ask you if you want to check if plug-in settings are correct, select yes.
  5. JDT Weaving should be enabled. Select recommended default settings and select OK.
  6. Now you should be able to select new project File>>New>Project… then select Scala Wizard>>Scala Project. Name your project then select next.

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...
  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.
  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…
  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.

Shane
  • 1,629
  • 3
  • 23
  • 50
  • 1
    I just thought that it would be nicer to get precise answer if somebody else will stumble upon same problem – om-nom-nom Feb 19 '12 at 00:24
  • @om-nom-nom I did not think it was a bad thing, I just got a few questions asking for more explanation and wanted them to know I was not pushing for an answer myself. Either way, I added to my answer details on my full process, hopefully that will cover many questions and help those who are looking to get started. – Shane Feb 19 '12 at 17:37
  • Can we assume the same thing should be possible (and fairly easy) in other IDEs like JetBrains and NetBeans? – iconoclast Aug 02 '13 at 14:05
1

--- Edited as this answer is a dead end. ---

I'm leaving this posted so others don't attempt to go down this road. That said, it does not deserve any more up votes (but don't down vote it into oblivion either)

The Facets concept is the right concept, yet at this time Eclipse doesn't support core IDE components outside of web development in a manner that complements Facets.

--- Original post follows ---

Convert (or create) the project into a facets project. With facets you can add to (or remove) aspects of the default project nature. In your case, I would guess you would want to add JRuby, Scala, (and others) to a standard Java project.

--- Additional info after request ---

enter image description here Basically, you open up the project properties, then you select "Project Facets" from the left hand side, and select "convert project to facets form". Doing this will "unbundle" the facets of your "project nature" and allow you to add and remove individual project plugins. Using such techniques you may be able to add in your Scala / JRuby / etc additional helpers.

I've mostly done this to tweak the web services side of the stack, so your mileage may vary. However, if it doesn't work as advertised, this is the correct way to add in such functionality, so don't feel shy about posting a bug.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • I have not heard of this, but it sounds like it might be a better option than creating a Scala Project with a Java class / main to start the project. Can you elaborate more on what this is and it's advantages? – Shane Feb 18 '12 at 22:48
  • Edited to better illustrate Eclipse's facets configuration. – Edwin Buck Feb 18 '12 at 23:16
  • 1
    Unfortunately, this solution will not work. Faceted projects are a part of WTP and do not have anything to do with the various language IDEs. So, it doesn't make sense to add a Scala or a JRuby nature to your project (it's not even possible). – Andrew Eisenberg Feb 19 '12 at 02:37
  • @AndrewEisenberg Sorry to hear that, it's too bad. Hopefully they will extend the Facets concept into the regular language IDEs someday. – Edwin Buck Feb 19 '12 at 03:12
  • 1
    @Shane If eclipse ever extends the Facets method of project definition into the IDE space (currently it only supports web applications), then one could mix-and-match parts of the mainline eclipse projects. – Edwin Buck Feb 19 '12 at 17:13