tags:

views:

64

answers:

2

Hi........

I am interested in sending tweets using Java program.I wrote the following prgram.It doen't show any error in compiling.But while executing it shows

"Exception in thread "main" java.lang.NoClassDefFoundError: hi
Caused by: java.lang.ClassNotFoundException: hi
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: hi.  Program will exit."

This is my code..

package twitter;
//import java.lang.object;
import net.unto.twitter.*;
import net.unto.twitter.TwitterProtos.Status;
public class hi
{
  public static void main(String args[])
  {

    Api api = Api.builder().username("usename").password("password").build();
    api.updateStatus("This is a test message.").build().post();

  }
}

Can anybody help me ..Pls

A: 

This error shows that you didn't start the application correctly. Could be a typical classpath problem, because you need a java library plus your own clode. Try this on a console/terminal:

(1) navigate to the root folder of you application (./twitter/hi.class should be a valid file from your actual path)

(2) execute the application with:

java -cp .;path/to/twitter/lib/twitterlib.jar twitter.hi

(replace path/to/twitter/lib/twitterlib.jar with the real path and lib name)


So, back to the basics:

You have class named twitter.hi, which needs to be stored in the file ./twitter/hi.java.

Then you must compile the java file to a file ./twitter/hi.class

When this is done, run the application with

java -cp .:hello_twitter/java-twitter.jar twitter.hi
Andreas_D
Hi...Thanks for ur reply..I have executed thisjava -cp hello_twitter/java-twitter.jar.HelloBut still same problem..Can u help me pls
Saranya.R
Do it exactly as I said in my answer. The classpath (-cp) needs to include the current path (`.`) **plus** the path to your twitter library. Not adding the current path will lead exactly to your error! (Maybe the `;` inside the `-cp` argument is not valid on your system - if it complaints, try `:` instead)
Andreas_D
java -cp .:hello_twitter/java-twitter.jar HelloI this u want me to do?
Saranya.R
Is the above syntax right?I m getting the same error
Saranya.R
is `./twitter/hi.class` a valid file at your current path where you execute the java command? And your class name is `twitter.hi` (according to your question) and not `Hello` as written in your last comment.
Andreas_D
hello_twitter/hi.javaThis is my path
Saranya.R
It will be much grateful if u solve the error...I m struggling
Saranya.R
+1  A: 

Class named 'hi' is within the package twitter. Include the package name while executing the class. Try this java -cp .:hello_twitter/java-twitter.jar twitter.hi

Varunkumar Nagarajan