Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import class using fully qualified name?

create file test1.scala with following code:

package test
import java.io.FileInputStream
object Foo 

create another file test2.scala with following code:

package test.java
object Bar 

Now compile as scalac test1.scala test2.scala:

We get the error:

test.scala:2: error: object io is not a member of package test.java
import java.io.FileInputStream
            ^
one error found

I think the error is because Scala thinks that java above refers to package test.java. How to resolve this problem apart from renaming the package?

like image 439
Jus12 Avatar asked Feb 19 '23 18:02

Jus12


1 Answers

import _root_.java.io.FileInputStream

Or to simplify things, you might use an alias:

import _root_.java.{io => jio}
import jio.FileInputStream
like image 131
Régis Jean-Gilles Avatar answered Feb 21 '23 21:02

Régis Jean-Gilles