简体   繁体   English

在Scala REPL中导入自定义Java类

[英]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). 我是Scala的新手,事实上,我对Java也一无所知(并且我不希望对Java知识有任何更深的了解)。 The problem is that I am using a Java API that's only available for Java (the Freeling Java API). 问题是我使用的Java API仅适用于Java(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). 现在,我创建了一个Java类调用分析器,其中仅包含一个静态公共方法调用analyst,该分析接受一个字符串的单词(句子的所有部分),并返回String的java.util.ArrayList,其中每个字符串都有一个单词,这个句子中每个单词的引理和标签(使用自由化)。

Now, I understand you can use a java class inside a scala app seamlessly. 现在,我知道您可以在scala应用程序内无缝使用Java类。 I declared the package (named analyzer) and compiled "Analyzer.java" to "Analyzer.class". 我声明了程序包(名为分析器),并将“ Analyzer.java”编译为“ 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. 我的想法是将此类“ Analyzer”加载到scala REPL中(另一次加载到scala源文件中),并从scala REPL中使用它来查看其是否有效。

I mean, I want to do something like this in the Scala REPL: 我的意思是,我想在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. 即使我使用-classpath选项启动scala,也指向了Analyzer.class所在的目录。

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. import仅使名称空间的内容可用,而无需使用包对其进行限定。 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. 如果将Java类打包为.jar文件,则可以使用特殊的:cp path-to-jar命令将该文件动态添加到REPL。 Alternatively, you can put the directory of the Java class on the class path when launching Scala ( -cp my-path ). 另外,您可以在启动Scala( -cp my-path )时将Java类的目录放在类路径中。

Since you write you used the -classpath option, perhaps you used the wrong path. 自编写以来,您使用了-classpath选项,也许您使用了错误的路径。 If your Java class is in package analyzer , you must add the path to the location containing the directory analyzer . 如果Java类在包analyzer ,则必须将路径添加到包含目录analyzer的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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