1

i want to run my java code with "java" command:

public class Statistics {

    public static void main(String[] args) throws IOException {

        .....
        .....

        final Runnable r = new Runnable() {
            @Override
            public void run() {
                .....
            }
        };

        scheduler.scheduleAtFixedRate(r, 0, 15, TimeUnit.MINUTES);
    }
}

I am getting this error:

Exception in thread "main" java.lang.NoClassDefFoundError: jsoup/Statistics$1
    at jsoup.Statistics.main(Statistics.java:37)
Caused by: java.lang.ClassNotFoundException: jsoup.Statistics$1
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

37 line: final Runnable r = new Runnable() {

kakajan
  • 2,614
  • 2
  • 22
  • 39
  • 3
    can you check you have that class in your classpath? – Peter Lawrey Jan 15 '15 at 21:03
  • possible duplicate of [Why am I getting a NoClassDefFoundError in Java?](http://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – Andy Brown Jan 15 '15 at 21:09
  • Odd that the issue seems to be it can't find it's own compiled anonymous class. – Andy Brown Jan 15 '15 at 21:13
  • Statistics.java:37 is points to code line: final Runnable r = new Runnable() – kakajan Jan 15 '15 at 21:14
  • Try recompiling. Perhaps, .class file got deleted from disk somehow? – Dima Jan 15 '15 at 21:17
  • the command which i use for running: java -cp lib/jsoup-1.7.3.jar:bin jsoup.Statistics – kakajan Jan 15 '15 at 21:19
  • And where are the files Statistics.class and Statistics$1.class located? – JB Nizet Jan 15 '15 at 21:23
  • thanks @JBNizet, i forget to upload Statistics$1.class, but i don't understand, what is the meaning of this class? I only uploaded Statistics.class – kakajan Jan 15 '15 at 21:28
  • Your Statistics class defines an anonymous inner class which implements Runnable. The compiler stores this class in Statistcs$1.class. If you had another anonymous inner class, it would be stored in Statistics$2.class. If you had a named inner class named Foo, it would be in Statistics$Foo.class. – JB Nizet Jan 15 '15 at 21:31

2 Answers2

0

Could it be that you exit the main() method before the scheduler have had the chance to execute? I.e. shouldn't you have your main() method waiting for the scheduler to finish, before returning?

keyoxy
  • 4,423
  • 2
  • 21
  • 18
0

The below is called an anonymous inner class. The Runnable interface normally could not be instantiated, but you are extending it on the fly to create a new class that implements Runnable.

final Runnable r = new Runnable() {
  @Override
  public void run() {
    .....
  }
};

Your class Statistics compiles to a class file called Statistics.class, while this anonymous class compiles to a class file called Statistics$1.class.

I suspect you moved the Statistics class file to a new directory location in order to run it using java. In that case, you must move its companion class file also. Or perhaps you mistakenly deleted Statistics$1.class.

Preferably you would not move the class files, but rather add the folder containing java.exe to your PATH environment variable in Windows.

gknicker
  • 5,509
  • 2
  • 25
  • 41