creating and using standalone scalaz jar without sbt - scala

I've downloaded scalaz snapshot from repository (version 6.0.4).
I want to create standalone jar file and put it into my scala lib directory to use scalaz without sbt.
I'have scala package from scala-lang.org, and stored in /opt/scala
As far I did:
go to untared scalaz directory
run sbt from scalaz project
compile scalaz project
make a package (by package command)
sbt make a jar full/target/scala-2.9.1/scalaz-full_2.9.1-6.0.4-SNAPSHOT.jar
it also produce other jar: full/lib/sxr_2.9.0-0.2.7.jar
I moved both jars to /opt/scala/lib
After this I try scala repl and I can't import scalaz. I tried to import scalaz._, Scalaz._, org.scalaz._, scalaz-core._ and don't work.
REPL code completition after typing import scalaz it suggest: scalaz_2.9.1-6.0.4-SNAPSHOT.
But import scalaz_2.9.1-6.0.4-SNAPSHOT._ don't work
Any idea?

you can download scalaz and extract the jar that contains scalaz-core_2.9.1-6.0.3.jar. Or download scalaz-core directly.
then you can use : scala -cp scalaz-core_2.9.1-6.0.3.jar to launch the REPL finally import scalaz._ as expected.
If you want to use the jar produced by sbt, you can find it in core/target/scala-2.9.1/scalaz-core_2.9.1-6.0.4-SNAPSHOT.jar (you will also find source and javadoc packages in the same directory). Just put this file in your classpath (using scala -cp for example) and you will be able to import scalaz._

I think I know the problem.
scalaz-full_2.9.1-6.0.4-SNAPSHOT.jar is not a java jar class package, it's just a zip with scalaz project - so it contains not package - like directory tree (eg: directory names contains '.').
So to use it we need to unpack scalaz-full_2.9.1-6.0.4-SNAPSHOT.jar, and copy desired jars (eg: scalaz-core_2.9.1-6.0.4-SNAPSHOT.jar, scalaz-http_2.9.1-6.0.4-SNAPSHOT.jar ...) to lib directory.

Related

How to use Phantom in Scala IDE

I want to use phantom with my scala IDE.So for this i clone the git hub repository and created a .jar file of phantom using sbt -> compile -> package.I add this .jar file to build path in my Scala IDE but still while importing
import com.websudos.phantom.connectors._
is throwing error that
object connector is not a member of com.websudos.phantom.
While using auto complete function of scala ide it is showing only the import for
import com.websudos.phantom.example
.I don't know if the jar files got created for example then why it is not created for other.
I search in internet but all other option are given as to add dependency in sbt build path but i dont want to use it.
Use sbt-assebly instead to create a fat jar.
https://github.com/sbt/sbt-assembly

Making jar files available to the scala REPL

I can't seem to get this to work, so i have tried what's probably the most straightforward case--ie, retrieve the jar file and into a given directory then start the scala REPL from that directory--still no luck.
I know the jar files are fine and correctly references (eg, i can access them from project directories via eclipse/scala ide)
I also know that my import statements (one is shown below) are valid.
cd ~/my_scala_jars
retrieve the artifact from the maven repo
wget -q -nd -np -r \
http://repo1.maven.org/maven2/org/scalaz/scalaz-core_2.12.0-M1/7.1.2/scalaz-core_2.12.0-M1-7.1.2.jar
start the scala REPL and add the jar to the REPL classpath:
scala -cp scalaz-core_2.12.0-M1-7.1.2.jar
scala> import scalaz.stream.io
<console>:7: error: object stream is not a member of package scalaz
import scalaz.stream.io
in case it's useful, trying this with different jars (that i downloaded as i showed with scalaz above) will sometimes cause the REPL to throw a not found error instead
scala> import breeze.linalg._
<console>:7: error: not found: value breeze
import breeze.linalg._
it seems to me that the REPL should recognize these jars because the directory has been added to my CLASSPATH, but also because i believe the current directory is also added to the classpath (at least during the REPL session)--and still both fail.
I am not sure why the jar files are not picked up as I haven't really tried to do that. What I do, and find useful, is to create a number of build.sbt files for specific purposes, e.g. for working with a specific suite of libraries at a REPL.
If you do this, and then launch a REPL using the command sbt console, from the directory containing the appropriate build.sbt, then you can have a console with the libraries you want available on the classpath to that console.
This might not be ideal, but it does work and I find it quite useful.

Scala REPL unable to import packge

I'm trying to import com.lambdaworks.crypto.SCryptUtil (from crypto) in the Scala REPL. I'm running the REPL from the Java directory containing com/lambdaworks/crypto.
The REPL can't find com.lambdaworks.crypto.SCryptUtil, but it can autocomplete up to com.lambdaworks.crypto but can't find anything after that.
When I used the REPL in the IntelliJ IDEA after including the package in my project, I was able to find the SCryptUtil class.
Am I missing some classpath parameters that are required for import?
The REPL won't compile the Java code for you—it's only autocompleting that far because it's aware of the directory structure, but once it gets to the crypto directory it won't find any class files.
You can see this more dramatically by moving up a directory and opening a new REPL—you'll be able to autocomplete import java.com.lambdaworks.crypto, even though that's obviously not a real package hierarchy.
In this case you can move to the project root, run mvn compile to compile the Java code, and then start the REPL like this (still in the project root):
scala -classpath target/classes
Now you can import com.lambdaworks.crypto.SCryptUtil.
This only works because the project doesn't have any runtime dependencies, though—in other cases you may need either to add other things to the classpath, to build a JAR with the dependencies baked in (e.g. with the Maven Assembly plugin), or to use the mvn scala:console goal of the Maven Scala plugin.

Scala - sbt, main.scala and custom package

I use SBT 0.11.0. It requires that all sources must be placed in:
[project]/src/main/scala/
However I want a custom package such as:
[project]/src/com/test/...
I can leave things as SBT requires, then specify in every source file the custom package:
package com.test
But I got some problems with Eclipse auto-features such as import statements, generating new classes... Eclipse always adds main.scala before com.test. Is there something I can do to solve this?
Edited
For example I have this:
[project]
src
main
scala
com
test
A.scala
package com.test
...
B.scala
package com.test
...
In B, I use some functions of A, I type it, then press Ctrl+Shift+O to let Eclipse import A. Eclipse does this:
import main.scala.com.test.A
But in A I set package to com.test (not main.scala.com.test).
This is small example. But when I have many source files, I need to refactor my code, things will be harder...
You are confusing source folders and packages!
sbt by convention uses the source folder src/main/scala for Scala code. Within this folder you can create whichever packages you want, e.g. a subfolder foo/bar with a file Baz.scala that contains the following code:
package foo.bar
object Baz {
...
}
If you use the eclipse plugin for sbt then you can tell sbt to generate an Eclipse project which has all the details configured to work correctly with sbt's project structure. All the details you need to add it to your sbt project and to run it are at the link above.

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.