Scala in IntelliJ error - scala

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.

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.

Could not find or load main class in scala in intellij IDE

I have searched for this error, but the answers were for Java,
but my case is Scala. I am trying to run this project in IntelliJ IDE
sentimenAnalysis, but it throws an error. This is also the structure of the project. Class Not found
Update 1 According to answers, Adding $ at the end of the name of search class
Update 2 after adding sbt task:
Update 3
My problem was resolved by importing the project, instead of getting it directly from GitHub, probably the manual configuration that Mike Allen said could resolve the problem, but I couldn't successfully apply that.
My problem was resolved when I marked "src" folder as sources root.
Right-click on (folder) src -> Mark Directory as -> Sources Root
Probably you imported project somehow wrong. I see scala folder is not highlighted as sources.
Usually, you don't even need to setup Run configuration manually for Scala SBT project. Do you have Scala and SBT Plugins in your IDE?
You should open SBT projects through "Import project" and choose "build.sbt" file. I would also recommend enabling auto-import to install all of the dependicies.
That's how it look for me:
If you still wanna make it manually your configuration should work. That's my Run configuration that works:
You will get this error if you tried to open the project and imported it incorrectly. I would open the project like this in Intellij:
File>New> Project from Existing Source>(select) Import project from external model>
(select)sbt
click Next>Finish
To fix this issue in my project I invalidated caches and restarted:
In my case the object in the object MyObject extends App was nested, if you have it unested meaning in your scala file it's not under any other object it made it work.
Netsted caused this error in intellij:
object External {
object MyMain extends App // Could not find or load main class in scala in intellij IDE
}
While the below unested worked:
object External { }
object MyMain extends App // Worked!
Hi I solved this problem by defining the class in src package under main and by setting up the configuration as default.
Create a new project and make sure SBT is proper loaded along with Scala Library. This happens with many times, bad internet connection or failure of Scala library/SBT loading might be some of the reasons.
Best of Luck for Next Project, Happy Developers & Coders.
Had a similar problem with latest Intellij IDEA build (2022.1.1) and scala 3.1.2 - both sbt and Intellij scala projects. For me the solution was to use non-ascii path.
#SeriousDron answer helped me with my problem. You need to make sure you have Scala language installed in IntelliJ. You can do this by going to Settings > Plugins > search for "Scala". Now, it will automatically pick up your .sbt file and project.
I met the same issue, please check the whole execute command and check the configuration, make sure the compiled files path is correctly configured.
refer to my screenshot:

Intellij Idea setup for Scala, clarification needed

Using IntelliJIdea 12, i downloaded the Scala plugin and created a HelloWorld application.
The following import seems odd. Why does IDEA not recognize this to be a Scala Application? Is my setup incomplete?
I checked to see where does my compiler points to and got this. Don't really see a way to navigate IDEA to my scala installation.
Please advise.
When creating a new Scala project from the wizard IDEA can download and configure everything for you:
Settings can be changed later in the Scala Facet:
and in Compiler:
Libraries configuration:
Code and imports completion is available from the Scala library:
You need to add Scala as a framework for your project.
To do this, right click on the top level directory in your project, choose "Add Framework Support..." and select Scala. Then Scala will be added as a "Facet" of your project.
Here is a good page with more info:
IDEA Scala Project Configuration Explained
I got around this by downloading the Scala library from scala-lang.org, unzipping it and then pointing the project to it. It worked fine after that. But of course, I'm an eclipse guy so perhaps someone else might be able to give you a better automated (?) solution. Here's what my project looks like in Module Settings -> Libraries:

How to debug the error "Class file needed by X is missing. reference value Y of package Z refers to nonexisting symbol" in 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.

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