Java Question

How to import library in Java

I just found project in the Internet which I need to compile and understand how I can use those libraries;
But I just don't know how to compile it, because I don't understand how to include required libs.

I'm coding on Java just for three days, and I don't even understand how to include my own .java files to work together. (I was invoking bar() method from another Foo.java file like Foo.bar()).

I need somebody to tell me, or just give me a link to any quick guide how to do this without any additional soft (e.g. IDE). Just javac and java commands.

Answer

You are biting off a large piece here for someone that is just starting out.

After you make sure you know what a "package" is, you then need to understand the relationship between java packages and the directory structure of java source, class, and jar files; after that, we can introduce the concept of a "classpath". To attempt to short circuit that, I will attempt to tell you how to use these libraries by rote, so to speak, but it is difficult to tell how many things need to be explained when we can't talk about it. But I'll try.

https://github.com/sarxos/webcam-capture/blob/master/README.md contains a link to a zip file that purports to have all the necessary libraries in it for the project you mentioned. Can I assume you know what a Zip file is and how to expand one? You should download this one, then extract the jar file from its root and the two jar files in its lib directory. Put all three of those files in the directory where you intend to do execute javac.

In order to use a class from one of these (presumably you'll only be using classes from the webcam-capture jar), you'll need to know the package that file is in. For instance, the Webcam class is in com.github.sarxos, so to use that class, you would include the line

import com.github.sarxos.Webcam;

near the top of your own source file. Then you are allowed to use Webcam in your source, and the compiler will know that you mean the Webcam class in that package.

In order to compile this, you have to let the compiler know where to find the class files. The compile line would look something like this:

javac -cp .;webcam-capture-0.3.10.jar;bridj-0.6.2.jar;slf4j-api-1.7.2.jar MyClass.java

(This is Windows syntax; if you're on an *nix system, you use ":" instead of ";".) We are assuming MyClass is in the "default" package; if it is in some other package, you must put its package qualification in as well as its name. You can not set your default directory down to where the source file is and skip entering the package name, you need to compile it from the "root" of your package names. This, incidentally, will not only compile MyClass but all classes MyClass refers to. Assuming it is the 'main' class in a java application, and that you don't do anything fancy about instantiating files with Class.forName(), this javac command will compile all the classes necessary for your program.

Once you get it to compile without errors, you can run it with the same classpath string:

java -cp .;webcam-capture-0.3.10.jar;bridj-0.6.2.jar;slf4j-api-1.7.2.jar MyClass

So this covers at least two meanings of "including a library"; it gives you an example of telling the compiler and then the runtime where to find the class files in a (jar) library, and I mentioned the "import" statement which, though not required, is standard practice for referring to other classes whether they're in a library or in your own code.

Good luck.