获取库以在NetBeans中正确导入 [英] Getting a library to import properly in netbeans

查看:152
本文介绍了获取库以在NetBeans中正确导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想弄乱我刚刚下载的名为stdlib.jar的库(从 http://introcs.cs.princeton.edu/java/stdlib/)

So I want to mess around with a library I just downloaded called stdlib.jar (downloaded from http://introcs.cs.princeton.edu/java/stdlib/)

因此我将库添加到项目中,当我检查项目时,它在netbeans中看起来像这样:

So I added the library to the project, and when I inspect the project it looks like this in netbeans:

因此,似乎库已正确设置.

So it seems that the library is set up properly.

但是在我的项目中,当我尝试使用一种库类时,它看起来像这样.

But then in my project when I try to use one of the library classes, it looks like this.

将鼠标悬停在顶部的import语句上, expected "."
而且,如果我将导入语句更改为import stlib.StdAudio ,则表示该库不存在.
有人知道问题出在哪里吗?

Mousing over the import statement at the top, says expected "."
And If I change the import statement to say import stlib.StdAudio it says that the library does not exist.
Any body know what the problem is?

推荐答案

该库的所有源代码(这很不寻常:我第一次遇到此问题)都在默认程序包中,这意味着您不必如果您的班级也在默认包中,则导入任何内容.只需使用类名即可.

All the source code of this library (which is pretty unusual: first time I encountered this) is in the default package, which means you don't have to import anything if your class is also in the default package. Just use the class name.

StdAudio.play(tone);

只需确保它在类路径中,但是我认为这是如果我从NetBeans中查看您的屏幕截图.

Just make sure it is in the classpath, but I think it is if I look at your screenshot from NetBeans.

更新:看来我错了.在页面底部,您可以找到以下内容:

Update: It seems like I'm wrong. At the bottom of the page, you can find this:

.如果我使用命名包来构造代码,则编译器将无法 不再访问stdlib.jar中的库.为什么不呢?
A.库 stdlib.jar中的文件位于默认"程序包中. 在Java中,您无法访问 命名包中默认包中的类.如果需要 使用我们的标准库和命名包,您必须将 像在此解决方案中一样,将它们打包在一个程序包中.

Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar. Why not?
A. The libraries in stdlib.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our standard libraries with a named package, you must put our libraries in a package, as in this solution.

因此,您必须将这些类的包musicplayer复制到您的项目中.

So, you have to copy the classes into your project its package musicplayer.

这篇关于获取库以在NetBeans中正确导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆