0

I have a super simple example where I have a list, a Java method and a call to this method. It looks like this:

#+name: animals
- dog
- cat
- guinea pig
- horse

#+name: does-contains-cat
#+begin_src java :results none
public boolean doesListContainsCat(List<String> list) {
    return list.contains("cat");
}
#+end_src

#+begin_src java :results value :var animalList=animals
doesListContainsCat(animalList);
#+end_src

Output → error: cannot find symbol doesListContainsCat(animalList)

When evaluating the first block it creates a Java file with a class and a main method containing my method… I know it is the expected behaviour but it is not the result I want. By keeping it as simple as that (if possible) I want to be able to display a result when evaluating the second block.

My other tries:

#+name: doesListContainsCat
#+begin_src java :results none :var list='("")
return list.contains("cat");
#+end_src

#+begin_src java :results value :var list=animals doesListContainsCat
return doesListContainsCat(list);
#+end_src

Output → error: incompatible types: unexpected return value

or with a #+CALL header:

#+CALL: doesListContainsCat(list=animals)

Output → error: incompatible types: unexpected return value

I know it's because the return type of the main method in Java is void.

How to make it work (and keep it simple) so that I can define a Java method inside a source block, call it later using another source block and display the result?

3
  • Have you looked at the Java-specific Babel documentation?
    – NickD
    Commented Sep 28, 2023 at 2:28
  • Since the first code block generates a separate class file, try to refer to your function the same way you would refer to any function in another class. 1. Use :classname header argument to set a class name within which your function is defined. 2. Make your function static. 3. Access to you function from the second code block using dot notation.
    – C. Aknesil
    Commented Sep 28, 2023 at 9:44
  • @C.Aknesil It doesn't work anyway since my first block does-contains-cat is encapsulated in a main method. @NickD Yes I have.
    – Nicryc
    Commented Sep 29, 2023 at 18:17

1 Answer 1

0

The best working solution I've found so far is:

#+name: animals
- dog
- cat
- guinea pig
- horse

#+name: does-contains-cat
#+begin_src java :results none :classname babel.java.A
public static void main(String[] args) {}
public static boolean doesListContainsCat(List<String> list) {
    return list.contains("cat");
}
#+end_src

#+begin_src java :results value :var animalList=animals[,0] :classname babel.java.B
return A.doesListContainsCat(animalList);
#+end_src

#+RESULTS:
: true
  • Defining two distinct classes with :classname and making a static call to my method as suggested by @C.Aknesil
  • Declaring an empty main method manually public static void main(String[] args) {} to force org-babel not to wrap my method doesListContainsCat in a main method
  • Using [,0] to tell org-babel to consider my list as a list and not a table

It's not the ideal solution but it works. I'm still looking for a better solution that doesn't involve the three workarounds listed above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.