So I'm pretty new at scala. I'm trying to use this library in my other project: https://www.github.com/desmondyeung/scala-hashing
I downloaded it, and looked up a guide on how to use downloaded projects (https://www.oreilly.com/library/view/scala-cookbook/9781449340292/ch18s11.html), and it said to make a build.scala and then put some such in it. The way I've tried to do that is this:
import sbt._
object MyBuild extends Build {
lazy val root = Project("root", file(".")) dependsOn(xxHash)
lazy val xxHash = RootProject(uri("file:///Users/[other stuff]/documents/GitHub/scala-hashing-master/project"))
}
And that's all that's in that build.scala file. It's under the project folder.
I tried to run it using some simple test code:
import xxHash._
object hashtest {
def main(): Unit = {
println(XxHash.com.desmondyeung.hashing.Xxhash64.hashByteArray(Array[Byte](xs = 123), seed = 0))
}
}
But I'm getting an error, it says "not found: object xxHash". I must be missing something, because the guide doesn't tell me how to reference it I don't think? I tried just using import com.desmondyeung.hashing.XxHash64 but it didn't work either, saying object desmondyeung is not a member of package com
I googled that, and it said to try putting _root_ before .com, but that did not work.
Related
I've been tasked to update and write a series of tests on an app in Scala Play, a language and framework I'm unfamiliar with. Part of what I'd like to do is integrate the ScalaTestPlus library. To get started I have been following the following tutorial:
https://www.playframework.com/documentation/2.2.x/ScalaTestingWithScalaTest
Unfortunately I am not getting very far. I have added a new unit test file to the tests folder:
import org.scalatestplus.play._
class StackSpec extends PlaySpec {
"A Test" must {
"pass" in {
assert(1 == 1)
}
"Fail" in {
assert(1 != 1)
}
}
}
and I have updated my build.sbt to include the scalatestplus library
"org.scalatestplus" % "play_2.37" % "1.2.0" % "test"//,
Using Activator, I am trying to run my test file with test-only. Everything compiles without errors, but activator is not finding any tests
[info] No tests were executed.
I don't believe the issue is with activator, since I can run old test files (from the previous engineer) using the test and test-only commands. A quick sample of one of the previous (working) test files:
import java.util.concurrent.TimeUnit
import com.sun.xml.internal.bind.v2.TODO
import scala.collection.JavaConverters._
import controllers.Application
import models.{Item, PriorityBucket}
import play.api.test._
class WebSpec extends PlaySpecification {
"Home page" should {
"do something" in new WithSeleniumDbData(TestUtil.testApp) {
Redacted.deleteAll()
val ObId = TestUtil.create(Some(PriorityBucket.Low),
Some(Application.ENGLISH))
val item = Item.find(ItemId).get
browser.goTo("/")
browser.await().atMost(2,
TimeUnit.SECONDS).until(Selectors.all_obs).isPresent
}
Any ideas where I've gone astray? Thanks in advance for the help!
I am using scala 2.11
I am using play 2.3.7
EDIT: Possibly relevant, I switched the extension from PlaySpec to FlatSpec and saw the following error when compiling:
SampleSpec.scala:10: value in is not a member of String
[error] "pass" in {
I made sure to import FlatSpec as well, which has me a bit confused--is FlatSpec a member of ScalaTest but not a member of ScalaTestPlus, I don't see why else the compilation would fail.
UPDATE: To further investigate the issue I spun up a brand new Play app and copied over my sample test. After some tooling around with versions I've been able to get my test to run on the activator test command with the rest of the suite. Unfortunately, any other commands like test-only are still returning no tests run.
For those following I ran across the issue...the class name in this case needed to be identical to the file name, otherwise test-only cannot locate it.
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"))
}
I'm trying to add artifact to my play project, I've looked in couple of forums and looks like this is the proper way to do it:
lazy val playProject = play.Project(myProjectName, myProjectVersion, path = file("."))
.settings(addArtifact(Artifact (myProjectName, "dist", "zip"), dist).settings: _*)
but then I'm getting compilation error:
"...project/Build.scala:26: not found: value dist"
where I need to define it? what am I missing here?
additional info: my "playProject" is a module inside scala project that contain some other scala modules.
It is difficult to be sure with such a limited extract of your build definition, but my guess would be you are in a scala build file and didn't import the dist key in scope.
Try adding the following import to your build file
import com.typesafe.sbt.packager.universal.UniversalKeys.dist
addArtifact has the following signature :
def addArtifact(a : sbt.Artifact, taskDef : sbt.TaskKey[java.io.File])
UniversalKeys.dist is defined as follows:
val dist = TaskKey[File]("dist", "Creates the distribution packages.")
So the types are correct at least :)
I am trying to use JOGL with Scala in Eclipse, but being a JOGL/Scala neophyte, have run into some dependency errors which I cannot make any heads or tails of. Googling hasn't returned much of anything useful.
I have set up a Java JOGL project as per
Setting_up_a_JogAmp_project_in_your_favorite_IDE.
Compiling the following Java class in a Java project that depends on the above project
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.awt.GLCanvas;
public class Game {
public static void main(String[] args) {
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
GLCanvas canvas = new GLCanvas(caps);
System.out.println("Hello World");
}
}
works, and outputs Hello World as expected.
However, making a Scala project and trying to use the JOGL project as follows
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.awt.GLCanvas;
object Game {
def main (args: Array[String]): Unit = {
val glp = GLProfile.getDefault();
val caps = new GLCapabilities(glp);
val canvas : GLCanvas = new GLCanvas(caps);
System.out.println("Hello World");
}
}
won't even compile, as Eclipse informs me of the following errors:
error while loading CapabilitiesImmutable, Missing dependency 'class com.jogamp.common.type.WriteCloneable', required by G:\Eclipse\workspace\JOGL\jogl-all.jar(javax/media/nativewindow/CapabilitiesImmutable.class) Scala JOGL Unknown Scala Problem
error while loading GLCanvas, Missing dependency 'class com.jogamp.common.util.locks.RecursiveLock', required by G:\Eclipse\workspace\JOGL\jogl-all.jar(javax/media/opengl/awt/GLCanvas.class) Scala JOGL Unknown Scala Problem
error while loading GLContext, Missing dependency 'class
com.jogamp.common.util.locks.RecursiveLock', required by G:\Eclipse\workspace\JOGL\jogl-all.jar(javax/media/opengl/GLContext.class) Scala JOGL Unknown Scala Problem
Removing the lines pertaining to GLCapabilities and GLCanvas, giving
object Game {
def main (args: Array[String]): Unit = {
val glp = GLProfile.getDefault();
System.out.println("Hello World");
}
}
does compile and print Hello World.
My questions are - Why doesn't the Scala code work, and what can I do to fix it? Am I doing some crazy voodoo mixing up JOGL and Scala code that I shouldn't be doing? Did I forget to add some dependencies?
Version Information
Eclipse: (Version: Juno Release Build id: 20120614-1722)
Scala IDE for Eclipse: (Version: 2.1.0.nightly-2_09-201208290312-cc63a95)
(Provider: scala-ide.org)
JOGL as part of JOGAMP Release 2.0-rc10
Edit:
Ok, adding the gluegen-rt.jar and jogl.jar libraries to the build path in the Scala project itself solves this issue (I can't believe I didn't think of doing that first .. ). I'm still not exactly sure what I was doing wrong though.
Just for reference, in one of my JOGL SBT projects, I needed to add:
gluegen-rt.jar
gluegen-rt-natives-.jar
jogl-all-2.0-rc9.jar
jogl-all-2.0-rc9-natives-.jar
to the list of dependencies in order for this to work. My guess is that you have to include these on the build path in Eclipse.
These are available from this repository: http://jogamp.org/deployment/maven
I am learning Scala so bear with me if this is a stupid question.
I have this package and a class (teared it down to most simplistic version):
package Foo {
class Bar {}
}
then in main.scala file I have:
import Foo.Bar
object test {
def main() {
val b = new Bar()
}
}
Why am I getting this:
test.scala:1: error: Bar is not a member of Foo
It points at the import statement.
scalac is the scala compiler. Foo.bar needs to have been compiled for you to use it, so you can't just run your main.scala as a script.
The other mistake in your code is that the main method needs to be
def main(args: Array[String]) { ...
(or you could have test extends App instead and do away with the main method).
I can confirm if you put the two files above in an empty directory (with the correction on the main method signature) and run scalac * followed by scala test it runs correctly.
The most likely explanation is that you did not compile the first file, or you are doing something wrong when compiling. Let's say both files are in the current directory, then this should work:
scalac *.scala
It should generate some class files in the current directory, as well as a Bar.class file in the Foo directory, which it will create.
To quickly test a scala code in IntelliJ (with the Scala plugin), you can simply type Ctrl+Shift+F10:
Note that for testing a Scala class, you have other choices, also supported in IntelliJ:
JUnit or TestNG
ScalaTest