1

I have compiled the following java file, and added the file path to the classpath. But after calling the class it shows some errors. As I'm new in Java, please help me.

MyQueue2.java

import java.util.PriorityQueue;
import java.util.*;

public class MyQueue2 {
  Comparator<Double> c;
  PriorityQueue<Double> PQ;

  public MyQueue2() {
    c = new Comparator<Double>(){
            public int compare(Double o1, Double o2){
              if(o2 > o1) {
                return -1;
              } else if(o1 > o2) {
                return 1;
              } else {
                return 0;
              }
            }
        };
    PQ = new PriorityQueue<Double>(1000,c);
  }

  public void addElement(double d) {
    PQ.add(d);
  }

  public double removeElement() {
    return(PQ.remove());
  }
}

My commands in MATLAB:

import java.util.PriorityQueue;
import java.util.*;
import MyQueue2;
methods('MyQueue2') %

Methods for class MyQueue2:

MyQueue2       equals         hashCode       notifyAll      toString       
addElement     getClass       notify         removeElement  wait    

a = MyQueue2

Java exception occurred:
java.lang.NoClassDefFoundError: MyQueue2$1

    at MyQueue2.<init>(MyQueue2.java:9)

Caused by: java.lang.ClassNotFoundException: MyQueue2$1

    at java.net.URLClassLoader$1.run(Unknown Source)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(Unknown Source)

    at com.mathworks.jmi.CustomURLClassLoader.findClass(ClassLoaderManager.java:760)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

    ... 1 more

b = MyQueue2    % again

Java exception occurred:
java.lang.NoClassDefFoundError: MyQueue2$1

    at MyQueue2.<init>(MyQueue2.java:9)

Please help me to solve the problem.

2
  • I am not matlab guru, but I have some suggestion for you :1. add a package name; 2. build a jar file to include both class file; 3. put the jar in the classpath. If you can still find the MyQueue2 class, that should be ok for MyQueue2$1.class Commented Aug 6, 2013 at 9:54
  • BTW,Add comment like this, do not add answer Commented Aug 6, 2013 at 9:57

1 Answer 1

1

After you compile the MyQueue2.java , you should get 2 class file MyQueue2.class and MyQueue2$1.class--which is for the anonymous class generated for Comparator , did u put them all in the classpath ? I guess you forgot this.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I have added the new $1.class file in the same forlder. But It shows the same error

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.