-1

i have create the java class below. This class uses twitter4java api for java-twitter communication.

import twitter4j.*;
import twitter4j.auth.AccessToken;

import java.util.List;

public class SearchTweets {

    public static String API_Key = new String("xxxxxxxxxxxxxxxxx");
    public static String API_Secret = new String("xxxxxxxxxxxxxxxxx");
    public static String Access_Token = new String("xxxxxxxxxxxxxxxxx");
    public static String Access_Token_Secret = new String("xxxxxxxxxxxxxxxxx");

    public static void main(String[] args) {

        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(API_Key, API_Secret);
        AccessToken accessToken = new AccessToken(Access_Token, Access_Token_Secret);
        twitter.setOAuthAccessToken(accessToken);

        try {
            Query query = new Query("paok");
            QueryResult result;
            do {
                result = twitter.search(query);
                List<Status> tweets = result.getTweets();
                for (int i=0 ; i < tweets.size() ; i++)  {
                    Status s = tweets.get(i);
                    if (s.getPlace() != null) {
                        System.out.println("@" + s.getUser().getScreenName() + " - " + s.getPlace().getName());                     
                    }
                }
            } while ((query = result.nextQuery()) != null);
            System.exit(0);
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to search tweets: " + te.getMessage());
            System.exit(-1);
        }
    }
}

I have create it in eclipse. Now i want run it on a web site with php. I use shell_exec. First i create a simple java class for testing and all running ok.

After i go run my SearchTweets class and i can't. I export my project in a jar from eclipse. But when go run it i take this from terminal

itsoum@itsoum-Inspiron-3542:/opt/lampp/htdocs/tweet$ java -jar ilias.jar Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/TwitterException at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2625) at java.lang.Class.getMethod0(Class.java:2866) at java.lang.Class.getMethod(Class.java:1676) at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486) 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:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ... 6 more

Any idea what goes wrong?

1 Answers1

0

Your program refers to external library (twitter4j), you would need to reference this external lib to run your program

You have two options:

  • include all jar files from the lib directory into the manifest of your jar file (you can use relative paths there)
  • Specify everything (including your jar) on the command line using -cp: java -cp ilias.jar:<path to twitter4j.jar> SearchTweets.Main
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • First of all i thanks you for answer. Can you tell the specific command line order for run my SearchTweets? I need many twitter4j jars. Must I re-compile from terminal or no? – Ilias Tsoumas Sep 29 '15 at 12:48
  • you do not need to recompile - if you have many `jar` file you can include them all with `java -cp ilias.jar:lib/*.jar SearchTweets.Main` assuming all your jar files are in the lib/ folder under your project structure – Frederic Henri Sep 29 '15 at 12:54
  • Also how see if i have include all jars files into the manifest? What exactly is manifest? – Ilias Tsoumas Sep 29 '15 at 12:56
  • Error: Could not find or load main class TwitterTest.Main https://drive.google.com/file/d/0BxuSpi5N7V7fbFRqNW1lQmFOY0U/view?usp=sharing – Ilias Tsoumas Sep 29 '15 at 13:01
  • to be short, [manifest.mf is a text file](https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html) which contain information about the files packaged in a JAR file. if your specific lib refers to others jar files, you can indicate in it. If you're not so familiar with Jar and manifest file creation, you can stick with option 2 – Frederic Henri Sep 29 '15 at 13:01
  • is your class TwitterTest or SearchTweets, in your question you put SearchTweets – Frederic Henri Sep 29 '15 at 13:04
  • now i will try with TwitterTest... of course i change it when i try with your answer – Ilias Tsoumas Sep 29 '15 at 13:08
  • did you rebuild your jar then ? – Frederic Henri Sep 29 '15 at 13:26
  • man thnx. I recreate .jar from eclipse and all is ok. Now i want give some arguments from php on my jar. Is it possible? What i must change on my class and basic on shell_exec("java -jar ilias2.jar"); for give in jar arguments? – Ilias Tsoumas Sep 29 '15 at 13:42
  • yes you certainly can `java -cp ilias.jar:lib/*.jar SearchTweets.Main arg1 arg2 arg3` you would retrieve those values in the `args` variable of main method – Frederic Henri Sep 29 '15 at 13:46