0
votes

i write simple program using eclipse IDE

the code is following

package java_pr;

import java.io.Console;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTestHarness {

    /**
     * @param args
     */     
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Console console = System.console();
        if (console == null) {
            System.err.println("No Console !!");
            System.exit(1);
        }
        while (true) {
            Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));

            Matcher matcher = pattern.matcher("Enter input string to search: ");

            boolean found = false;

            while (matcher.find()) {
                console.format("I found the text" +
                        "\"%s\" starting at " +
                        "index %d and ending at index %d.%n",
                        matcher.group(),
                        matcher.start(),
                        matcher.end());
                found = true;
            }
            if (!found) {
                console.format("No match found.%n");
            }
        }
    }

}

when i try to run this program from command prompt, i got following error

Exception in thread "main" java.lang.NoClassDefFoundError: RegexTestHarness (wrong name: java_pr/RegexTestHarness) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$000(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: RegexTestHarness. Program will exit.

While running fine in eclipse

1

1 Answers

0
votes

I'm guessing this is just the way you're running from the command line.

~/tmp# ls
java_pr
~/tmp# ls java_pr
RegexTestHarness.java RegexTestHarness.class
~/tmp# java -cp . java_pr.RegexTestHarness
Enter your regex:

This should work correctly for you