简体   繁体   中英

Import custom java class in scala REPL

I am new to Scala, and as a matter of fact I'm quite an ignorant in Java as well (and I am not interested in going any deeper in my Java knowledge). The problem is that I am using a Java API that's only available for Java (the Freeling Java API).

Now, I have created a java class call Analyzer with only one static public method call analyze which takes a String of words (all parts of a sentence) and returns a java.util.ArrayList of String, in which each string has the word, lemma and tag of each of the words of this sentence (using freeling).

Now, I understand you can use a java class inside a scala app seamlessly. I declared the package (named analyzer) and compiled "Analyzer.java" to "Analyzer.class". My idea is to load this class "Analyzer" into the scala REPL (and another time into a scala source file) and use it from the scala REPL to see if it works.

I mean, I want to do something like this in the Scala REPL:

scala> import analyzer.Analyzer
scala> val result: java.util.ArrayList[String] = Analyzer.analyze("The cat eats fish")
scala> println(result.get(0))
The the DT

The problem is that when I try to import the package, it gives me an error:

scala> import analyzer.Analyzer
<console>:7: error: not found: value analyzer
   import analyzer.Analyzer

Even though I start scala with the -classpath option pointing to the directory where Analyzer.class is.

Is this possible? How should I do it?

import only makes the content of a namespace available without the requirement to qualify it with the package. In order for the compiler to find your package it must be on the class path. If you package your Java classes into a .jar file, you can add that file dynamically to the REPL using the special :cp path-to-jar command. Alternatively, you can put the directory of the Java class on the class path when launching Scala ( -cp my-path ).

Since you write you used the -classpath option, perhaps you used the wrong path. If your Java class is in package analyzer , you must add the path to the location containing the directory analyzer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM