wix
wix

Reputation: 13

Java: NoClassDefFoundError when adding package statement

The java source code is as below:

package test;

public class DotMain {
    public static void main(String... args) {
        String s1 = "abcdex";
        String s2 = "ac";
        boolean[] r1 = new boolean[26];
        for (char c : s1.toCharArray())
            r1[c - 'a'] = true;
        boolean contained = true;
        for (char c : s2.toCharArray()) {
            if (!r1[c - 'a']) {
                contained = false;
                break;
            }

        }
        System.out.println(contained);
        System.out.println(s1 + s2);
    }
}

If there is no "package test;" the corresponding class file will behave correct. But when I add "package test;" the exception as stated in the title occured. Is there something wrong?

Upvotes: 0

Views: 89

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499800

Chances are you're not building it properly or not running it properly.

Build it like this (filling in the path information appropriately):

javac -d . path/to/DotMain.java

Run it like this:

java test.DotMain

(You can change the output directory specified by -d of course, at which point you should either add that directory to the classpath or change to that directory before running.)

Upvotes: 1

Related Questions