1

I am new to Emacs and I want to use org-mode to summarize the coding exercise and key points while learning Java. I am currently using IntelliJ to evaluate algorithmic coding questions I did on Leetcode and because they are usually lightweight, I wish I can do it directly in org-mode.

My problem is, in my IntelliJ, I have a package called com. Excercise under which I have a class called test to evaluate the function I wrote in another class, called Solution.

Basically, the structure of the file looks like the following:

com.Exercise

  • Test
  public class Test {
      public static void main(String[] args) {
      Solution test = new Solution();
      System.out.println(test.majority(new int[]{2,1,1,3,1,4,5,6}));
      }
  }
  • Solution
public class Solution {
    public List<Integer> majority(int[] array) {
        Map<Integer,Integer> map = new HashMap<>();
        map.put(array[0],1);
        for(int i = 1; i<array.length; i++){
            if(!map.containsKey(array[i])){
                if (map.size()>=2) {
                    Set<Integer> temp = new HashSet<>(map.keySet());
                    for(Integer val:temp){
                        map.put(val,map.get(val)-1);
                        if(map.get(val) == 0){
                            map.remove(val);
                        }
                    }
                } else {
                    map.put(array[i],1);
                }
            } else{
                map.put(array[i],map.get(array[i])+1);
            }
        }
        List<Integer> list = new ArrayList<>();
        for(Integer key:map.keySet()){
            if(map.get(key)>0){
                list.add(key);
            }
        }

        return list;
    }
}

this would allow me to write comments between the test block and solution block and also sometimes I just want to list several test cases and make comments on each one of them.

I followed the instruction here by first configuring the init.el file and I tried to evaluate the function majority in org-mode by writing the following .org file

#+begin_src java  :results output :classname com.package.Test :imports com.package.Solution
  public class Test {
      public static void main(String[] args) {
      Solution test = new Solution();
      System.out.println(test.majority(new int[]{2,1,1,3,1,4,5,6}));
      }
  }
#+end_src


#+begin_src java :classname com.package.Solution :imports java.util.*
public class Solution {
    public List<Integer> majority(int[] array) {
        Map<Integer,Integer> map = new HashMap<>();
        map.put(array[0],1);
        for(int i = 1; i<array.length; i++){
            if(!map.containsKey(array[i])){
                if (map.size()>=2) {
                    Set<Integer> temp = new HashSet<>(map.keySet());
                    for(Integer val:temp){
                        map.put(val,map.get(val)-1);
                        if(map.get(val) == 0){
                            map.remove(val);
                        }
                    }
                } else {
                    map.put(array[i],1);
                }
            } else{
                map.put(array[i],map.get(array[i])+1);
            }
        }
        List<Integer> list = new ArrayList<>();
        for(Integer key:map.keySet()){
            if(map.get(key)>0){
                list.add(key);
            }
        }

        return list;
    }
}
#+end_src

In the java source code documentation, it says:

The package and classname of a class can be defined in the source code block or by the :classname header argument or both. If they are defined in both places, then they must match. :classname can just be a classname, like Greeter or it could contain the package name, such as com.package.Greeter. This example names the class Greeter and puts it in the com.package package.

However, the evaluation block throws error messages as the following:

com.package.Test.java:1: error: class Test is public, should be declared in a file named Test.java
public class Test {
       ^
com.package.Test.java:3: error: cannot find symbol
    Solution test = new Solution();
    ^
  symbol:   class Solution
  location: class Test
com.package.Test.java:3: error: cannot find symbol
    Solution test = new Solution();
                        ^
  symbol:   class Solution
  location: class Test
3 errors
Error: Could not find or load main class com.package.Test
Caused by: java.lang.ClassNotFoundException: com.package.Test

I am wondering how exactly is the evaluation block works in org-mode? Is it possible to resolve issues other than only writing a single block? (making the majority a static method and putting it in the Test class...)

I hope my question makes sense!

1 Answer 1

0

This is really a Java question, not an Emacs one. I think the easiest is to declare your Test class as a public static nested class, that is, move its whole definition (plus the added keyword static) into the body of class Solution:

public class Solution {
  public static class Test { ... };
  public List<Integer> majority(int[] array) { ... }
}

You then call Solution.Test.main(...);

7
  • The reason why I am asking this question is that I don't want to structure the code this way... if I know how to reference different classes across blocks it will give me more flexibility to write comments between blocks
    – JoZ
    Dec 23, 2021 at 1:03
  • The problem you need to address is a Java one: a top-level public class defined in a file with a different name. I suggested a way you might fix this. You say you do not want to structure the code this way, understandably, but you do not say how you do wish to structure your code. I can imagine good reasons why, but since I don't know what they are, I don't think I can help any further at this point. Can I suggest that you take this question to the Java StackExchange? Only once you have resolved the class/file naming issue will you be able to deal with any remaining Org mode issue(s). Dec 23, 2021 at 21:46
  • Hi, I read over the orgmode.org/worg/org-contrib/babel/languages/… documentation and I supposed if specifying com.pacakge. the :classname will know com.pacakge represent the name of the package and will be able to locate the Solution.class file under ~/com/package/ path as this is how my IDE organized my .java script. But each time I evaluate the block, the .java file will have name com.package.Solution.java and it won't be in the path ~/com/package/ as I created which seems different from what the manual suggested.
    – JoZ
    Dec 24, 2021 at 21:21
  • I am wondering if I have missed any configuration for org babel? It seems I cannot even complete a lot of simple sample block from the link I just given
    – JoZ
    Dec 24, 2021 at 21:23
  • Again, the issues you are facing are Java issues, not Emacs ones. I am assuming that you are new to Java programming. The issues you are facing have nothing to do with Org, Babel, or Emacs. You need first to modify your Java code, at least to the point that javac (the Java compiler) can successfully compile your .java file to a .class file, before you try to integrate your code with Org/Babel. The Java StackExchange is the place to ask how. Dec 24, 2021 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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