简体   繁体   English

如何在Eclipse内的JAR文件中引用类和方法?

[英]How do I reference classes and methods in JAR file inside Eclipse?

Basically I wish to use the methods of a class within the Jar file, which looks like this: 基本上,我希望使用Jar文件中的类的方法,如下所示:

IDE屏幕截图

Can somebody please tell me what I need to import in order to use those methods and variables? 有人可以告诉我使用这些方法和变量需要导入什么吗?

You don't need to import anything. 您无需导入任何内容。

Jar files aren't imported , they are added to the classpath. Jar文件不会导入而是添加到类路径中。
From the screenshot you've posted, we can see that the myJar.jar file is included in your eclipse classpath, so there's nothing more to do there. 从您发布的屏幕快照中,我们可以看到myJar.jar文件已包含在Eclipse类路径中,因此,无需执行其他操作。

Classes are imported, if they are in a different package. 如果位于其他程序包中则将导入它们。
Your Examplew class is in the default package . 您的Examplew类在默认包中 BMIcalculator is also in the default package . BMIcalculator也在默认软件包中 Because they are the same package, you don't need to import it. 因为它们是相同的软件包,所以您无需导入它。

You should be able to simply make references to BMIcalculator from within Examplew . 你应该能够简单地做出引用BMIcalculator从内部Examplew Just try it. 去尝试一下。

Try compiling this code - it should work: 尝试编译此代码-它应该可以工作:

public class Examplew
{
    private BMIcalculator calc = new BMIcalculator();
}

You might get warnings about the unused private field, but you can ignore that for now. 您可能会收到有关未使用的私有字段的警告,但是您现在可以忽略它。

If that doesn't work for you, then please post the error, because it doesn't look like the problem is with your imports (or your classpath) 如果这对您不起作用,请发布错误,因为它看起来好像问题不在于您的导入(或您的类路径)

Quote from this question : 引用这个问题

You can't use classes in the default package from a named package. 您不能使用命名包中默认包中的类。

Prior to J2SE 1.4 you could import classes from the default package using a syntax like this: 在J2SE 1.4之前,您可以使用以下语法从默认包中导入类:

import Unfinished;

That's no longer allowed . 不再允许了 So to access a default package class from within a packaged class requires moving the default package class into a package of its own. 因此,要从打包的类中访问默认的包类,需要将默认的包类移动到自己的包中。

If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning. 如果您可以访问groovy生成的源,则需要进行一些后期处理,以将文件移动到专用包中并在其开始处添加此“ package”指令。

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

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