Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing library in Java

Tags:

java

I am new in Java and learning it by a book called "Thinking in Java". Author wrote one library called net to ease understanding. For example, print in place of System.out.println and likewise. 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

like image 379
nurgasemetey Avatar asked May 18 '26 02:05

nurgasemetey


1 Answers

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.

like image 115
akhilless Avatar answered May 19 '26 15:05

akhilless



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!