Importing library in Java

Importing library in Java

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 -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:

javac -classpath .:D:libraries MyClass.java

Then in your source code you can just import the library the way the author does, i.e. 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. 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). Then the IDE will automatically add it to the classpath during the compilation.

P.S. 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. 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.

It is simple to import any library that is referenced in the Project properties. Add library in the project using properties then import that. But net it built-in library provided in JDK and JRE. You can use import java.net.*; to use classes in the net package.

Importing library in Java

Leave a Reply

Your email address will not be published.