36

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user225269
  • 10,743
  • 69
  • 174
  • 251

6 Answers6

40

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • 1
    Exception in thread "main" java.lang.NoClassDefFoundError: Quizzer Caused by: java.lang.ClassNotFoundException: Quizzer at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: Quizzer. Program will exit. – user225269 Feb 25 '11 at 03:46
  • does your main class have a package name? – Bala R Feb 25 '11 at 03:52
  • @StackOverflowError - you also need a `-classpath` argument on the `java` command. – Stephen C Feb 25 '11 at 03:57
  • If you compile in directory that .java file is in you use that: `java -classpath external.jar:com.mycompany myClass` – Halil İbrahim Oymacı Jul 18 '16 at 08:33
  • Do I need to type exactly `-classpath` or do I need replace "classpath" with an actual path like `E:\some folder\my_lib.jar`?? – parsecer Sep 16 '16 at 23:50
  • @parsecer - Not sure what you are asking. If you don't understand this Answer, I recommend that you read the Oracle (web) manual pages for the `java` and `javac` commands for your platform. Don't forget to follow the link to the page that explains the classpath and read that as well. – Stephen C Sep 14 '20 at 08:08
19

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
7

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Will Hartung
  • 115,893
  • 19
  • 128
  • 203
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
f0ster
  • 549
  • 3
  • 12
  • 1
    What I am really trying to say is that if you rely on the `CLASSPATH` environment variable to find JARs, there is a risk that the `CLASSPATH` settings for one Java application with conflict with the settings needed for another one. This can be mitigated, but it is liable to trip you up. A better way (less error prone) is to either write a wrapper script (or custom launcher) to launch the app, or turn it into an "executable" JAR which deals with its own classpath, and run it using `java -jar ...` – Stephen C Sep 11 '20 at 02:40
  • @StephenC sorry, got frustrated... ended up going with shebang java source. `FOO=bar java...` is technically an env var, but in my head it's a benign parameter. env stands for environment... modifying a large environments to manipulate large tenants, is the bad idea... i would argue `CLASSPATH=. java` is no worse than `java -classpath .` – Ray Foss Sep 11 '20 at 02:59
  • I would agree with that. But it is not how your typical Linux / Windows / Java beginner would set CLASSPATH. – Stephen C Sep 11 '20 at 03:26
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Derek Beattie
  • 9,429
  • 4
  • 30
  • 44