148

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 (I found out 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

I'm using JCreator LE.

Gaurav Dadhania
  • 5,167
  • 8
  • 42
  • 61
  • 16
    Why is there a downvote? In my opinion, it's a good question and there are interesting information in the answers! (+1) – guerda Jan 20 '09 at 07:50

5 Answers5

197

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.Classname;

public class mysource {
    public static void main(String[] argv) {
    ......
   }
}

First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;

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

Please note the difference in using : and ; while compiling

  • 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
    
Quazi Irfan
  • 2,555
  • 5
  • 34
  • 69
GabrieleV
  • 2,303
  • 2
  • 16
  • 9
  • 4
    Could you explain this notation a little more? This fixed my problem, but unfortunately I have NO IDEA what the semicolon/colon means in this particular case. – cemulate Apr 27 '12 at 23:50
  • 2
    This semicolon / colon story just costed me the full day. At least I found it now, thanks! – Maxim Zubarev May 24 '13 at 23:54
  • 2
    @ChaseMeadors The [semicolon](http://technet.microsoft.com/en-us/library/cc772047.aspx) / [colon](http://www.gnu.org/software/bash/manual/bashref.html#index-PATH) essentially means "and" here. It's separating `.` from `org.example.jar`. – duozmo May 26 '14 at 19:04
  • If you're skimming, heed a distinction from most `*PATH` variables: .jar files [need to be individually named](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html). – duozmo May 26 '14 at 19:13
  • 1
    I think it's confusing to use the package name as the jar name. How about just `example.jar`? – tgdavies Feb 18 '22 at 03:02
  • @tgdavies Yes, the answer is confusing and looks wrong. The name of the JAR file is irrelevant to the *import* statement. See: https://stackoverflow.com/a/25730253/13312580 – mateleco Jan 19 '23 at 08:33
37

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 org.xml.sax.SAXException;

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. It would definitely enable us to help you further.

And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,

java -jar my_program.jar

you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.

Community
  • 1
  • 1
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • old, I know, but actually there is no need to `import` anything - it just avoids having to write the fully qualified class name who ever the class is used, allowing just the simple class name (`SAXException`) - kind of just importing the namespace – user85421 Jul 27 '19 at 20:29
16

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
  • 103,016
  • 27
  • 158
  • 194
8

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
  • 25,101
  • 4
  • 35
  • 56
6

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194