tags:

views:

3396

answers:

5

I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it(since I came to know yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar". The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.

The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php

Thanks for putting up with me.

Edit: I'm using JCreator LE. Thanks for the lightning fast responses guys.

Edit: Thanks guys, finally added the .jar file to the classpath and got it working. Thank you.

+3  A: 

You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.

workmad3
+4  A: 

Not every jar file is executable.

Now, you need to import the classes, which are there under the jar, in your java file. For example,

import java.util.Map;

If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. I would definitely enable us to help you further.

And if you are not using any IDE, then please look at javac -cp option.

Adeel Ansari
+1  A: 

As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)

If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.

Jon Skeet
classpath env variable is useful if you are using scripts or something like firedaemon to run your app. It's only a pain when you use it globally :)
workmad3
I still like to see it explicitly.
Jon Skeet
I agree, I like to see it explicitly.
J3r3myK
+3  A: 

You need to add the jar file in the classpath. To compile your java class:

javac -cp .;jwitter.jar MyClass.java

To run your code (provided that MyClass contains a main method):

java -cp .;jwitter.jar MyClass

You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.

kgiannakakis
A: 

Let's Say we need to use the class

Classname

that is contained in the jar file

org.example.jar

And your source is in the file

mysource.java

Like this:

import org.example.Classename;
public class mysource {
    public static void main(String[] argv) {
    .....
   }
}

First, as you see, in your code you have to import the classess you need:

import org.example.Classename;

Second, when you compile the source, you have to reference the jar file.

Please note the difference in using

:

and

;

If you are under a unix like operating system:

javac -cp '.:org.example.jar' mysource.java

If you are under windows:

javac -cp .;org.example.jar mysource.java

After this, you obtain the bytecode file

mysource.class

Now you can run this.

If you are under a unix like operating system:

java -cp '.:org.example.jar' mysource

If you are under windows:

java -cp .;org.example.jar mysource
GabrieleV