简体   繁体   中英

Regarding how to use external libraries in eclipse (java)

I'm a complete beginner in Java using eclipse and even after installing those correctly external libraries,(I installed them in to my build path and they come in my referenced library section) which would make my job easy I can't use them for some reason.

import acm.*;

I used this to import all the classes of this library and when I tried to use those classes in my program, It didn't work for some reason.It gives me the following error if I try to use the method print() which is a method of the class IOconsole of this library.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method print(String) is undefined for the type ShortPrint

at ShortPrint.main(ShortPrint.java:5)

I don't know if I missed any steps but I'm pretty sure I have installed the libraries correctly,Just can't get them to use.

EDIT 1: Heres my program.

   import acm.*;

public class ShortPrint {
    public static void main(String []args) {
        print ("hello");

   }
}   

I believe you should change your import to:

import static acm.IOConsole.*

Since it appears that the print() method is static in IOConsole .

You need to have an object of ShortPrint, like so

ShortPrnt sp = new ShortPrint();
sp.print("Hello");

I am guessing you are trying to call print like this:

ShortPrint.print("Hello");

which would only work is print was a static function of ShortPrint

Another possibility is that you do not inherit ShortPrint from IOConsole , this the IOConsole.print is not accessible from ShortPrint

UPDATE: after OP added code on usage, the suggestion is to add the import

import acm.io.*;

as the IOConsole class resides in the acm.io package. Then change the call to

IOConsole cons = new IOConsole();
cons.print("hello");

as print() is not a static member of IOConsole

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM