Making jar files available to the scala REPL - scala

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.

Related

cannot add a jar to scala repl with the :cp command

If I issue:
$ scala -classpath poi-3.9/poi-3.9-20121203.jar
scala> import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFSheet
Everything works ok, but if instead I issue:
$ scala
scala> :cp poi-3.9/poi-3.9-20121203.jar
Added '/home/sas/tmp/poi/poi-3.9/poi-3.9-20121203.jar'. Your new classpath is:
".:/home/sas/tmp/poi/poi-3.9/poi-3.9-20121203.jar"
Nothing to replay.
scala> import org.apache.poi.hssf.usermodel.HSSFSheet
<console>:7: error: object apache is not a member of package org
import org.apache.poi.hssf.usermodel.HSSFSheet
^
Am I missing something?
No one seems to mention this but the correct solution is using the :require command:
scala> :require foo.jar
Added 'foo.jar' to classpath.
Another way to get jar files imported, in this case by default, is to add links in $SCALA_HOME/lib to the jar files of interest. Then every time you enter REPL, such linked jars will be uploaded.
For instance, for poi-3.9-20121203.jar, consider
(cd $SCALA_HOME/lib && ln -s full_path_to_poi_jar/poi-3.9-20121203.jar)
As noted in the comments, this is a known issue. You could use an alternative REPL implementation such as Ammonite.

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.

creating and using standalone scalaz jar without sbt

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.

Scala : trying to get log4j working

Scala newb here (it's my 2nd day of using it). I want to get log4j logging working in my Scala script. The script and the results are below, any ideas as to what's going wrong?
[sean#ibmp2 pybackup]$ cat backup.scala
import org.apache.log4j._
val log = LogFactory.getLog()
log.info("started backup")
[sean#ibmp2 pybackup]$ scala -cp log4j-1.2.16.jar:. backup.scala
/home/sean/projects/personal/pybackup/backup.scala:1: error: value apache is not a member of package org
import org.apache.log4j._
^
one error found
I reproduce it under Windows: delimiter of '-classpath' must be ';' there (not ':'). Are you use cygwin or some sort of unix emulator?
But Scala script works anywhere without current dir in classpath. Try to use:
$ scala -cp log4j-1.2.16.jar backup.scala
JFI: LogFactory is a class of slf4j library (not log4j).
UPDATE
Another possible case: broken jar in classpath, maybe during download or something else. Scala interpreter does report only about unavailable member of the package.
$ echo "qwerty" > example.jar
$ scala -cp example.jar backup.scala
backup.scala:1: error: value apache is not a member of package org
...
Need to inspect content of the jar-file:
$ jar -tf log4j-1.2.16.jar
...
org/apache/log4j/Appender.class
...
Did you remember to put log4j.jar in your classpath?
Had Similar issue when started doing Scala Development using Eclipse, doing a clean build solved the problem.
Guess the Scala tools are not matured et.
Instead of using log4j directly, you might try using Configgy. It's the Scala Way™ to work with log4j, as well as configuration files. It also plays nicely with SBT and Maven.
I asked and answered this question myself, have a look:
Put it under src/main/resources/logback.xml. It will be copied to the right location when SBT is doing the artifact assembly.

"is not a member of package" error when importing package in Scala with SBT

(Relative beginner here, please be gentle...)
I've got a Scala program that I can build with sbt. I can (from within sbt) run compile and test-compile with no errors. I've defined a package by putting package com.mycompany.mypackagename at the top of several .scala files. When I do console to get a Scala REPL, this happens:
scala> import com.mycompany.mypackagename._
<console>:5: error: value mypackagename is not a member of package com.mycompany
import com.mycompany.mypackagename._
Any variation of this also fails. When I just do import com.mycompany I get no problems.
I thought that running the Scala console from within sbt would properly set the classpath based on the current projects? What (completely obvious) thing am I missing?
I ran into this same problem, and then I realized I was running scala 2.10.0 on commandline, and IDEA was using Scala 2.9.2. So the fix was to change both to use the same version, and:
sbt clean
What will happen if you import actual class name instead of wildcard.
import com.mycompany.mypackagename.ActualClassName