no access to a java static method from scala
Asked Answered
M

3

7

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

Madame answered 26/8, 2013 at 9:41 Comment(1)
What's the error message?Furbelow
D
4

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.

Declassify answered 26/8, 2013 at 10:14 Comment(1)
@Madame : that's a shame. I assume you have "Build automatically" checked?Bakeman
C
3

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.

Carping answered 26/8, 2013 at 10:26 Comment(3)
hello, the package is application, the class name is launcher, and append is the method name, I'm sure.Madame
hello, I would like to post it but it's impossible because the program uses a library named javaexe which transform it into a service, and because of that it does not gain access to the console (no println allowed); I tried to intercept the error message with this:Madame
here is the code: def sors_tout_de_suite() { try { val fileWriter = new FileWriter("logs\\zz.txt", true) val bufWriter = new BufferedWriter(fileWriter) try{ application.launcher.append("SCALA") }catch{ case e:Exception => bufWriter.append(e.getMessage()) } bufWriter.close() } catch { case e: IOException => {} } }Madame
S
1

You have to import application.launcher._

Sen answered 30/7, 2014 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.