How to debug the error "Class file needed by X is missing. reference value Y of package Z refers to nonexisting symbol" in Scala? - scala

I am just getting started with Scala since last week and I am having this problem that is driving me crazy.
I have some code that I want to migrate fully from Java to Scala (except libraries) but I have hit a block where I can't now proceed because of this mysterious error.
Let's say that I have a class that I have defined in org.domain.subdomain.MyClass
I have a class that seems to have no compilation problems (as suggested by Intellij) but when I build my project's modules, I would get the following
Class file needed by MyClass is missing
reference value subdomain of package org.domain refers to nonexisting symbol.
I am certain (or almost?) that I have got the classpath right as the projects were compiling before I started rewriting the code in Scala.
I have not been able to get it to compile with the verbose option on. I am using FSC compiler as the project compiler. Intellij does not show anything with verbose option!
What is going on here?
How can I solve this?

I just had this problem, and reimporting the project in IntelliJ worked. I removed .idea directory and .iml file, then reimported the project as maven.
This is an IntelliJ issue - outside maven builds worked fine.

Your classpath is incomplete. The reference to org.domain.subdomain appears in a classfile referenced by one of our sources (as indicated by the error message).
If you are rewriting Java code, you may need add more of your transitive dependencies in each module's classpath. The Scala compiler is more eager when it comes to type-checking and in some situations it needs a larger classpath.

I know, the question is more than one year old, but maybe it helps someone.
I just had the same issue. One of my libraries used an newer version of org.scala-lang:scala-library than the main project. After the library update everything works fine.

Related

"Error: Could not find or load main class" when trying to launch Run Configuration in Intellij

In a project that was originally set up for python I do have both python and java SDK's defined:
I am attempting to run a scala program: and the src directory is correctly marked as sources:
The class itself does have a main :
But the Intellij does not provide assist for setting up a Run Configuration - which should have been available by right click/context menu. So I set it up manually:
But when trying to actually run the program it is not successful saying Error: Could not find or load main class com.blazedb.algos.CourseraAlgos:
Update: for reference purposes here is a Run Configuration from a similar project that does work. I do not see any structural differences between the two.
Any ideas why Intellij does not recognize the file as a scala class?
In addition to the steps shown above I had also tried:
adding a new scala-specific module
nuking and recreating the IJ project
These did not resolve the issue. It turns out the problem is that there were no pom.xml in this project. The resulting behavior by Intellij made it difficult to trace down the root cause: there was no message like
You need a pom.xml or a build.sbt to proceed
Apparently stray scala classes (dissociated from a formal build) are only haphazardly supported in Intellij.
So finally the answer is to create a new scala based project. Adding scala back to a project built for python is at the least unreliable and maybe not possible at all.

Scala in IntelliJ error

I just downloaded the plug in for Scala in IntelliJ and have created a project but now have various errors I read that the problem can be that I am missing a library. But when I try and go to project structure -> dependencies to add a library I have no clue where in the files to look for a library.
The errors are really simple but I can't seem to figure it out.
Any suggestions would be helpful :)
Here's everything you need to properly set up Scala plugin in Intellij. Furthermore your code has several errors:
Are you sure you created a Scala project (when you did File->New->Project)? This looks to me like a Java project? That class file looks to me like a Java class not a Scala class (that's why you're getting compilation errors on def but not public class).
1) value is not defined anywhere, of course it will throw a compilation error
2) classes in Scala are by default public, you do not (cannot) mark them as such
You can save yourself a lot of trouble by creating a simple SBT project that specifies all of your dependencies, etc. and then just pointing the IDE at that. Then when you change build.sbt, IntelliJ IDEA will notice that and update itself automatically. Plus, your build.sbt gets checked into your code base so that anyone you are collaborating with sees your changes to the dependencies. And the project can be built in batch mode using sbt compile and friends.
The following page talks about IntelliJ IDEA's "SBT Import" feature:
https://confluence.jetbrains.com/display/IntelliJIDEA/Getting+Started+with+SBT
Scala source files end in .scala, not .java. Try renaming Counter.java to Counter.scala. This should improve things a lot.

Intellij compile failures: "is already defined as"

I've got a scala project that compiles, runs and tests fine when using SBT from the command line. However, when building the project in intellij, it seems every class in the project has this error in the event log, causing the build to fail:
SendCommandToService is already defined as case class SendCommandToService
case class SendCommandToService(service: String, commandName: String, keys: Array[String], values: Array[String])
^
For me, the reason is that both myproject/src and myproject/src/main/scala are marked as Source. So IntelliJ failed to build myproject/src/main/scala due to above errors. Unmark Source from myproject/src (in IntelliJ, File->Project structure, select myproject Module, select src folder in Sources Tab, remove it from Source in the "Add Content Root" pane) solved the problem.
It means there are two compiled classes with identical package and class name found in your classpath. One compiled by sbt, one compiled by IntelliJ.
One of the following should be able to solve the issue:
try to generate IntelliJ .iml file with sbt-idea rather than import directly.
sbt clean before click Build -> Rebuild in IntelliJ
when rebuilding with IntelliJ, make sure sbt is not running
I ran into this issue today on IntelliJ 2021.2.1 and according to this page it's some issue with IntelliJ's incremental compiler for Scala, so the solution is to change the "Incrementality Type" from "IDEA" to "Zinc" in Preferences -> Build, Execution, Deployment -> Compiler -> Scala Compiler
For me, the solution was to double check the source folders in each of my modules in IntelliJ.
File > Project Structure > Modules and for each module, double check that the Source Folders only contain your intended folders, e.g. src/main/scala, and do not contain any generated sources (e.g. target/scala-2.12/src_managed/main.
I had the same problem and #Max is right, there is a conflict with the compiled classes, but the solution provided didn't work for me. It turns out that I was using sbt-idea to generate the IDEA project structure as a workaround of an Intellij IDEA 14 + scala plugin bug on the SBT import, that is not fixed yet at the time I write this.
In order to fix it, I had to remove src_managed/main/controller that was in conflict with src_managed/main in the Module settings because of an sbt-idea bug. So double-check your module source folders and make sure you don't have subfolders in conflict with a parent folder already declared as source.
You need to change "Settings -> Build,Execution,Deployment -> Scala Compiler -> Compile order" from "Mixed" to "Java then Scala".
If you have compile the project previous, you should first run "sbt clean".
I had a similar issue repeatedly both within Idea and without: plain SBT.
It turned out that CVS stores copies of some *.scala files in subdirectory CVS/Base, which SBT apparently tries to compile. The problem went away when I deleted the CVS subdirectories.
Problem is caused by duplicated line in .idea/modules/<your_project_name>.iml file. Check if you do not have duplicated <source_folder> tag.
In my case I had the same problem with all classes in src/test/scala path, and after removal duplicated tag for this path, project build fine.
Do you have any other files in your project with an SendCommandToService in them?
You could try renaming it to something else, see if that works
If you want to keep the same names, you can put them into separate packages.
Or have them in different encapsulating objects
object traitdemo{
object Ex1{
...
}
}
object otherdemo{
object Ex1 {
...
}
}
that will work even in the same file
In my case problem solved by change ScalaTest template configuration in Idea. I select use sbt, disable print info, remove build before launch.
I like to use SBT for clean/package/test on specific module. I also use mixed Java/Scala classes in test (but I replace compile order to Java than Scala).
At least now I can test from IDE withot this error.
PS: Now I disable use sbt. My tests work fine (but I'm not sure, that they will work).
PPS: New tests not runs before compilation. It is disadvantage of removing build (and, maybe, of disabling use sbt). But this extra build cause problem with dublication, as I think
File -> Invalid Caches/Restart worked for me. All other answers here did not.
After the sbt compile I had to mark the folder as Generated Sources Root because I needed those files for compilation.
I'll just add mine to the list in case anyone else made this beginner mistake: I temporarily "saved my progress" by doing cp Foo.scala Foo-save.scala, forgetting that sbt would try to compile all the .scala files in the directory.
(I don't know, I guess I was thinking of programming languages where any file not explicitly included was ignored ...)
Of course, since both the main file and the "temporary backup" file defined the same classes ... yeah.
I had the same error message and it turned out that IntelliJ for some reason created duplicate copies of some existing source files. For example I had a file Attribute.scala that was tracked with git and then there was an untracked file Atrribute 2.scala with the same contents in the same directory (which I never created). This was of course a problem, because the compiler considers them part of the project, hence the duplicate object definition error.
I am not 100% sure when this happened (I suspect it was during git rebase). So, if you run into this problem again, it's also worth checking with git status if you have some untracked files which duplicate contents of tracked files.
Remove the untracked files and the problem is solved.
Kudos to this question thread, it helped me to solve this issue.
My case is a project with the mix of Scala, Java and Avro schemas.
IDE: IntelliJ IDEA 2022.1.3
How I solved it step by step (in IntelliJ):
File -> Project Structure
Project Settings -> Modules
Now we need to fix the "source" files. I searched for target/scala-2.12/src_managed in each module and marked it like “Source directory” (blue colour).
Unmarked all other paths like target/scala-2.12/* in all modules (in my case it was target/scala-2.12/src_managed/main/compiled_avro). As an example from my project, I left only these sources in one of the modules: target/scala-2.12/src_managed , src/main/scala.
Save the changes and rebuild the project.
ADDITION: Oh, and looks like sometimes this error occurs when you compile your scala project (with avro files) outside of the IntelliJ. For example when you build a project using sbt externally and then run some tests using the IntelliJ -> class already defined error occurs (or is not a member of package error). In such case: you need to run sbt clean compile externally first and go to step #1.
this happen when you incorrectly src/main/any other folder as Sources Root. Please check if you have any such cases. If yes, then Unmark those by righclick on it. then clic one level above of your groupId starting. Lets say your package is com.company.test and com may comes under java or scala, then right click on that(java/scala) then Mark as Sources Root.
In my case, the problem was the protobuf Idea plugin:
Remove the idea protbuf plugin.
Close Idea
Remove all folders related with idea (.idea and .idea_modules)
Open Idea and Import the project again.

Using Joda Time in Scala (scalaj-time) with IntelliJ IDEA

EDIT:
Version information, as of 01/27/2013:
Scala 2.10.0
IntelliJ IDEA Leda 123.139 (EAP)
Scala Plugin version 0.7.108 (Nightly Build)
JDK 7u11
Joda Time 2.1
ScalaJ-Time 2.9.1-0.6
I have given up on trying to work with SBT, as I didn't see any progress with it - I'm not sure how to combine IntelliJ and SBT, and after hours of trying, I had ended up where I'd first begun.
My JodaTime library contains the Scalaj-Time jar:
I have tried almost every combination of settings, reaching from Joint compilation (first Scala, then Java and vv), external build, over to lifting my JodaTime library over Scala library in the dependencies (yes, I've even done that), and invalidating caches. All I get is a bunch of errors - no matter if I "Make" the project, "Run" the current file or "Compile" it - that are produced randomly. And, sometimes, it works. Yes, as idiotic as it sounds, the behaviour is that undefined. I'm not sure as to what causes this. I have counted 3 different error messages that appear, I will upload them to pastie.org, otherwise it will take too much space: http://pastie.org/pastes/5887847/text.
What I have not tried: Going back to earlier releases of any of the apps/libraries vide supra, getting deep into SBT (I figured it's not worth it).
What do I expect from this thread? I expect that someone provide a solid solution to this problem which, in the best case, will not include any mention of SBT (optional).
Also, on a side note: Why does Joda-Time ship their library in a broken format? Is there any secret behind this that everyone should know about? What was the problem with including FromString into the release version? (I still think that's the cause of it all)
Regards,
Danyel
ORIGINAL POST: (you can ignore this)
I've got the latest build of IntelliJ (123.139), Scala 2.10.0, and hadn't encountered any problems thus far.
Instead of using Java time library, I was eager to use Joda Time, and seeing that there are a bunch of implicits to use in Scala, I wanted to try that out! I added all the libraries, but implicits wouldn't be recognized. When I tried to compile it via IDEA Popup Menu, following errors appeared:
error while loading DateTime, class file '~\workspace\Libraries\JodaTime\joda-time-2.1.jar(org/joda/time/DateTime.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 10 at byte 42)
error while loading DateTimeZone, class file '~\workspace\Libraries\JodaTime\joda-time-2.1.jar(org/joda/time/DateTimeZone.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 7 at byte 44)
error while loading AbstractInstant, class file '~\workspace\Libraries\JodaTime\joda-time-2.1.jar(org/joda/time/base/AbstractInstant.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 10 at byte 10)
I went to the source of those DateTime and noticed that org.joda.convert.FromString; was imported, but the resource didn't exist! I guessed it was because of that why Scala wouldn't compile. So I googled for what I should do. And everywhere it said "SBT, SBT!!", and I was trying to find out what this SBT was, so I installed it: Version 0.12.2, Manual Installation (I'm using Windows 7) from http://www.scala-sbt.org/. I wanted it to run with my IntelliJ so I went to ~\.sbt\plugins, and created a file build.sbt with the line addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0") and downloaded the zip from https://github.com/mpeltonen/sbt-idea, extracted it, and went to that very folder with CMD, entered sbt and gen-idea within sbt and it did a lot of things and all seemed to work out. So I went to my project that I wanted to run Joda Time with, and first of all, it seemed very hard to include Scala 2.10.0 to SBT. I created a file <PROJECT>\build.sbt and filled it with these lines:
name := "Project"
version := "1.0"
scalaHome := Some(file("C:/Program Files (x86)/scala"))
scalaVersion := "2.10.0"
Then, to include the scalaj-time, I went to <PROJECT>\project\project\ and created build.sbt with the line libraryDependencies += "org.scalaj" %% "scalaj-time" % "0.6".
Lastly, I went to <PROJECT> with CMD, entered "sbt", it (down)loaded a lot of files, and it seemed to work out, then I entered gen-idea, and everything was fine.
I opened IntelliJ and I could open the project easily. When I opened Project Structure, I went to Modules, and there were two Modules: Project and Project-build. I ignored the Project-build one, clicked on the Project module, and there were a lot of Source Folders and Test Folders that were not existent: src/main/scala etc., I removed them all and added my src as Source Folder.
I noticed that Joda-Time was not included in External libraries (btw I use Joda-Time 2.1 and Scalaj-time 0.6), so I had to add them myselves. The joda-time library is a .jar while the scalaj-time is a folder, but that should be no problem, right? I could run a simple println( DateTime.now ) instruction (I don't know if it was possible before; didn't try), but a + 2.months was still not possible. Here is a screenshot so you get the idea:
I think I explained in very detail how I proceeded and I hope that you can find something that I did wrong. I've been working for many hours to resolve this, but in the end, I landed where I was in the beginning...
Thank you for your time!
Regards!
How to import FromString for joda-time?
This worked like a charm (for now, who knows how things will ensue). I am so mad that I didn't think of this in the first place... Grrrrr.
EDIT:
This answer is invalid. See the EDIT in the main post. While this works SOMETIMES, it is still heavily undefined as to WHEN it works. When I create a new project, it varies from working to not-working (not compiling, printing random errors), but in another larger project, it had the same undefined behaviour, except now (for whatever reason) it doesn't work, PERIOD. (On my newly created project it still varies)
ORIGINAL POST:
So, after another 10 minutes of trying and googling, I found a .JAR for Scalaj-time (0.6).
I was like "Well, what harm can it bring?", downloaded it, and included it instead of the folder.
Tadaaa, everything worked... All I needed, too, was only these lines:
object Test extends App {
import org.scala_tools.time.Imports._
println( DateTime.now + 2.months )
}
No SBT, nothing, no other imports whatsoever. You just download scalaj_time from http://mvnrepository.com/artifact/org.scalaj/scalaj-time_2.9.1 (Version 0.6, as of now) and Joda Time (V 2.1), make a library ScalajTime that includes 2+ jars, and add it to your dependencies. All you need is that one import statement and you are ready to go.
Maybe the main problem was that I cannot import SOURCE Files from libraries? Because the source folder I imported seemed to have no impact whatsoever. I don't know how I "ignored" that fact but it seemed stupid to assume.
So, now it works.
Regards.

How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?

I recently gave up trying to use Scala in Eclipse (basic stuff like completion doesn't work). So now I'm trying IntelliJ. I'm not getting very far.
I've been able to edit programs (within syntax highlighting and completion... yay!). But I'm unable to run even the simplest "Hello World". This was the original error:
Scala signature Predef has wrong version
Expected 5.0
found: 4.1 in .... scala-library.jar
But that was yesterday with IDEA 9.0.1. See below...
UPDATE
Today I uninstalled IntelliJ 9.0.1, and installed 9.0.2 Early Availability, with the 4/14 stable version of the Scala plug-in.
Then I setup a project from scratch through the wizards:
new project from scratch
JDK is 1.6.u20
accept the default (project) instead of global / module
accept the download of Scala 2.8.0beta1 into project's lib folder
Created a new class:
object hello {
def main(args: Array[String]) {
println("hello: " + args);
}
}
For my efforts, I now have a brand-new error :)
Here it is:
Scalac internal error: class java.lang.ClassNotFoundException [java.net.URLClassLoader$1.run(URLClassLoader.java:202), java.security.AccessController.doPrivileged(Native Method), java.net.URLClassLoader.findClass(URLClassLoader.java:190), java.lang.ClassLoader.loadClass(ClassLoader.java:307), sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301), java.lang.ClassLoader.loadClass(ClassLoader.java:248), java.lang.Class.forName0(Native Method), java.lang.Class.forName(Class.java:169), org.jetbrains.plugins.scala.compiler.rt.ScalacRunner.main(ScalacRunner.java:72)]
FINAL UPDATE
I uninstalled 9.0.2 EA and reinstalled 9.0.1, but this time went with the 2.7.3 version of Scala rather than the default 2.7.6, because 2.7.3 is the one shown in the screen-shots at the IntelliJ website (I guess the screen-shots prove that they actually tested this version!). Now everything works!!!
I have encountered the same scalac error when trying to run a Scala project in Intellij Idea 9.0.2 and I've managed to find a solution by chance :). These are the steps I took in creating the project and running it.
I have created a Scala project in Intellij Idea 9.0.2 final (it was released today). I have installed the Scala plugin, restarted the IDE and created a new Scala project (with the name "TestScala") with scala-2.8.0.Beta1 as project library. Once the project is created and the scala libraries downloaded, I have created a Test.scala file with the following content:
object Test {
def main(args:Array[String]){
println("hello")
}
}
After that, I created a launch configuration ("Edit Configurations"), choosing the "Application" template. I set as main class Test and choose the project name ("TestScala") in the "Use classpath and JDK of module" combo box. When I run the configuration I get the same error as you reported ("Scalac internal error: class java.lang.ClassNotFoundException") .
Now comes the freaky part :). I right click on the project, choose "Module Settings", have a look on all settings but I don't change anything . Click "apply" and "ok", try to run configuration again and it works :) .
I use Intellij Idea 9.0.2 the final release (build 95-66); Ubuntu 9.10 and JDK 1.6.0_18. I also have to mention that I had a JDK configured in Intellij, otherwise there is an extra step to configure it.
UPDATE:
When checking the setting of the module, one needs to click on the Module->Scala and Facets->Scala (expand it and click on Scala(ProjectName)) . Both of these settings are about the scala compiler and scala library location. I would guess these values are not properly set when the project is created but are saved once the user touches them and saves the settings.
To answer your question, it's difficult to get a working IDE for Scala for two reasons:
(a) Scala is only just beginning to reach a wide audience and
(b) due to (a), there is no business case for spending time on a Scala IDE.
Also, if you are old enough to cast your mind back and young enough to still remember, you would know that for the first five or more years of Java, we were stuck with okay-ish tools like JBuilder that did little more than compile your code when you said so - no error highlighting, no auto-importing, and the word refactoring didn't even exist. If you want to pioneer, you need to be prepared to cut some of the road yourself, or at least bush-bash.
I know it won't help you, but I have successfully used IDEA for Scala on Linux, Mac and Windows. I typically have the Scala SDK installed somewhere locally and point IDEA at that rather than using the 'download' option.
Presently, I am mostly using an EAP version of IDEA 9 on Mac OS X with Scala 2.8.0.Beta1-RC5 and it's working well (except that fsc doesn't seem to worked with mixed sources).
You could try your luck over at the IDEA Scala Plugin Discussion Forum, though I haven't had a great lot of responses to my own postings there.
Installing the plug-in is prerequisite one.
The next thing you should do is define a library (global or project-specific; I use global) that holds the Scala library and compiler JAR files (at a minimum, that's scala-compiler.jar and scala-library.jar). Adding source JARs and a documentation JAR or URLs is a good idea, too. Then make this library a dependency of any modules in your project that include Scala code.
Lastly, find the Scala facets in those modules and de-select both check-boxes there.
I just did a fresh install and had exactly this same problem myself.
It turned out that, because I had created the file in the root package, IDEA had added a package statement at the top with naming a package. I assume that this then got compiled as "package object Main" - valid syntax in 2.8? Anyway, I deleted the line that said package and it all worked fine.
I had the same problem yesterday while trying to set it up. Solution is pretty simple, you just have to set scala somewhere in project settings.
You are mixing code compiled with two different Scala versions.
I use Netbeans to write scala programs. So far it works very well with my codes. You can try the plugin here: http://wiki.netbeans.org/Scala68v1.
I was getting this error and also had to right click on the project and "Open Module Settings". However, it was more than just hitting apply. I had to make sure that my Content Root was correct for each project. For some reason, there were some incorrect Source and Test Folders.
My project uses maven as the main build tool and importing the project into Intellij is probably what created these incorrect settings.
I had similar problem, following this blog post instructions solved the problem for me