Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no access to a java static method from scala

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

like image 736
lolveley Avatar asked Aug 26 '13 09:08

lolveley


3 Answers

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.

like image 69
mikołak Avatar answered Oct 21 '22 03:10

mikołak


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.

like image 32
monnef Avatar answered Oct 21 '22 03:10

monnef


You have to import application.launcher._

like image 33
Luís Guilherme Avatar answered Oct 21 '22 03:10

Luís Guilherme