Scala IDE: can't read XML file into scala worksheet - eclipse

New to Scala and having problems reading an XML file in a Scala worksheet. So far I have:
downloaded the Scala IDE (for Windows) and unzipped it to my C:\ drive
created a Scala project with the following file path: C:\eclipse\workspace\xml_data
created the xml file ...\xml_data\music.xml using the following data
created a package sample_data and create the following object (with file path: ...\xml_data\src\sample_data\SampleData.scala):
package sample_data
import scala.xml.XML
object SampleData {
val data = XML.loadFile("music.xml")
}
object PrintSampleData extends Application {
println(SampleData.data)
}
This runs OK, however, when I create the Scala worksheet test_sample_data.sc:
import sample_data.SampleData
object test {
println(SampleData.data)
}
I get a java.lang.ExceptionInInitializerError which includes: Caused by: java.io.FileNotFoundException: music.xml (The system cannot find the file specified).
The workspace is C:\eclipse\workspace. Any help or insight much appreciated. Cheers!
UPDATE:
Following aepurniet's advice, I ran new java.io.File(".").getAbsolutePath() and got the following respectively:
SampleData.scala: C:\eclipse\workspace\xml_data\.
test_sample_data.sc: C:\eclipse\.
So this is what is causing the problem. Does anyone know why this occurs? Absolute file paths resolve the problem. Is this the best solution?

Regarding what is causing different user directory between the scala class and worksheet:
You are likely hitting the Eclipse IDE issue listed here
https://github.com/scala-ide/scala-worksheet/issues/102
Jfyi, I used Intellij and the issue is not reproducible there.
Regarding using absolute paths:
Using absolute path works fine for quick testing, but would NOT be a good practice for the actual implementation. You can consider passing the path along with the filename as input to SampleData.
Some hack mentioned here to get the base path of the workspace from the scala worksheet: Configure working directory of Scala worksheet
If this is just for your testing, hacking the absolute path of workspace inside the worksheets might be the easiest for you.
SampleData.scala
package sample_data
import scala.xml.XML
object SampleData {
def data(filename: String) = XML.loadFile(filename)
}
object PrintSampleData extends Application {
println(SampleData.data(System.getProperty("user.dir") + "/music.xml")
}
Scala worksheet:
import sample_data.SampleData
object test {
val workDir = ... // Using the hack or hardcoding
println(SampleData.data(workDir + "/music.xml"))
}

Related

Get relative path from inside a scala library (while developing that library)

I'm currently developing a scala library and wanted to get a file that is inside it, event when compiled as a Jar dependency. The problem is that when executed from another project where the library is imported, the path is relative to that project. Here is the code to get the file :
private val pathToDocker: Path = Paths.get("src", "main", "resources", "docker-compose")
What can I do to look for my file inside the imported dependency ?
The file won't be in a file system -- it'll be in the compiled JAR.
You can get a JAR resource using a class loader. Here's an example of how to do that in another codebase.
Utility function:
https://github.com/hail-is/hail/blob/6db198ae06/hail/src/main/scala/is/hail/utils/package.scala#L468
Usage to load version info:
https://github.com/hail-is/hail/blob/6db198ae06/hail/src/main/scala/is/hail/package.scala#L21
There is a JarUtil - from an answer of access-file-in-jar-file translate to Scala:
import java.util.jar.JarFile
val jar = new JarFile("path_to_jar/shapeless_2.12-2.3.3.jar")
val entry = jar.getEntry("shapeless/Annotation.class")
val inStream = jar.getInputStream(entry)
As you mentioned scala.io.Source.fromResource works only within your project:
Source.fromResource("tstdir/source.txt")
Make sure the file is in the resources directory, like:
The way I found to achieve getting the path inside a jar depedency was creating a singleton (scala object) that has a method that loads the files. Since it is inside the Jar, the resulting path is relative to the jar itself. Here is the code :
object ClassLoaderTest {
def dockerFile: File =
new File(getClass.getClassLoader.getResource("docker/docker-compose.yml").getPath)
}
When called from a Trait (or an interface), the resulting path is :
jar:file:/home/myname/.ivy2/cache/com.mypackage/libraryname/jars/libraryname.libraryversion.jar!/docker/docker-compose.yml

Scala: FileNotFoundException when the file does exist

I started learning Scala a few weeks ago and I am now trying to read data from a .csv file.
However, the line val data = fromFile("test.csv") returns me the error
java.io.FileNotFoundException: test.csv (No such file or directory)
Usually, this error happens when the file does not exist, but I made sure that test.csv is in the root folder of my project (I am working on IntelliJ)
This code is in a scala worksheet, and the entire code looks like
import scala.io.Source.fromFile
object Test{
class Class1(...){...}
class Class2(...){...}
def function1(...)
val data = fromFile("test.csv")
}
I am afraid that this issue is caused by IntelliJ as I tried to install Eclipse and run my code there and it seemed to work. Do you have any idea of what could cause that error to happen?
Note: The file is supposed to be read in a method of Class1 but I took it out to see if it worked outside Class1 and it turns out it doesn't
EDIT: My test.csvcan be anywhere, as long as it works. I do not need a solution using a relative path

not found: type SimplexSolver while using org.apache.commons.math.optimization.linear

I am working with scala and want to use SimplexSolver (https://opensource.googleblog.com/2009/06/introducing-apache-commons-math.html). I am using sbt, hence downloaded the .jar file for commons-math-2.2 and imported it in the code. Then I tried to use it, but it showed not found: type SimplexSolver. Following is the code snippet.
import org.apache.commons.math.optimization.linear
class MySimplexSolver{
val solver = new SimplexSolver()
}
I have a lib folder where all the .jar files are kept. Any help regarding this is appreciated. :)

'Unable to load a Suite class' while running ScalaTest in IntelliJ

I'm having some issues today with running a simple TestKit test in Intellij. The tests are for Scala code (I have the Scala plug-in for Intellij) and are based on Ray Roestenburg's example.
The Intellij project was created using a "Maven Module" which I then added all the dependencies to and created my project. The tests are located in the following place:
ActorBlast/src/test/scala/basicTest.scala
I'm basically "right-clicking" on the test and selecting "Run". What I get is the following error:
"C:\Program Files\Java\jdk1.7.0_25\bin\java" -Didea.launcher.port=7540...
Testing started at 2:29 PM ...
Unable to load a Suite class. This could be due to an error in your runpath.
Missing class: BasicActorSpec java.lang.ClassNotFoundException:
BasicActorSpec at
java.net.URLClassLoader$1.run(URLClassLoader.java:366) at
java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
org.scalatest.tools.Runner$$anonfun$35.apply(Runner.scala:2393) at
org.scalatest.tools.Runner$$anonfun$35.apply(Runner.scala:2391) at
scala.collection.TraversableLike$$anonfun$filter$1.apply(TraversableLike.scala:264)
at scala.collection.immutable.List.foreach(List.scala:318) at
scala.collection.TraversableLike$class.filter(TraversableLike.scala:263)
at scala.collection.AbstractTraversable.filter(Traversable.scala:105)
at
org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:2391)
at
org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1006)
at
org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1005)
at
org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:2659)
at
org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1005)
at org.scalatest.tools.Runner$.run(Runner.scala:845) at
org.scalatest.tools.Runner.run(Runner.scala) at
org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:144)
at
org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606) at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 0
I can't figure out what this means. I've done a lot of searching but can't seem to find an answer. Note that the class the runner is complaining about not finding is the class I'm trying to test/run. The basicTest.scala looks like this:
// Testing specific imports
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{ShouldMatchers, WordSpecLike, BeforeAndAfterAll}
import akka.testkit.{TestKit, DefaultTimeout, ImplicitSender}
// Actor specific imports
import akka.actor.{ActorRef, Actor, ActorSystem, Props}
// Misc. needed imports
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory
// In order to run tests in this module you need to use JUnitRunner (as per scalatest.org)
#RunWith(classOf[JUnitRunner])
class BasicActorSpec extends TestKit(ActorSystem("BasicActorSpec", ConfigFactory.parseString(BasicActorSpec.config)))
with DefaultTimeout with ImplicitSender with WordSpecLike with ShouldMatchers with BeforeAndAfterAll {
import BasicActorSpec._
val echoRef = system.actorOf(Props[EchoActor])
val forwardRef = system.actorOf(Props[ForwardActor])
override def afterAll {
shutdown(system)
}
/**
* The actual tests...
*/
"An EchoActor" should {
"Respond with the same message it receives" in {
within(500 millis) {
echoRef ! "test"
expectMsg("test")
}
}
}
"A Forwarding Actor" should {
"Forward a message it receives" in {
within(500 millis) {
forwardRef ! "test"
expectMsg("test")
}
}
}
}
/**
* Companion object of test class
*/
object BasicActorSpec {
val config =
"""
|akka {
| loglevel = "Warning"
|}
""".stripMargin
/**
* Classes of Actors used in testing
*/
class EchoActor extends Actor {
def receive = {
case msg => sender ! msg
}
}
class ForwardActor(next: ActorRef) extends Actor {
def receive = {
case msg => next ! msg
}
}
}
Any help as to why I am getting this error would be GREATLY appreciated.
Run build the project - It helped me to resolve that issue that could have happened to me when I cleared Cache IDEA :) while trying to tackle another issue
This is how I solved same exception:
--> Right click on your project folder in IDE:
--> Click Add Framework Support
--> Then Check Scala
--> Click OK
My project already had the setup as mentioned by #Rustam Aliyev. Still was getting the same exception. Rebuilding the project did not help either. Quite weird ; but Restarting the IDE helped to solve the issue
This issue happened to me recently when I was trying to run tests in an inherited Scala project using IntelliJ IDEA 2018 (Community Edition). Below steps helped me to fix it:
File → Project Structure → {Choose specific module} → "Paths" tab → Select "Use module compile output path" → Modify "Test output path" to point to test-classes folder. For example:
Output path: /home/rustam/IdeaProjects/{project}/{module}/target/scala-2.12/classes
Test output path: /home/rustam/IdeaProjects/{project}/{module}/target/scala-2.12/test-classes
IntelliJ does not like multiple Scala classes defined in one file, so make sure the test class is named the same as the test file and nest other helper classes, which you can refactor later as necessary.
I met this issue when I used Gatling
I fix it by replacing gatling-classes to test-classes on File -> Project Structure -> Modules -> Module Name -> Paths -> Test output path
You need to set up the Scala SDK.
1.) Usually, intelliJ will ask you by showing a message on right hand corner of your editor
2.) You can do it by yourself as mentioned on the https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html
In my case, I was missing the Scala facet in my module.
https://blog.jetbrains.com/scala/2010/09/02/project-configuration-explained/
I got rid of the error once I configured module properly.
You can try to recompile the test classes:
sbt test:compile
If you are using IntelliJ to run scalatest make sure the class paths are correct. For example:
/dummyApp
your build.sbt should look like, name := "dummyApp". If you name it name := "dummy App" you will get errors.
In my case I had in Preferences -> Build, Execution, Deployment -> sbt
Use sbt shell for build and import checkbox enabled
And the test was not in the expected directory src/test but in src/it (integrated test).
I encountered the error when the test class was not part of any package.
I am using the multi maven module and tried all possibilities here but not able to fix this. But for me I closed the complete IntelliJ -> removed .idea folder -> deleted managed projects from recent projects window.-> reimport the project did the job.
This kind of thing keeps happening to me every now and then. All the more concrete suggestions above have merit. Another possibility (which worked this time for me): Edit the run configuration for your tests; click "Use sbt"; run the tests (this should have no problem since it's just using the sbt-shell to run the tests); now unselect "Use sbt" and try it. It worked for me.
Another one in the long list of checks, if you do not use unique test names you get this error without any hint, in a long test suite it is easier to miss.
Failed
test("Check thing 1") {
class TestClass1 extends MainClass1{
... }
test("Check thing 1") {
class TestClass2 extends MainClass2{
... }
Successful
test("Check thing 1") {
class TestClass1 extends MainClass1{
... }
test("Check thing 2") {
class TestClass2 extends MainClass2{
... }
I got the same message on Idea 2021.1. I tried all the ways above but what helped me is running Scala-tests via sbt like this:
sbt "; project nameOfProject; testOnly some.package.SomeTest"
After that I could debug the same tests via Idea.
I had two modules marked as Test Sources Root and thus it didn't like that. So I unmarked one and then it worked great

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