Error in executing - java

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

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

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

Related

Getting error when importing json jar in my java file in linux

This is my java code
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PlutoMake {
public static void main(String[] args) throws JSONException {
}
}
and I compile it with this and it compiles without error
javac PlutoMake.java -cp java-json.jar
Now I copied this project from eclipse from windows, and using that jar it worked fine. I could access all the json functions. But here on linux, when I run it, I get
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
If I remove the throws JSONException then I can run it. So for some reason I can import the jar but I can't access any of its functions/exceptions without crashing my program...
Jar file is from the answer from this link Importing JSON into an Eclipse project
Does anyone know how to fix this?
Thanks
Add the jar file to the runtime classpath
java -cp java-json.jar:. PlutoMake
When you have create project in eclipse on linux you need configure buildpath inserting the jar

Compiling and running a java program in command prompt

I have written a simple hello world program given below
package helloworld;
public class helloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
I'm using Windows 7 and have set my CLASSPATH and PATH variables in the environment variables window as
C:\Program Files\Java\jdk1.6.0_24\bin;
The program is located at
C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld
So, basically when i try to run it in my command prompt, the following happens
C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld>javachelloWorld.java
C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld>java helloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: helloWorld (wrong name: helloworld/helloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: helloWorld. Program will exit.
The program seems to be compiling fine and does not throw any errors. But when i try to run it, it says than exceptions have occurred.
I tried looking up on Google thinking that setting my PATH and CLASSPATH was wrong, but I don't seem to find any issues with that.
In case your helloWorld.java file is in a package, you can run as follows :
C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\
java packageName.helloWorld
Run the code from outside the helloworld package directory i.e from src directory (C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src)
Also check if the name of your java file is the same as your class name (helloWorld.java)
Also it seems that the name of your package is helloworld (all in small letters) however the folder name is helloWorld (note that W is capital) both the names need to match.
You need to make sure you are in the directory containing your helloWorld.class file when you issue the command
java helloWorld

SNMP4J.jar - java.lang.ClassNotFoundException

I'm trying to check some SNMP example, so
i've downloaded snmp4j.jar
compiled SNMP.java using javac -cp snmp4j.jar SNMP.java (it's OK)
tried to start it using java -cp snmp4j.jar SNMP, but
it reports:
root#comp:~/workspace_c/SNMP# java -cp snmp4j.jar SNMP
Exception in thread "main" java.lang.NoClassDefFoundError: SNMP
Caused by: java.lang.ClassNotFoundException: SNMP
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: SNMP. Program will exit.
UPDATE:
And when I import snmp4j.jar to the java project, it works normally in ECLIPSE...
UPDATE#2: main part of SNMP.java
import org.snmp4j.CommunityTarget;
...
public class SNMP {
public static void main(String[] args) {
...
}
}
what i'm doing wrong? it's first time i've met snmp4j so i know noting about it
If I understand correctly, SNMP is your example class, which isn't inside the jar. In that case, you need to add to the classpath both the jar and your current directory:
java -cp .:snmp4j.jar SNMP
As far as I can see you are trying to run your application with SNMP4J.jar (upper case) but when you compiled your application you probably used lower case version.
Your prompt shows that your are using unix OS that has case sensitive file system. So, check the case of the JAR file name. I believe that all letters are in lower case.

Why won't this project run NetBeans, but it does in Eclipse

I have built a small program for school that runs perfectly when i run it through eclipse, but if I run the same code in NetBeans I get this error:
java.lang.NoClassDefFoundError: myrunnable/Main
Caused by: java.lang.ClassNotFoundException: myrunnable.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: myrunnable.Main. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
How do i fix this?!
I have not used netbeans but from what I can see from your exception looks to me JRE may not be in the classpath. Have you checked that? For example when you create a project in eclipse it will automatically add JRE to the classpath.
OR
may be you need to explicitly compile/build before you can run. In eclipse it is auto too. When you run a simple.java it would have created the corresponding simple.class in the bin output folder.
Check those two areas you will be fine I think
Is there a main method defined in the class?
public static void main(String[] args) {
}
Also, is your Java JDK properly registered inside Netbeans?

Could not find main class error when running a java wsdl client

I have written the following code, which is meant to be a client wsdl for http://www.nanonull.com/TimeService/TimeService.asmx?WSDL:
package time;
class Client {
public static void main(String args[]){
TimeService service = new TimeService();
TimeServiceSoap port= service.getTimeServiceSoap();
String result = port.getTimeZoneTime("UTC+10");
System.out.println("Time is "+result);
}
}
But when I try to run it with java I get the following:
C:\Program Files\Java\jdk1.6.0_22\bin>java client.Client
Exception in thread "main" java.lang.NoClassDefFoundError: client/Client
Caused by: java.lang.ClassNotFoundException: client.Client
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: client.Client. Program will exit.
Does this error mean I should be importing any classes?
The main class you wrote is time.Client, but you are trying to run client.Client. Better run it like this:
java time.Client
If this does not help, then you have a classpath problem - java cannot find the main class in the classpath. Set the classpath with -classpath option:
java -classpath classes-directory;list-of-jar-files time.Client
You need to set the classpath:
C:\Program Files\Java\jdk1.6.0_22\bin>java -cp . client.Client

Categories