Reputation: 1796
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.
Upvotes: 2
Views: 416
Reputation: 340060
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:
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.
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:
javax.xml.transform.Source s = new javax.xml.transform.Source() ;
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.
Upvotes: 3
Reputation: 8557
You can also search Maven library by full class name by fc
operator at search.maven.org
eg.
fc:javax.xml.transform.stream.StreamResult
https://search.maven.org/search?q=fc:javax.xml.transform.stream.StreamResult
Of course one class can be found in many library...
Upvotes: 2
Reputation: 1323
There is something called java standard library
- this means that a lot of things are automatically avaibable to you and you don't have to add anything to pom
file.
Maven is used for adding external libraries
that are not included and shipped with your java.
Easiest way to find out if you need to add anything to pom
file is to use good IDE
(for example Intellij
) that provides support for maven. It should mark any libraries that are missing - then add those to your pom
file.
And you still need import everything you need to use for each *.java file.
Upvotes: 2