Calling Scala from Java

Hi people. I know this is probably far to early to request features but at the moment you don't seem to be able to call scala classes from java (you can of course call java classes from scala).

I get a :

Cannot find symbol class TestScala

when I build, even though the IDE doesn't flag it up as a problem. The scala source is there and the class file is in the output folder.

I know the eclipse plugin can do this (I think the scala compiler was changed recently to facilitate this) so please, if you only add one feature to the plugin in the next year let it be this one!

I want to use scala but I need to be able to call it from the massive java code base.....

Thanks, Paul.

17 comments
Comment actions Permalink

Paul,
there are 2 aspects here: one to make IDE happily navigate to scala source, another add scala classes to classpath when compiling java (or vice versa).
The first one is really solved for classes as you noted (though not for methods and other members that should be visible from java).
The second one requires some form of 'joint compiler' similar to the one that was done for groovy. This is indeed not implemented, but should be in the future.

0
Comment actions Permalink

Hi Eugene, thanks for the reply.

However, I was under the impression that a 'joint compiler' had already been sorted (or at least worked around).

In eclipse, when you compile a project that contains scala files that call java as well as java files that call scala, it first compiles the scala files and then compiles the java files.

This is made possible because the scala compiler can look at the java source code rather than class files, and so if a method exists in java source, the scala compiler is happy enough and creates the scala class files. Then the java compiler can obviously just compile against the scala class files.

So I think if you use the latest version of the scala compiler you should get this behaviour for free. It doesn't seem to be working though when I test this with the latest version of scala , idea and scala plugin. I can't see what idea does when I do a "rebuild project". I presume it is compiling the java first and then the scala. If it were to do it the other way round then it might just work....

At least I think this is how all this works. I'll find out more information and report back.

Thanks, Paul.

0
Comment actions Permalink

It's good idea to reverse compiling order (scala first), but we are going to work with compiler (and also debugger) in august. I think it can be 'joint compiler', but if scala compiler can do things as you say, so you are right, it will be better to use this and don't rewrite good functionality.

0
Comment actions Permalink

Hm, if this is true, then it must be Eclipse specific: I don't believe scalac does also java parsing to look at java code. Any details would be more than welcome, thank you.

0
Comment actions Permalink

Hi again.

I can confirm that scalac parses java source.

In my two test classes (java calling scala and scala calling java) idea can't build it.

However, if I do it on the command line like:

$ ~/programs/scala-2.7.1.r15535-b20080712010056/bin/scalac -sourcepath . *.scala *.java

It compiles the scala class fine. I then compile the java class as normal (with the new scala class file on the classpath) and it works as expected.

This is not eclipse specific.

So if idea would just compile the scala first when doing a build everything should be OK! (and it would be great if this could happen soon:)

Note that you need the latest version of scala from the nightly builds for this to work (see the version I was using above).

Thanks, Paul.

0
Comment actions Permalink

There's an experimental support for mixed compilation in Scala: http://community.livejournal.com/levin_matveev/18265.html (in Russian, sorry).

0
Comment actions Permalink

This is wonderful news indeed, thank you. Once scala team pushes the new version with this functionality enabled, it will be not a big change to the plugin to enable this. Though we still have to be backward compatible and not include .java files for earlier versions of scalac.

0
Comment actions Permalink

Guys, did you see a very interesting related article and discussion here: The Need for a Common Compiler Framework ?

0
Comment actions Permalink

Thanks for excellent link.

0
Comment actions Permalink

Hi Eugene. Good news indeed. Even better is that version 2.7.2 (which has this 'joint compiler' in it) is going to be released before the end of August.

It would be great if a new release of the plugin that exploited this behaviour could be released at roughly the same time :o)

Thanks, Paul.

0
Comment actions Permalink

Great news, Paul. That's we need.

0
Comment actions Permalink

Not that new to us, but thanks anyway:)

0
Comment actions Permalink

Guys, tell me immediately when I start to bother you with too much of newbie activity, and I will stop :)
You probably already saw a post by Martin Odersky there regarding Scala 2.7.2 and mixing it with Java sources?

0
Comment actions Permalink

Vladimir, we are indeed subscribed to scala mailing list and read it quite thoroughly. Thank you for your help!

0
Comment actions Permalink

Hi again. I'm sure you know but I'll tell you anyway :o)

The Scala 2.7.2 RC1 is out!

Enjoy, Paul.

0
Comment actions Permalink

BTW, thanks, Paul.
We modified our compiler wrapper implementation according scala 2.7.2 and now we're waiting for next IDEA eap to publish new plugin version.

Ilya

0

Please sign in to leave a comment.