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?
Your program refers to external library (twitter4j), you would need to reference this external lib to run your program
You have two options:
java -cp ilias.jar:<path to twitter4j.jar> SearchTweets.Main
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.