Run Scala 2.9.0 application on IntellijIdea 10 - scala

When I run my Scala hello world application, like
package pack
object App {
def main(args: Array[String]) {
println("Hello, world!")
}
on Idea 10.0.3, I get compile errors
'App' to
D:\prog\java2\scala3\out\production\scala3\pack\App.class
(The filename, directory name, or
volume label syntax is incorrect)
'App' to
D:\prog\java2\scala3\out\production\scala3\pack\App$.class
(The filename, directory name, or
volume label syntax is incorrect)
How can I deal with it?

This is an Intellij bug. See bug SCL-3185 in the jetbrains tracking for pointers to release candidate code fixing it.

Related

Error: Could not find or load main class Main Scala

I've recently installed Scala as a part of my functional programming course and I've encountered a problem: IntelliJ IDEA 2017.2.1 (Java version 9, build 9+181) doesn't run any of my scala code, exitting with
Error: Could not find or load main class Main
This code is an example.
object Main {
def length[A](list:List[A]):Int = {
if (list == Nil) 0
else 1 + length(list.tail)
}
def main(args: Array[String]): Unit = {
length(List[Int](1, 4, 5, 12, -1))
}
}
It's really simple, yet IntelliJ refuses to run it. Windows CMD doesn't even react to a scala command, resulting into
'scala' is not recognized as an internal or external command,
operable program or batch file.
even though I have it installed on my computer. If I call Scala Console inside of IntelliJ everything works fine and compiles as expected. I've tried switching to JDK 1.8 inside of IntelliJ, yet it led to no result.
What could be the problem?
For me it turns out that the src/main was not marked as Sources Root
which causes the following error
...
One of the two will be used. Which one is undefined.
Error: Could not find or load main class Main
Process finished with exit code 1
So of course after I mark the src/main as Sources Root, the Scala Hello World example runs happy again.
Notice the blue color of directory src/main when it's marked as Sources Root
Are you using the little green arrow to run the program from inside of your Main object?
How did you create the program? It could be that your build file SBT configuration of the project is a different Scala version than what's installed on your computer.
It's really simple, yet IntelliJ refuses to run it. Windows CMD
doesn't even react to a scala command, resulting into
'scala' is not recognized as an internal or external command, operable program or batch file.
This means that Scala is not added to your class path in your terminal. Look up how to do that and see if that doesn't help out your IntelliJ problem too.

What is the 8-bit simulator in scala compiler and how to launch it?

I was reading this question and saw:
scalac includes an 8-bit simulator of a fully armed and operational battle station, viewable using the magic key combination CTRL-ALT-F12 during the GenICode compilation phase.
To show what compilation phases scalac has, I used scalac -Xshow-phases.
phase name id description
---------- -- -----------
< ... >
icode 23 generate portable intermediate code
Seems to be the relevant phase.
I compile a Hello-world program
object Hello {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
by scalac Hello.scala and pressed CTRL-ALT-F12 all the way. Nothing happened. And tried scalac -Xprint:icode Hello.scala with CTRL-ALT-F12, nothing special as well.
How can I trigger the simulator? And what does that do?
On scala 2.11.8 and OS X 10.11.5

Compilation error in scala

I'm a newbie in scala. I recently started using netbeans for writing scala programs.I have installed scala version 2.11.0.400 and its plugins for netbeans.When I try to compile my scala code:-
package scalaapplication2
object Main {
def main(args: Array[String]): Unit=
{
println("Hello, world!")
}
}
I get the following compilation error even after setting the path variables:-
Compiling 1 source file to C:\Users\Nitin\Documents\NetBeansProject\ScalaApplication2\build\classes
C:\Users\Nitin\Documents\NetBeansProjects\ScalaApplication2\nbproject\build- impl.xml:423:
The following error occurred while executing this line:
C:\Users\Nitin\Documents\NetBeansProjects\ScalaApplication2\nbproject\build-impl.xml:238: bad option: '-make:transitive'
Why is it so?
Removing '-make:transitive' from the scalac addparams option (Files view > ScalaApplication > nbproject > build-impl.xml) worked for me.

Why does enabling the "Any Exception" break point in IDEA cause a ClassNotFoundException when launching Scala swing apps?

It appears that at least an Application-type run target in IDEA with the Scala plugin and the "Any Exception" special break-point enabled will cause a ClassNotFoundException with the launcher being unable to find the main class or any of the Scala classes, seemingly (I didn't go through every subsequent exception but all the classes I did go through were Scala ones).
Is this just a bug or some Scala behaviour with respect to the Java debugger and the "Any Exception" break-point?
Test code:
package testIDEABug
import swing.{MainFrame, SwingApplication}
import java.awt.Dimension
object TestApp extends SwingApplication {
override def startup(args: Array[String]) {
new MainFrame {
title = "Testing IDEA Bug"
size = new Dimension(400, 300)
}.open()
}
}
On the up side, it did rightfully punish me for trying to do some parts of the app without tests - sigh
I experienced the ClassNotFoundException problem sporadically (not because I was doing anything with breakpoints, though.) My solutions were 1) to delete the Run/Debug configuration and recreate it using a right-click on the main method I wanted to run (there will be a context-menu item to create a temporary build config for that main.) or 2) switch from external build to internal build. But I think the external build is new in IntelliJ IDEA 12, which I don't think was available when you asked this question...

Scala project won't compile in Eclipse; "Could not find the main class."

I have installed Eclipse 3.5.2 and today's Scala plugin from /update-current (that's Scala 2.8 final.) I can compile and run Scala projects consisting of a single singleton object that implements main().
But, if a project contains more classes, I receive the "Could not find the main class" error.
I have tried searching for the solution and I discovered:
Eclipse is correctly looking for the Main$ class, not the Main class
* under Debug Configurations, my main class is correctly identified as mypackage.Main
* my plugin is up to date and recommended for my version of Eclipse
* cleaning, restarting etc. doesn't help.
The same project will compile with scalac.
Thanks for any ideas on how to solve this.
EDIT: MatthieuF suggested I should post the code.
This snippet produces an error. It's not the most idiomatic code, but I wrote it that way to test my environment. I tried it as a single file and as separate files. It DOES work with scalac.
import swing._
class HelloFrame extends Frame {
title = "First program"
contents = new Label("Hello, world!")
}
object Hello {
val frame = new HelloFrame
def main(args : Array[String]) : Unit = {
frame.visible = true
}
}
BUT, if I nest the definition of HelloFrame within Hello, it works. This snippet runs perfectly:
import swing._
object Hello {
class HelloFrame extends Frame {
title = "First program"
contents = new Label("Hello, world!")
}
val frame = new HelloFrame
def main(args : Array[String]) : Unit = {
frame.visible = true
}
}
For me, the problem was that there was a build error (see Problems tab) which was preventing compilation; oops! The reason you see the error is that the run macro proceeds despite the failed compilation step, and attempts to run class files it expects to be there; they don't exist because there was a build error preventing compilation, so it says it can't find Main (not compiled).
Problem goes away when build can complete successfully, i.e. errors are fixed.
I guess, theoretically, there may be more complicated reasons your build is not completing successfully that are not listed in Problems.
One possibility is that you are trying to launch using ctrl-F11, but from a different class.
The Scala Eclipse plugin does not obey the defaults for Java launching. In Preferences->Run/Debug->Launching, there are some options Launch Operation->Always Launch the previously selected application, etc. This currently does not work in the Scala eclipse plugin. To launch a specified main, you need to launch it from the editor for the class.
There has been a bug raised for this. http://scala-ide.assembla.com/spaces/scala-ide/tickets/1000023-scala-launch--does-not-follow-jdt-behaviour
EDIT: This is now (mostly) fixed.
For me it was Eclipse specific problem. I noticed that .class file wasn't built at all. So bin directory doesn't have compiled classes.
When I manually compiled *.scala file using *.sbt and copied it to bin directory it was working as expected.
I tried different tips and tricks and it wasn't worked until I reinstalled Scala plugin in Eclipse .
I'd solve similar problem by executig "Project->Clean.." with next automatically building.
I had the same error message with a Java application made by myself.
The problem was that I deleted (though inside Eclipse) a jar that belonged to the Java build path, without deleting it from the Java build path (project's Properties window). When I did it the class could compile and run again.
Make sure that the .class files exist, usually below the bin directory.
In particular, if you have errors in unrelated files in the same project then the compilation may fail, and no .class files will be produced.
There can be the case of projects, containing errors, added to the build path of the application which prevents the completion of successful compilation. Make sure you remove any such project from the build path before running the application.
Removing these projects solved the problem for me.
Do you have a proper build tool setup? Like sbt have you installed it?
You can check its version by $sbt --version
If it is not setup you can download from here http://www.scala-sbt.org/download.html
You might have to restart your eclipse after installation.
Just copy your XXX.scala file code. Remove the package and create a new Scala Class. Paste your XXX.scala code. (If you are using maven, do a maven clean and build.) Run configuration again. This works for me.
I have faced this issue. I have just deleted the package name, created scala class, Written the same code, Set Build to "Build Automatically". Finally, It works perfectly fine.
Check scala-ide.log
For me the issue was that there were errors on:
AppData\Local\Temp\sbt_10d322fb\xsbt\ClassName.scala:16: error: not found: value enteringPhase
enteringPhase(currentRun.flattenPhase.next) { s fullName separator }
If you are using Intellij, mark directory as source root