0

I am trying to write a program for LSD (Least Significant Digit) Count sort in Java to sort strings of fixed length by count sort method applied at a particular position of string. It compiles fine but at run time, it gives java.lang.NoClassDefFoundError. I have tried searching net for it but have not received a solution so far. So, I would be very grateful if someone could point out my mistake. Thanks in advance!

public class LSD
{
    public static void LSDsort(String[] a, int W) // fixed-length W strings
    {
        int R = 256;
        int N = a.length;
        String[] aux = new String[N];

        for (int d = W-1; d >= 0; d--)
        {
            int[] count = new int[R+1];
            for (int i = 0; i < N; i++)
                count[a[i].charAt(d) + 1]++;
            for (int r = 0; r < R; r++)
                count[r+1] += count[r];
            for (int i = 0; i < N; i++)
                aux[count[a[i].charAt(d)]++] = a[i];
            for (int i = 0; i < N; i++)
                a[i] = aux[i];
        }
    }

    public static void main (String[] args)
    {
        String[] arr = {"11AG3EP04", "11AG30022", "11CS10023", "11EC10015",
        "12CS3EP01", "10BT3FP01", "14NA10004", "13MF10012"};
        LSDsort(arr, 9);

        for (String s: arr) System.out.println(s);
    }
}
Neha
  • 325
  • 4
  • 14
  • How do you run it? Is it in your class path? Does it repeat when using IDE (Eclipse for example)? – amit Apr 27 '15 at 12:03
  • http://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java – Albert Apr 27 '15 at 12:19
  • Amit, I was trying to run it using command prompt (Windows) - javac LSD.java and java LSD. There the problem occurs. Moreover, I tried it again using NetBeans and the problem does not show. Can't really understand, why? – Neha Apr 27 '15 at 12:42
  • Albert, thanks for the reference. I also went through a few resources explaining the causes of the error, yet I could not figure out what I could do further to do away with the error. – Neha Apr 27 '15 at 12:44

2 Answers2

1

Is LDS class in a package?

If so, it must be run from package's base directory. I mean, if package is foo.bar, it must be run from foo's parent directory:

java -cp . foo.bar.LSD

If LSD has no package, it should work the way you did, run from directory where LSD.class is. You could try

java -cp . LSD 

Although I think it should work without -cp, just like @meskobalazs comments:

The default value of the class path is ".", meaning that only the current directory is searched

Albert
  • 1,156
  • 1
  • 15
  • 27
1

The normal error when Java does not encounter a main class, is something like that (my Java produce French error message... which does not help):

$ java  Foobar
Error: unable to find or load main class Foobar

You should try to recompile your java file using javac: it should produce a LSD.class.

javac LSD.java

Then try running it again:

java -cp . LSD

Also, provide the full stack trace, not just the error message. It might contains more informations.

NoDataFound
  • 11,381
  • 33
  • 59
  • Thanks java -cp . LSD works. But still I have this little doubt. The LSD.java file is stored in Documents (without any package) and the LSD.class file also gets produced in Documents on compiling, which is the current directory. Then why should it not work without -cp . ? – Neha Apr 27 '15 at 15:03