0

I am trying to add all the jar files in the directory below but I get

Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/TwitterException error.

I am sure there is TwitterException class inside the .jar file. Is there any error in my classpath syntax ?

mahmut@ubuntu:~/Desktop/Java/TwitterDeneme/bin$ java -classpath "~/twitter4j-2.2.6/lib/*.jar:." GetHashtag
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/TwitterException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
    at java.lang.Class.getMethod0(Class.java:2685)
    at java.lang.Class.getMethod(Class.java:1620)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:488)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
Caused by: java.lang.ClassNotFoundException: twitter4j.TwitterException
    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:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 6 more

I am using ubuntu and jdk 1.7. I tried the same command in windows and it works flawlessly. Is there anything special to do related to Ubuntu OS?

Brian Agnew
  • 254,044
  • 36
  • 316
  • 423
mdemirst
  • 484
  • 5
  • 8

1 Answers1

8

The * syntax for classpaths is rather limited. Try:

java -classpath "~/twitter4j-2.2.6/lib/*:." GetHashtag
Mathias Schwarz
  • 6,833
  • 18
  • 27
  • 1
    By the way, on Windows you need to use ; instead of : for the command to work. – Mathias Schwarz Aug 13 '12 at 10:41
  • +1 but see http://stackoverflow.com/questions/1237093/using-wildcard-for-classpath – Brian Agnew Aug 13 '12 at 10:44
  • Tried both lib/* and lib/*.jar but no luck! Is it related to wildcard character or any other problem? I also tried to include jar files individually but it gives the same error either. Actually, I compiled the code in windows and just moved class file to ubuntu. Am I missing anything? – mdemirst Aug 13 '12 at 11:11
  • Are you sure that class is in any of your jar files? – Mathias Schwarz Aug 13 '12 at 11:15
  • I found the problem. ~ character inside quotes causes problem. I wrote "/home/mahmut/twitter4j-2.2.6/lib/*:." instead of "~/twitter4j-2.2.6/lib/*:." and it worked. Thanks! – mdemirst Aug 13 '12 at 12:18