Add a .so library with class files to sbt scala project - scala

I need to call some c functions from my sbt project. I have already used SWIG and created a .so file along with .class files.
I wrapped everything in a jar file and put it in lib/ folder but it keeps saying no value found.
How can I use the .so library generated from swig along with the .classes files in an sbt project?
The name of the library is libsample.so.
I can successfully upload the library in sbt with System.LoadLibrary("sample") but I cannot call sample.entry() (not found value sample...)

Can you use something like a DependencyWalker and make sure your are not missing any dependencies on they box?

Related

how to package and reference text file in resources folder

I have a spark project using scala and sbt. At one point it references a text file which I want to be packaged.
This is how it is referenced in the application source:
getClass.getResource("/myFile.txt")
This works fine running the source code with sbt run. But I want it to be packaged and deployed to a server.
In build.sbt, after some googling I have got this to work
import NativePackagerHelper._
mappings in Universal ++= directory("src/main/resources")
adding this meant that the myFile.txt appears in the resources folder in the package. created using
sbt universal:packageBin
resulting folder structure:
target - universal - bin
- lib
- resources
however when I run my packaged application from bin/my-application.bat , I get the following error
Exception in thread "main" org.apache.spark.sql.AnalysisException: Path does not exist: file:/C:/Software/my-application-0.0.1/lib/my-application-0.0.1.jar!/myFile.txt;
Bear in mind I have zero experience of deploying scala or jvm based things so you may have to spoonfeed me the explanation
EDIT I later realised that the text file was in fact included in the .jar file.
the issue then was that getResource does not work in this case and I had to adapt my code to use getResourceAsStream
This can have multiple reasons.
Include files in your resulting jar
You added this line, which is not correct
import NativePackagerHelper._
mappings in Universal ++= directory("src/main/resources")
The src/main/resources directory is the resourceDirectory in Compile and the contents are always present in the package jar file (not the zip!). So I would highly recommend removing this snippet as you will have your files twice in your classpath.
The mappings in Universal (documentation link) define the content of the created package (with universal:packageBin the zip file). I assume that you are using the JavaAppPackaging plugin, which configures your entire build. By default all dependencies and your actual build artifact end up in the libs folder. Start scripts are being place in bin.
The start scripts also create a valid classpath, which includes all libraries in lib and nothing else by default.
TL;DR You simply put your files in src/main/resources and they will be available on the classpath.
How to find files on the classpath
You posted this snippet
getClass.getResource("/myFile.txt")
This will lookup a file called myFile.txt in the roots of your classpath. As in the comment suggested you should open your application jar file and find a text file myFile.txt at the root, otherwise it won't be found.
hope that helps,
Muki

SBT unmanaged jars

I have a dependency that includes several jar files in the lib folder. When I try to compile my application, it does not find the classes in those jar files.
More specifically, it fails to find the classes defined in polyglot.jar, with this error:
Error:scalac: Class polyglot.frontend.JLExtensionInfo not found - continuing with a stub.
Is there any way to make the included jar files visible on the classpath, without copying them into my own project's lib folder?
Thanks!
What 0__ said: no, nested jars won't be discovered. There are some workarounds, but they really aren't worth it. If you want the jars as unmanaged dependencies, you'll have to add them to your own lib/.

How to make sbt-native-packager refrain from putting my resources into a jar file?

I'm using the sbt-native-packager plugin to generate a start script for my application, which is very convenient as this plugin generates the correct classpath specification with all my library dependencies. I am not distributing this applictaion, therefore I'm not packaging the entire thing into one tarball. I just use the lib directory generated by sbt-native-packager that contains all the jar-files on which my project depends, both third-party libraries as well as the jar-file that contains my own class and resource files.
In my project's src/main/resources directory I have files that I want to be able to edit without having to use sbt-native-packager to regenerate the entire installation, for example configuration files. This is difficult because those files are zipped up in the jar file with all my classes.
Question: how can I tell sbt-native-packager not to put my resource files into a jar-file, while still generating the start-script with the correct classpath for those resource files to be located and read by my application as they are now from within the jar file? If this means leaving all my class files out of a jar file that is fine, as long as the files from src/main/resources remain as files that I can change without re-invoking sbt stage and as long as the start-script works.
While it is possible to filter these resources I would suggest to put them into a different directory and add them to the classpath.
Modifying the start script generated by sbt-native-packager is a bit cumbersome as the class com.typesafe.sbt.packager.archetypes.JavaAppBashScript that is generating the classpath is prefixing all paths with $lib_dir/. The cleanest approach would probably be to provide your own implementation and use that to generate the bashScriptDefines.
A simpler but hacky way would be to just add the following lines to your build.sbt:
packageArchetype.java_server
// add your config files to the classpath for running inside sbt
unmanagedClasspath in Compile += Attributed.blank(sourceDirectory.value/"main"/"config")
// map all files in src/main/config to config in the packaged app
mappings in Universal ++= {
val configDir = sourceDirectory.value/"main"/"config"
for {
file <- (configDir ** AllPassFilter).get
relative <- file.relativeTo(configDir.getParentFile)
mapping = file -> relative.getPath
} yield mapping
}
scriptClasspath ~= (cp => "../config" +: cp)
This will prepend $lib_dir/../config to your start script's classpath. If your app has to run on Windows you will have to provide similar settings for the batScriptDefines.

build paths eclipse java j2ee

I have imported a project (I am very new at this) and I get the following errors:
Project cannot be built until build path errors are resolved
Project FST is missing required library: 'C:program Files/Apache Group/Tomcat 4.1/common/lib/servlet.jar'
Project FST is missing required library: 'C:program Files/Apache Group/Tomcat 4.1/common/lib/struts.jar'
The project cannot be built until build path errors are resolved
Unbound classpath variable: 'TOMCAT_HOME/common/lib/jasper-runtime.jar' in project
Unbound classpath variable: 'TOMCAT_HOME' in project FST
I create a variable called TOMCAT_HOME and give it the proper directory ,but Also, we should change the project classpath to use TOMCAT_HOME rather than the absolute path.
i dont know how to do it (change the project classpath and the absolute path)
thanks !
To answer somewhat indirectly, if you configure your project build with something like Maven or Gradle, so that you can successfully build the project using the corresponding command-line tool, then it should be quite straightforward to import the project into eclipse using the Maven or Gradle eclipse plugin. I think doing so will be worth whatever trouble it causes you in the short run--just take care to make your project structure conform to the usual project structure that Maven expects or you'll be asking for trouble (it should be no problem to do so for greenfield work).

I can't use sbt.Process inside /src?

I'm currently using sbt to build and run my scala programs. I'm trying to use sbt.Process to execute system commands. I must be missing something because when I try to import sbt.Process in one of my files in src/ I get this error.
not found: value sbt
[error] import sbt.Process._
So it looks like I can't access the sbt package inside my src/ files. What do I need to do to access it? Thanks.
SBT's environment (v 0.7.x) is only available in your build file or a Plugin.
The easiest way to use sbt.Process library (until 0.9.x which will have Process as an independent library) is to copy (BSD License) Process.scala and ProcessImpl.scala into your project
There are different classpaths for running sbt and compiling your source files.
One classpath is for compilation of files in directory project/build (that one contains sbt jars and usually scala library 2.7.7) and the other one is for building source files of your project (that one contains your dependencies from lib and lib_managed and usually scala library 2.8.*). If you'd like to use sbt.Process in your source files you can do two things:
add sbt jar to lib or lib_managed for it to be available on your project's classpath
use snapshot version of scala 2.9, it would have sbt Process built-in as sys.process package
Wait for Scala 2.9, and then just use it out of scala.sys.process.
sbt package has became an integral part of the Scala standard library since version 2.9
...this API has been included in the Scala standard library for version 2.9.
quoted from sbt wiki
Here's the link (scroll down)
well, in order to use it, all you have to do (assuming you are using sbt for build), is to add in build.sbt file the following line of code: sbtPlugin := true it will add the needed dependencies to your project.
of course, this solution is only to get your imports with sbt package to work. you should refactor your code to use the new package scala.sys.process like Daniel C. Sobral suggested.