Using Scala from Java code in Eclipse shows fake compiler errors
Migrated from http://lampsvn.epfl.ch/trac/scala/ticket/344
Reporter gcv
I'm attaching two small sample projects, one in Scala and one in Java. The Java project has a Test class which calls out to Scala's TestClass. It compiles and runs, but the Eclipse editor puts red error markers next to the import and the instantiation of the Scala class. Screenshot attached. I tried cleaning the project, closing the project, and restarting Eclipse. Same behavior. Tried Mac OS Eclipse 3.4M1 (Leopard 10.5.1, Apple JVM 1.5.0_13) and Windows Eclipse 3.3.1.1 (XP, Sun JVM 1.5.0_13). Same behavior. Completely reproducible, just import these two projects into a new workspace with the Scala plugin installed.
Probably for a related same reason, Eclipse autoimport feature does not pick up Scala packages.
Note: Java projects want scala-library.jar explicitly imported. Exporting the Scala runtime from a Scala library doesn't work for some reason. So the Java project will have a build path entry pointing to my installation of the Scala Plugin. You'll have to change this to make the workspace work.
Reporter gcv
I'm attaching two small sample projects, one in Scala and one in Java. The Java project has a Test class which calls out to Scala's TestClass. It compiles and runs, but the Eclipse editor puts red error markers next to the import and the instantiation of the Scala class. Screenshot attached. I tried cleaning the project, closing the project, and restarting Eclipse. Same behavior. Tried Mac OS Eclipse 3.4M1 (Leopard 10.5.1, Apple JVM 1.5.0_13) and Windows Eclipse 3.3.1.1 (XP, Sun JVM 1.5.0_13). Same behavior. Completely reproducible, just import these two projects into a new workspace with the Scala plugin installed.
Probably for a related same reason, Eclipse autoimport feature does not pick up Scala packages.
Note: Java projects want scala-library.jar explicitly imported. Exporting the Scala runtime from a Scala library doesn't work for some reason. So the Java project will have a build path entry pointing to my installation of the Scala Plugin. You'll have to change this to make the workspace work.
Leave a comment
on 2008-01-06 20:35 *
By tracImporter
on 2008-01-06 20:36 *
By tracImporter
on 2008-04-20 11:33 *
By tracImporter
Trac author: mcdirmid
Did you try using the SCALA_LIB variable? At anyrate, this sounds like the problem we had with Scala inner classes in Java a while back, not an Eclipse plugin bug, bumping this bug back to devteam.
Did you try using the SCALA_LIB variable? At anyrate, this sounds like the problem we had with Scala inner classes in Java a while back, not an Eclipse plugin bug, bumping this bug back to devteam.
There's definitely something a little peculiar about the Scala project the reporter attached.
It appears that as posted the project doesn't build and doesn't report any build errors, instead it copies the .scala files to the output folder, hence the errors in the Java project. Recreating the Scala project from scratch with the latest nightly (ie. using the new Scala project wizard and copying over the sources) appears to fix the problem.
This suggests that the project is misconfigured in some way. A side by side comparison shows that the original .settings probably originated from a fairly old version of the plugin, so it's possible that the issue is with migrating an old project to the most recent version of the plugin.
As a side note, a better way to add the Scala library to the classpath of a Java project is to add the Scala classpath container to the Java project's .classpath, ie., insert the line,
<classpathentry kind="con" path="ch.epfl.lamp.sdt.launching.SCALA_CONTAINER"/> immediately before the entry for the JRE classpath container.
It appears that as posted the project doesn't build and doesn't report any build errors, instead it copies the .scala files to the output folder, hence the errors in the Java project. Recreating the Scala project from scratch with the latest nightly (ie. using the new Scala project wizard and copying over the sources) appears to fix the problem.
This suggests that the project is misconfigured in some way. A side by side comparison shows that the original .settings probably originated from a fairly old version of the plugin, so it's possible that the issue is with migrating an old project to the most recent version of the plugin.
As a side note, a better way to add the Scala library to the classpath of a Java project is to add the Scala classpath container to the Java project's .classpath, ie., insert the line,
<classpathentry kind="con" path="ch.epfl.lamp.sdt.launching.SCALA_CONTAINER"/> immediately before the entry for the JRE classpath container.
Trac author: Martin
CC Change: martins@…
I'm someone else who's having this problem. I'm using the latest nightly available as of the time I post this ticket. I had already used the 'better way' suggested above, of adding the Scala project to the Java project's build path. I'm attaching a screenshot and the two problem projects (one java, one scala). I cannot seem to reference the Scala class from the Java project.
Thanks!
CC Change: martins@…
I'm someone else who's having this problem. I'm using the latest nightly available as of the time I post this ticket. I had already used the 'better way' suggested above, of adding the Scala project to the Java project's build path. I'm attaching a screenshot and the two problem projects (one java, one scala). I cannot seem to reference the Scala class from the Java project.
Thanks!
on 2008-08-09 16:54 *
By tracImporter
on 2008-08-09 16:54 *
By tracImporter
on 2008-08-09 16:57 *
By tracImporter
Trac author: Martin
Also, I wanted to note that I am getting .class files from the Scala project, unlike the first poster. (even trying to build directly against those class files seems to fail from inside Eclipse, though disregard that, as I really would prefer to get it working the 'right' way.)
Also, I wanted to note that I am getting .class files from the Scala project, unlike the first poster. (even trying to build directly against those class files seems to fail from inside Eclipse, though disregard that, as I really would prefer to get it working the 'right' way.)
Those two projects work for me if I configure the Java builder for the Scala project correctly. To do this, right context menu click on the Scala project in the Package explorer, then Properties => Java Compiler => Building, then enable project specific settings, followed by disabling "Scrub output folders when cleaning projects" and add *.scala as a filtered resource.
These settings should have been selected if the Scala project was created using the New Scala Project wizard but won't have been if the project was created by hand.
These settings should have been selected if the Scala project was created using the New Scala Project wizard but won't have been if the project was created by hand.
on 2008-08-11 11:42 *
By tracImporter
Trac author: Martin
Hmmm... I repeatedly used the New Scala Project wizard in order to make sure I wasn't repeating the mistakes of the previous ticket poster. I'll give it a try again.
Hmmm... I repeatedly used the New Scala Project wizard in order to make sure I wasn't repeating the mistakes of the previous ticket poster. I'll give it a try again.
on 2008-08-11 21:52 *
By tracImporter
Trac author: Martin
Seems to work now. I had to close and reopen the projects and suddenly things worked. I didn't do anything wonky on the first go-round, so I'm still surprised I got stuck, but thanks again!
Seems to work now. I had to close and reopen the projects and suddenly things worked. I didn't do anything wonky on the first go-round, so I'm still surprised I got stuck, but thanks again!
on 2009-04-15 08:33 *
By tracImporter
Closed As: worksforme