0

I have a jar file named Stdlib.jar and I have already imported into the project that I'm currently working on (using Build path in Eclipse). I have checked that the .jar file appears in the "Reference Libraries", but I'm not able to call the method that lies in the class of the .jar library.

I searched a lot and here are my questions. Do I need to write a import statement in addition to the build path to use the method? If so, what is the syntax? (Because I did "add external jar", the .jar file is on my desktop. So should I write "import desktop/stdlib.jar;"?)

I used to do it on Linux and it was pretty easy with flag "cp- filepath". But when I came to Eclipse, I had no clue how to utilize an external library. Thank you very much for any kind of help!

lwang456
  • 55
  • 2
  • 3
  • 5
  • you need to `import` class in this `jar` to enable this class in this the file you enabled – Rugal Jan 09 '14 at 03:53
  • What class/method are your trying to call? (Also [double posting](https://stackoverflow.com/questions/21011048/how-to-call-methods-that-is-in-an-external-library) isn't generally liked) – MadProgrammer Jan 09 '14 at 03:54
  • `import [classname];` – takendarkk Jan 09 '14 at 03:56
  • Also, where are you getting the `Stdlib.jar` library from? – MadProgrammer Jan 09 '14 at 04:03
  • @MadProgrammer I guess this(http://stackoverflow.com/questions/13082299/how-to-import-a-jar-file-which-is-in-a-package) question asks essentially the same thing. But I need to call it from a jar rather than a package. – lwang456 Jan 09 '14 at 04:08

2 Answers2

2

I'm not sure what your JAR file is (sounds like a JavaINI interface to C Std-Lib), but in any case, when you add a JAR file to a Java project in any IDE (Eclipse, Netbeans, etc) to be able to use the functionality provided by the JAR you must import the desired classes from the desired packages.

The general syntax is like this:

// To just add a specific class:
import packagename.ClassName;

// Or simply add all the classes in the package:
import packagename.*;

And these imports should come at the top of your Java source file.

Then you can use the methods like this:

// For static methods:
ClassName.staticMethod(params);

// For non-static methods, first create an object:
ClassName obj = new ClassName(params);
// then call the desired methods:
obj.method(params);

Here is the "Using Packages" section of the Java-Tutorial: Using Packages

Also here are two similar question/answers on StackOverflow:

Community
  • 1
  • 1
Seyed Mohammad
  • 798
  • 10
  • 29
  • Thanks for answering. I'm confused by what do you mean by "import packagename.Classname" I just add the JAR using the buildpath feature but I don't think it is in any package or has any package name. – lwang456 Jan 09 '14 at 04:14
  • The JAR file you are using probably has some documentation for using it. Check out the docs and see what packages you should import. It seems to me you are very new to Java programming. If so, it's better you try to study and learn the basics first. The Java-Tutorial I have linked is a very good place to start. – Seyed Mohammad Jan 09 '14 at 04:17
0

Add the jar as reference and import the class or library into your class. Then create a object of a class if it is not static and access the method if it is public..

Damith
  • 1,982
  • 3
  • 28
  • 42
  • See http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java) and this http://stackoverflow.com/questions/7869006/import-a-custom-class-in-java – Damith Jan 09 '14 at 03:57