How to import class using fully qualified name?

3.2k Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER
import _root_.java.io.FileInputStream

Or to simplify things, you might use an alias:

import _root_.java.{io => jio}
import jio.FileInputStream