Question

I have a Scala library that is called from Java.

This is scala code.

package org.fun

object ScalaPower{
  def showMyPower(time:Int) = {
    (0 to time-1).mkString(", ")
  }
}

This is Java code.

import org.fun.*;

public class Test {
    public static void main(String args[]) {
       System.out.println("show my power: " + ScalaPower.showMyPower(3));       
    }
}

With this build script:

SCALA_LIB=~/bin/jar/scala/scala-library.jar

scalac -d ~/temp/java s.scala
javac -cp .:~/temp/java:$SCALA_LIB Test.java -d ~/temp/java
java -cp .:~/temp/java:$SCALA_LIB Test

I have errors.

Test.java:1: error: package org.fun does not exist
import org.fun.*;
^
Test.java:5: error: cannot find symbol
       System.out.println("show my power: " + ScalaPower.showMyPower(3));       
                                              ^
  symbol:   variable ScalaPower
  location: class Test
2 errors

Interestingly, I have no problem when I get the class path from environment variable.

SCALA_LIB=~/jar/scala/scala-library.jar
JP=.:~/temp/java:~/temp/java2:$SCALA_LIB

scalac -d ~/temp/java s.scala
scalac -d ~/temp/java2 s2.scala
javac -cp $JP Test.java -d ~/temp/java
java -cp $JP Test

Why is this? Do I have to always use environment variable for setup class path with -cp? Or did I make some mistakes? I checked with Mac OS X and Linux; both of them working with $JP, but not with direct path enlisting.

The code is copied from this post: How do you call a Scala singleton method from Java?

No correct solution

OTHER TIPS

I should have used environmental variable for a parameter to -cp. I checked with Mac OS X and Linux; both of them working with $JP, but not with direct path enlisting.

SCALA_LIB=~/jar/scala/scala-library.jar
JP=.:~/temp/java:~/temp/java2:$SCALA_LIB

scalac -d ~/temp/java s.scala
scalac -d ~/temp/java2 s2.scala
javac -cp $JP Test.java -d ~/temp/java
java -cp $JP Test
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top