Where to put dependencies for scala - scala

I'm getting this compile error:
owner#PC ~/scala/fxml: scalac x.scala
x.scala:1: error: object asynchttpclient is not a member of package org
import org.asynchttpclient.*;
^
one error found
I figured I needed to download the .java files for org.asynchttpclient.* so I copied those to c:\classes and set CLASS_PATH to c:\classes but that didn't work.
Note: I know about sbt and maven but I just want to get scalac working.

The error is with the dependency for x.scala. You need to download the asynchttpclient jar if you don't have it. Then apply the following command to include it in compilation.
scalac -classpath "asynchttpclient.jar:other dependent jars" x.scala

It's CLASSPATH, not CLASS_PATH. You can also use -classpath ... as an option to scalac.

Related

Compiling with scalac does not find sbt dependencies

I tried running my Scala code in VSCode editor. I am able to run my script via spark-submit command. But when I am trying with scalac to compile, I am getting:
.\src\main\scala\sample.scala:1: error: object apache is not a member of package org
import org.apache.spark.sql.{SQLContext,SparkSession}
I have already added respective library dependencies to build.sbt.
Have you tried running sbt compile?
Running scalac directly means you're compiling only one file, without the benefits of sbt and especially the dependencies that you have added in your build.sbt file.
In a sbt project, there's no reason to use scalac directly. This defeats the purpose of sbt.

Scala project with maven in IntelliJ does not compile

I created a scala project and included Maven framework in it. After that the code doesn't compile and hence when I run the file, I end up getting the following error
Error: Could not find or load main class main.scala.TestSpark
Mark scala directory as Sources Root.

Compile single scala file with TypeSafe Activator

I have Activator installed. Which means I have a full SBT on my system. I don't want to create a brand new activator project. All I want to do is compile a single scala file as we used to do with the scalac command. How can I do this please? Thanks.
You go into the directory containing your scala file and type "sbt compile" on the command line.
To run the program, you type "sbt run"
see also
http://www.scala-sbt.org/0.13/tutorial/Hello.html

scalac's dependencyfile option doesn't work?

I was reading a previous stack overflow question about setting up ant tasks for scalac:
FSC recompiles every time
The weird thing is when I test the -dependencyfile option, I find no dependency file generated anywhere. I am testing this with ubuntu's scalac 2.9.1 and also with the official 2.10.2 jar in ant tasks. It seems the command-line utility and the ant task both take my input without complaining anything (they are grammatically correct?) though.
Am I using this feature in the wrong way?
E.g. from command-line:
scalac -dependencyfile scala_dependencies *.scala
from an ant tasks
<!-- inside a macro definition -->
<scalac destdir="#{destdir}" deprecation="yes"
force="yes" dependencyfile="scala_dependencies"
addparams="#{addparams}"
scalacdebugging="true">
<!-- src, classpath, patternset and etc -->
</scalac>
updates:
I added -make:transitive options to comand-line it does generate a file for me but I ran it for the second time, scalac simply crashed.
$ scalac -make:transitive -dependencyfile scala_dependencies *.scala
Recompiling 2 files
warning: there were 1 deprecation warnings; re-run with -deprecation for details
error: java.lang.NullPointerException
at scala.tools.nsc.io.Path$.apply(Path.scala:73)
at scala.tools.nsc.dependencies.DependencyAnalysis$$anonfun$saveDependencyAnalysis$1.apply(DependencyAnalysis.scala:33)
at scala.tools.nsc.dependencies.DependencyAnalysis$$anonfun$saveDependencyAnalysis$1.apply(DependencyAnalysis.scala:33)
at scala.tools.nsc.dependencies.Files$FileDependencies$$anonfun$emit$1$2$$anonfun$apply$6.apply(Files.scala:96)
at scala.tools.nsc.dependencies.Files$FileDependencies$$anonfun$emit$1$2$$anonfun$apply$6.apply(Files.scala:96)
at scala.collection.mutable.HashSet.foreach(HashSet.scala:72)
at scala.tools.nsc.dependencies.Files$FileDependencies$$anonfun$emit$1$2.apply(Files.scala:96)
at scala.tools.nsc.dependencies.Files$FileDependencies$$anonfun$emit$1$2.apply(Files.scala:96)
at scala.collection.mutable.OpenHashMap$$anonfun$foreach$1.apply(OpenHashMap.scala:221)
at scala.collection.mutable.OpenHashMap$$anonfun$foreach$1.apply(OpenHashMap.scala:219)
at scala.collection.mutable.OpenHashMap$$anonfun$foreachUndeletedEntry$1.apply(OpenHashMap.scala:226)
at scala.collection.mutable.OpenHashMap$$anonfun$foreachUndeletedEntry$1.apply(OpenHashMap.scala:226)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:38)
at scala.collection.mutable.OpenHashMap.foreachUndeletedEntry(OpenHashMap.scala:226)
at scala.collection.mutable.OpenHashMap.foreach(OpenHashMap.scala:219)
at scala.tools.nsc.dependencies.Files$FileDependencies.emit$1(Files.scala:96)
at scala.tools.nsc.dependencies.Files$FileDependencies.writeTo(Files.sca
la:103)
at scala.tools.nsc.dependencies.Files$FileDependencies$$anonfun$writeTo$1.apply(Files.scala:92)
at scala.tools.nsc.dependencies.Files$FileDependencies$$anonfun$writeTo$1.apply(Files.scala:92)
at scala.tools.nsc.dependencies.Files$class.writeToFile(Files.scala:163)
at scala.tools.nsc.Global$dependencyAnalysis$.writeToFile(Global.scala:498)
at scala.tools.nsc.dependencies.Files$FileDependencies.writeTo(Files.scala:92)
at scala.tools.nsc.dependencies.DependencyAnalysis$class.saveDependencies(DependencyAnalysis.scala:87)
at scala.tools.nsc.Global$dependencyAnalysis$.saveDependencies(Global.scala:498)
at scala.tools.nsc.dependencies.DependencyAnalysis$class.saveDependencyAnalysis(DependencyAnalysis.scala:32)
at scala.tools.nsc.Global$dependencyAnalysis$.saveDependencyAnalysis(Global.scala:498)
at scala.tools.nsc.Global$Run.compileSources(Global.scala:1022)
at scala.tools.nsc.Global$Run.compile(Global.scala:1038)
at scala.tools.nsc.Main$.process(Main.scala:106)
at scala.tools.nsc.Main$.main(Main.scala:123)
at scala.tools.nsc.Main.main(Main.scala)
for 2.10.2, the compiler complains:
warning: -make is deprecated: this option is unmaintained. Use sbt or an IDE for selective recompilation.
and I still can't find the generate file

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.