简体   繁体   English

从Java调用Scala代码?

[英]Call Scala code from Java?

I'm using Netbeans to write Scala and Java. 我正在使用Netbeans编写Scala和Java。

Netbeans generated a .jar file for the Scala project. Netbeans为Scala项目生成了一个.jar文件。 I tried importing that file in the Java project. 我尝试在Java项目中导入该文件。 But I couldn't import any class from that .jar file into my Java project. 但我无法将该.jar文件中的任何类导入到我的Java项目中。

I also tried importing scala-library.jar to the java project, and could import classes from that jar. 我也尝试将scala-library.jar导入java项目,并可以从该jar导入类。

I want to write my library in Scala, then expose a small interface that only involves Java stuff, then write a Java wrapper so that people can use it as a Java package. 我想在Scala中编写我的库,然后公开一个只涉及Java东西的小接口,然后编写一个Java包装器,以便人们可以将它用作Java包。 Is that possible? 那可能吗? How do I do it? 我该怎么做?

Thank you very much. 非常感谢你。

There should be no problem in doing this. 这样做应该没有问题。

  1. Have you verified (eg using WinZip or the jar utility) that your .jar file actually contains the relevant .class files? 您是否验证过(例如使用WinZip或jar实用程序)您的.jar文件实际上包含相关的.class文件? (use jar tvf mylib.jar to check) (使用jar tvf mylib.jar来检查)
  2. If you have verified that the correct .class files exist in your jar file, what is the runtime error you are seeing? 如果您已经验证了jar文件中存在正确的.class文件,那么您看到的运行时错误是什么? Is it a NoClassDefFoundError ? 它是NoClassDefFoundError吗? If so, which class cannot be found? 如果是这样,哪个class找不到? How are you referring to the (Scala) class in your (Java) code? 你是如何在(Java)代码中引用(Scala)类的?

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

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