I need to compile a scala code which calls a java code from it.
What I did:
1]I have a scala main file check.scala
package com.code
class check {
var Rectvalue = Array.ofDim[Int](5)
val str = Array.ofDim[String](1)
def nativeacces(arg: String, loop: Integer) {
val test = new testing()
test.process(arg, Rectvalue,str)
}
}
2.For creating instance val test = new testing() ,i added two .class(sample.class,testJNI.class) file from java source code inside the folder(package) com/code.
3.When I compile the scala code using
scalac check.scala
It generates the class file for the scala file.
What I have to do:
1.Instead of .class(sample.class,testJNI.class) file added inside the package ,i need to add jar file.
2.I tried, created jar file for the .class file and compile the Scala, it shows the error:
scala:6: error: not found: type testing
val test = new testing()
3.I need to link the .jar file and compile the scala main file
You can reference classes/directories/JARs via classpath option:
scalac -classpath your.jar check.scala
Related question: Adding .jar's to classpath (Scala).
If you want a proper build use SBT, put your JAR in lib directory in the root of project and it will figure out what to do for you. Here is Hello World of SBT.
Related
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
I'm writing an integration test method in scala (play framework). The test class is SourceIntegrationTest. I've placed a file, source.json, in /test/resources. I'm aware that "sbt copies files from src/test/resources to target/scala-[scalaVersion]/test-classes" as described in this answer. However, using the answer referenced there only works for me when running my test in IntelliJ. When I run sbt it:testOnly SourceIntegrationTest in terminal, my test fails with a NullPointerException. sbt cannot find source.json. How can I get sbt to find my file when running my integration test in terminal?
My test method looks like:
#Test
def testGetSource(): Unit = {
val jsonSource: String = Source.fromInputStream(getClass.getClassLoader.getResourceAsStream("source.json")).mkString
val json: JsValue = Json.parse(jsonSource)
val source = controller.getSource(json)
assertEquals(source.sourceName = "Premier")
}
When you write an integration test, the test file is not placed in the test folder. You need to create another directory for integration test. Now the source folder will contain three directories main, test, it. And all integration test will be kept in it folder. You can read about it here
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. :)
I have a Scala script that I want to call from sbt. This Scala script refers to some dependencies. One of those dependencies uses a properties file. This properties file is provided by the run time as this dependency is run as a separate application.
Just to have the possibility to run that property-using dependency as a standalone, I wrote this Scala script that I want to call from sbt.
val fis = new FileInputStream("my.properties") // Fails here
val props = new Properties()
When I run the above code, it fails with an exception in my dependency where the properties file is loaded.
How to make this properties file available to the script under sbt?
Place the file my.properties in src/main/resources and use Source.fromURL(getClass.getResource("/my.properties")) instead (as it gives you more flexibility in where you can place the file on file systems as long as it's on CLASSPATH).
As a helper, use the following code to learn about the place where the file is expected when a "bare" File* types are in use:
println(new java.io.File("my.properties").getAbsolutePath)
Since the current working directory is the top-level directory of a project, the file is searched in $PROJECT_ROOT_DIR/my.properties.
I have two different copies of scala version 2.9.2 from the Scala-Lang website.
No. 1 is binary code: scala-2.9.2.tgz
No. 2 is source code: scala-sources-2.9.2.tgz
I built scala from the source code. I have tried using both in an application and the binary works but the compiled source code does not.
My use case: I am compiling a tiny scala file that I wrote (see below for an example), then creating a jar, and then running "main" from within that jar on the command line. To make that jar, I must include "scala-library.jar" (from this SO answer) on the classpath. I need to be able to use the scala-sources that I build so that I can modify the source code later on.
When I use the compiled source code, I get an error regarding serialVersionUID, whereas the binary code does not cause this issue.
Exact error message (when using the scala that I build from source):
java.io.InvalidClassException: scala.collection.mutable.WrappedArray$ofRef; local class incompatible: stream classdesc serialVersionUID = 8184381945838716286, local class serialVersionUID = 6238838760334617323
Example code that I am running for deserializing a file:
object test {
def main(args: Array[String]) {
var infile = args(0)
val thing1 : MyClass = withObjectInput(infile) {
f => f.readObject().asInstanceOf[MyClass]
}
}
}
If MyClass doesn't explicitly supply a serialVersionUID, the the compiler generates one. This will be different for different versions of javac used when compiling it.
So this error will be caused by a mismatch between the version of javac that you used for compiling scala-library and the version that was used for the downloaded binary.