Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to include libraries in java without using an IDE

Tags:

java

libraries

How do I import libraries in my java program without using an IDE like Netbeans? In Netbeans I do it this way: enter image description here

How can I achieve the same thing by just using notepad++ or programmer's notepad. As much as possible I don't want to use Netbeans because it would be overkill since I'm only working on simple projects.

like image 914
user225269 Avatar asked Feb 25 '11 01:02

user225269


People also ask

How do I add a library to a Java project?

Right-click the Java project to which you want to add a library, and select Properties from the menu. Select Java Build Path, and click the Libraries tab. Click the Add Library button, and choose the appropriate Java EE Library. Click Next to view the library contents, and click Finish.

How do I access Java library?

There are two main steps to accessing the functionality provided by an external library: Make sure the library is available to the Java compilation step—javac—and the execution step—java—via the classpath (either the -cp argument on the command line or the CLASSPATH environment variable).

How do I download Java libraries?

Step 1: Right-click the project and select “Build Path » Add Libraries…”. Step 2: Dialog the window that pops up, select “User Library” and click the “Next” button. Step 3: Select the user libraries you want to add and click “Finish”.

Do we have libraries in Java?

One of the key features of Java is that it has a feature-rich and vast Core library. While the Standard Java library is powerful, you will need other Java libraries in professional Software Development.


2 Answers

javac -classpath external.jar myClass.java 

EDIT:

If your main class is in a package

package com.mycompany; public class myClass { ... ... 

then

you'll need

javac -classpath external.jar com/mycompany/myClass.java  

and

to run

java -classpath external.jar com.mycompany.myClass 
like image 122
Bala R Avatar answered Sep 19 '22 18:09

Bala R


In addition to @StackOverflowException's post adding multiple files and locations is prefectly ok too...

javac -cp location1/;location2/;file1.jar;file2.jar fileToCompile 

Notes::

-cp and -classpath are the same thing. If you're on Solaris (and some other UNIX flavors) change the ';' to ':'

like image 34
Simon David Kelly Avatar answered Sep 21 '22 18:09

Simon David Kelly