no access to a java static method from scala

7.7k views Asked by At

I created a program with java and scala mixed, but I am faced to an error while trying to call a java static method from scala.Here is the code:

object GestionBasesScala {

  def sors_tout_de_suite() {

    application.launcher.append("SCALA : exit")
  }
}

the append method of the launcher class is like this (in java):

public static void append(String text) {

    if (name_of_file != null && name_of_file != "") {
        BufferedWriter bufWriter = null;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(name_of_file, true);
            bufWriter = new BufferedWriter(fileWriter);
            // Ins�rer un saut de ligne
            bufWriter.newLine();
            bufWriter.write(text);
            bufWriter.close();
        } catch (IOException ex) {
              //     Logger.getLogger(TextFileWriter.class.getName()).log(Level.SEVERE,
            // null, ex);
        } finally {
            try {
                bufWriter.close();
                fileWriter.close();
            } catch (IOException ex) {
                // Logger.getLogger(TextFileWriter.class.getName()).log(Level.SEVERE,
                // null, ex);
            }
        }
    }

}

I don't see what the error could be.

olivier

3

There are 3 answers

1
mikołak On

If you're using Scala IDE/Eclipse, sometimes the in-editor compiler does not pick when static methods become created and/or updated.

Running "Clean..." on the project makes the error go away.

3
monnef On

application.launcher doesn't seem like a class name, are you sure it is? Shoudn't it be something like LauncherClass.append("SCALA : exit")?

EDIT1: After confirmation of the class name correctness I tried similar (a bit simplified) scenario, but I'm unable to reproduce behaviour described in Q. Following code works fine (did I miss something?):

package javastatic

object ScalaCaller extends App {
  def doStuff() {
     javastatic.JavaProvider.append("Scala here")
  }

  doStuff()
}
package javastatic;

public class JavaProvider {
    public static void append(String text) {
        System.out.println(text);
    }
}

The error message from compiler could help. Please consider posting it.

0
Luís Guilherme On

You have to import application.launcher._