简体   繁体   English

用Java导入库

[英]Importing library in Java

I am new in Java and learning it by a book called "Thinking in Java". 我是Java的新手,并通过一本名为“Thinking in Java”的书来学习它。 Author wrote one library called net to ease understanding. 作者写了一个名为net库来缓解理解。 For example, print in place of System.out.println and likewise. 例如, print代替System.out.println等。 So, how can I import this library? 那么,我该如何导入这个库?

UPDATE: 更新:
Author in his example does the following: 他的例子中的作者做了以下事情:

import static net.mindview.util.Range.*;
import static net.mindview.util.Print.*;

And I looked at his source codes and found build.xml in net folder 我查看了他的源代码,并在net文件夹中找到了build.xml

Basically, to compile your project with an externally provided library, you should add it to the classpath. 基本上,要使用外部提供的库编译项目,应将其添加到类路径中。 There are multiple ways to do it based on what tools you are using. 根据您使用的工具,有多种方法可以执行此操作。 If you go the "rough" way using text editor and javac (recommended for beginners), you can do it like this: 如果你使用文本编辑器和javac (推荐给初学者)采用“粗略”方式,你可以这样做:

javac -classpath .:/path/to/the/folder/containing/your/library MyClass.java

In your case if the folder net is in the folder D:\\libraries your compilation command will look like this: 在您的情况下,如果文件夹net位于文件夹D:\\libraries您的编译命令将如下所示:

javac -classpath .:D:\libraries MyClass.java

Then in your source code you can just import the library the way the author does, ie just copy past his code: 然后在您的源代码中,您可以像作者那样导入库,即只需复制他的代码:

import static net.mindview.util.Range.*;
import static net.mindview.util.Print.*;

public class MyClass {
.....
}

If you use an IDE (for example NetBeans) you just add the library to the project. 如果您使用IDE(例如NetBeans),则只需将库添加到项目中。 Just right-click on the Libraries in the Projects Window and choose Add JAR/Folder , then navigate to the folder containing the net library (folder D:\\libraries in my previous example). 只需在“ Projects Window右键单击“ Libraries ,然后选择“ Add JAR/Folder ,然后导航到包含net库的文件夹(前一个示例中的文件夹D:\\libraries )。 Then the IDE will automatically add it to the classpath during the compilation. 然后IDE将在编译期间自动将其添加到类路径中。

PS if you are a beginner in programming, I would recommend you avoiding Thinking in Java - it is meant for people wishing to broaden their knowledge at the post-beginner level. PS如果你是编程的初学者,我会建议你避免使用Java思考 - 它适用于那些希望在初学者级别上拓展知识的人。 Start with something like Java: How to Program by Deitel - it is written for beginners and does not use author-developed libraries confusing you as a beginner and hiding important language details from you. 从像Java这样的东西开始:Deitel如何编程 - 它是为初学者编写的,不使用作者开发的库让你混淆初学者并隐藏重要的语言细节。

It is simple to import any library that is referenced in the Project properties. 导入Project属性中引用的任何库都很简单。 Add library in the project using properties then import that. 使用属性在项目中添加库然后导入它。 But net it built-in library provided in JDK and JRE. net它内置的JDK和JRE提供的库。 You can use import java.net.*; 你可以使用import java.net.*; to use classes in the net package. 在net包中使用类。

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

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