Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include Java libraries to scala project

Tags:

scala

xstream

I'm totally new to scala. I want to serialize scala objects to xml using the java xstream library. (Is this a good idea or is there a better way?)

How to install the library so that i can import:

import com.thoughtworks.xstream.io.{HierarchicalStreamReader, HierarchicalStreamWriter}

now thoughtworks is not defined.

Is there a url to add in the build.sbt? or is there a location to copy the xstreams.jar?

like image 805
spitzbuaamy Avatar asked Oct 21 '25 02:10

spitzbuaamy


2 Answers

To add to @GamingFelix answer about scala-xml, SBT allows to easily include Java libraries into your Scala project.

When adding libraryDependencies to your project, you would usually write something like this:

libraryDependencies ++= "com.somecompany" %% "someproject" % "1.0.0"

This will attempt to resolve the following Maven project:

com.somecompany:someproject_2.12:1.0.0

Note the _2.12!

To import a Java project, you have to instead write:

libraryDependencies ++= "com.somecompany" % "someproject" % "1.0.0"

Note the single % instead of the double %%!

Now, SBT will try to resolve the Maven project:

com.somecompany:someproject:1.0.0

Which is the one you are looking for.

So, in your case write:

libraryDependencies += "com.thoughtworks.xstream" % "xstream" % "1.4.11.1"

When searching on Maven, you will actually be given such import statements right away:

https://search.maven.org/artifact/com.thoughtworks.xstream/xstream/1.4.11.1/jar

like image 171
Markus Appel Avatar answered Oct 23 '25 01:10

Markus Appel


There is actually very good support for xml in the standard scala library. I think you can read more about it here: https://github.com/scala/scala-xml All you need to do is to add the import in your class

import scala.xml

If you really want to use the java library instead. You can probably import it by adding the dependency into your build.sbt.

(Edit to include this part of the answer by ygor)

Check out mvnrepository.com/artifact/com.thoughtworks.xstream/xstream/… There is a "SBT" tab with the line, which you need to add to build.sbt.

like image 43
GamingFelix Avatar answered Oct 23 '25 01:10

GamingFelix