Skip to content
Advertisement

Maven: how to find dependencies

Maybe I am misunderstanding Maven’s dependency principles but this is my question:

I have a little Java program that requires these imports

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;

Now instead of importing these at the top of the code, I would just go into the POM file of my Maven project and add the dependencies. But on https://mvnrepository.com/ how do I find the correct imports? Is there another way besides looking on that site?

Thank you.

Advertisement

Answer

Now instead of importing these at the top of the code, I would just go into the POM file of my Maven project and add the dependencies.

No. You are conflating two different things:

  • Managing dependencies (downloading and placing libraries within your project)
  • Using dependencies (calling the library’s classes and methods from within your code)

Managing dependencies

To use a library, you need to obtain a physical copy, a file, usually a .jar file. You can manually download a copy. Or you can use Maven or Gradle to download a copy on your behalf. The Maven or Gradle approach is generally recommended over the manual approach.

Once downloaded, you need to place the file where it can be found within your project. Again, you can do this manually, or you can use Maven or Gradle to make the file available to your project. Again, the Maven or Gradle approach is generally recommended over the manual approach.

Using dependencies

After having obtained and placed a copy of the library, you are ready to access its classes and methods.

👉 The catch is that the authors of that library may have named some of their classes and methods coincidentally with the same name as found in another library.

Imagine you want to use a class named Source, but two of your libraries have such a class:

  • javax.xml.transform.Source
  • com.example.awesome.Source

If you write in your code:

Source s = new Source() ;

… how does the compiler know which of the two classes you meant? 👈

To resolve the mystery, you either:

  • Write a fully-qualified class name.
    javax.xml.transform.Source s = new javax.xml.transform.Source() ;
  • Write an import statement.
    import javax.xml.transform.Source ;

The second approach, writing an import statement, usually makes for less typing and easier reading than the first approach of using fully-qualified names.

The word import is a bit of a misnomer, and was used for legacy historical reasons. Its use here does not involve any moving of anything anywhere. A better name would have been namespace, as in, specifying a defined domain of known names.

When reading this:

import javax.xml.transform.Source ;

… think this:

namespace javax.xml.transform.Source ;

… meaning: “Any use below of the word “Source” can be assumed to mean the Source class from the library whose package is javax.xml.transform

In its effort to find the class of that package you named, the Java Virtual Machine at runtime automatically looks through all the libraries you obtained and placed.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement