The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Using Java class from Scala

2 replies on 1 page. Most recent reply: Feb 8, 2011 6:34 AM by George Berger

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Otfried Cheong

Posts: 5
Nickname: otfried
Registered: Dec, 2010

Using Java class from Scala Posted: Feb 7, 2011 6:53 AM
Reply to this message Reply
Advertisement
I cannot figure out this problem: I have a tiny Java class like this:
public class M1 {
  public static void main(String[] args) {
    System.out.println("Hello world");
  }
}

I compile it with "javac M1.java" to generate M1.class.

Now I can access this from scala like this:

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> M1.main(Array())
Hello world


However, when I create a tiny Scala script test.scala like this:

M1.main(Array())

then I get an error message:

$ scala test.scala
/opt/Strongspace/Mirror/Courses/DataStructures/code/01-recursion/test.scala:2: error: not found: value M1
M1.main(Array())
^


The crazy thing is that this works:

$ scala
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :load test.scala
Loading test.scala...
Hello world

scala>


Otfried Cheong

Posts: 5
Nickname: otfried
Registered: Dec, 2010

Re: Using Java class from Scala Posted: Feb 7, 2011 8:59 PM
Reply to this message Reply
It turns out that this has nothing to do with Java. It seems that scripts in Scala 2.8 cannot find compiled classes in the same directory, even though the interactive mode can.

Two files: M1.scala and M2.scala as follows:

object M1 {
def test(a : Int) {
println("Testing " + a)
}
}

and

M1.test(13)


I compile M1 using scalac M1.scala, resulting in M1.class and M1$.class in the current directory.

Running scala M2.scala results in this:

$ scala M2.scala
M2.scala:2: error: not found: value M1
M1.test(13)


However, this works:

$ scala
Welcome to Scala version 2.8.1.final (OpenJDK Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :load M2.scala
Loading M2.scala...
Testing 13


What also works is to replace the script M2.scala by a compiled scala class like this:

object M3 {
def main(args: Array[String]) {
M1.test(13)
}
}


Is this simply a bug?

George Berger

Posts: 24
Nickname: gcb
Registered: Jul, 2007

Re: Using Java class from Scala Posted: Feb 8, 2011 6:34 AM
Reply to this message Reply
Your example works okay for me:


$ cat M1.scala
object M1 {
def test(a : Int) {
println("Testing " + a)
}
}
$ cat M2.scala
M1.test(13)
$ scala -version
Scala code runner version 2.8.1.final -- Copyright 2002-2010, LAMP/EPFL
$ scalac M1.scala
$ ls
M1.class M1$.class M1.scala M2.scala
$ scala M2.scala
Testing 13
$


There is a difference in the scala we're using though. The version numbers are the same, but the jvms they use are different. Mine is using 1.5 while yours is 1.6. I don't know how to change that. The default java on my box is 1.6.


$ scala
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Server VM, Java 1.5.0_16).
...

$ java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Server VM (build 17.1-b03, mixed mode)

Flat View: This topic has 2 replies on 1 page
Topic: When will physical copies ship? Previous Topic   Next Topic Topic: PrintStream

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use