简体   繁体   English

关于eclipse(java)中如何使用外部库

[英]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.我是 Java 的完全初学者,使用 eclipse 甚至在安装了那些正确的外部库之后,(我将它们安装到我的构建路径中,它们进入了我的引用库部分)这将使我的工作变得容易我不能将它们用于一些理由。

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.我用它来导入这个库的所有类,当我试图在我的程序中使用这些类时,由于某种原因它不起作用。如果我尝试使用 print() 方法,它会给我以下错误这个库的 class IOconsole 的一个方法。

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.编辑 1:这是我的程序。

   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 .因为IOConsole中的print()方法似乎是static

You need to have an object of ShortPrint, like so你需要有一个 object 的 ShortPrint,像这样

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

I am guessing you are trying to call print like this:我猜你正试图这样调用print

ShortPrint.print("Hello");

which would only work is print was a static function of ShortPrint这只会工作是print是 ShortPrint 的ShortPrint

Another possibility is that you do not inherit ShortPrint from IOConsole , this the IOConsole.print is not accessible from ShortPrint另一种可能性是您没有从IOConsole继承ShortPrint ,这IOConsole.print无法从ShortPrint访问

UPDATE: after OP added code on usage, the suggestion is to add the import更新:在 OP 添加使用代码后,建议添加导入

import acm.io.*;

as the IOConsole class resides in the acm.io package. Then change the call to因为IOConsole class 驻留在acm.io package 中。然后将调用更改为

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

as print() is not a static member of IOConsole因为print()不是 IOConsole 的IOConsole成员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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