how to include libraries in java without using an IDE
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
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 ':'
All of the other posters are spot on, you just need to add the jar 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.
What you should do is this.
First, put all of your Java libraries in a single place on your system. C:\java\libraries, or whatever. Someplace that you remember, 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 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. Ant lets you more easily handle simple abstractions (like where all of your libraries are located).
The build.xml 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 reseting 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, 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, 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.
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 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 command line.
put the jars in your classpath, classpath is an environment variable