user225269
user225269

Reputation: 10913

How to include libraries in Java without using an IDE

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.

Upvotes: 37

Views: 93787

Answers (6)

Will Hartung
Will Hartung

Reputation: 118661

All of the other posters are spot on; you just need to add the JAR file to your classpath.

Java offers many mechanisms for setting the classpath, including via the command line, via an environment variable, and through setting it in the MANIFEST.MF of an executable Java JAR file.

These are all a pain in the neck to manage. It's good to know the technique, and understand the basics. But it's really a bad idea to actually use them.

You should do this.

First, put all of your Java libraries in a single place on your system. C:\java\libraries, or whatever. Someplace that you remember, and someplace accessible by all of your projects.

Next, name all of your libraries using their version numbers. If you using Log4j v1.4.1, then put the JAR file in a log4j-1.4.1 directory in your library area. This gives you "free" library versioning.

Finally, learn Ant. For simple projects, Ant is simple. Use the Ant build.xml file to compile, test, and run your application.

Why? Several reasons.

Because once it's set up, adding a new library to your project is trivial; you add a line to your build.xml file. Ant lets you more easily handle simple abstractions (like where all of your libraries are located).

The build.xml file is self-contained. If you use, say, an environment variable for the classpath, then the classpath for one project may be different from that of another. That means resetting the environment variable. Continue this and you'll end up swearing at some "new problem" where it "worked before" when it's because you had your classpath set wrong. Set it once in the build.xml file, and forget it.

Ant is portable. It runs the same on Windows, on Linux, on Mac, on AS/400, it runs everywhere that Java runs, unlike shells scripts or BAT files.

It's lightweight. Simple Ant scripts are simple. They don't bring a lot of baggage with them, and you can always make them scary complicated. It's much simpler than Maven for just builds.

Most IDEs support Ant directly. If you decided to go back to an IDE, most can simply use your Ant build file with minimal configuration.

This is how you solve your classpath problem with Notepad++. Setting the classpath works, but it doesn't go far enough. It's a pain to administer and manage. Learning the basics of Ant will take you much farther with minimal work.

Upvotes: 7

user1249380
user1249380

Reputation:

You should put them on your classpath, like

java -classpath someJar.jar YourMainClass

And, of course, you can do the same for javac.

If you need to have more than one JAR file or directory on your classpath, you'll need to use your platform's default path separator. For example, on Windows,

java -classpath someJar.jar;myJar.jar YourMainClass

On a side note, you might find it easier to use an IDE to manage this sort of stuff. I've personally used just my slightly scriptable editor and have managed fine. But it's good to know how to do this stuff by the command line.

Upvotes: 4

Simon David Kelly
Simon David Kelly

Reputation: 259

In addition to Bala R's post, adding multiple files and locations is perfectly 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 ':'.

Upvotes: 19

Derek Beattie
Derek Beattie

Reputation: 9478

Make sure the JAR file is in your classpath and you have the import statement.

Upvotes: 1

Bala R
Bala R

Reputation: 108957

Use:

javac -classpath external.jar myClass.java

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

Upvotes: 43

f0ster
f0ster

Reputation: 558

Put the JAR files in your classpath. classpath is an environment variable.

Upvotes: 2

Related Questions