So i am using gradle to get dependencies from maven central which is working fine. I just don't know how to import them to my actual java file.
How do i found out the name to import it?
at the top of my java file i have to write
import <name>
How do i find the name?
Thank You.
According to your comments. You have to import the packages, which are contained within the library, not the library itself. There is no guarantee, this package names are the same as group or artifact id of the library. To get know that package names, usually you may use a javadocs for the library. Or just simply let your IDE to make it for you, them you're trying to use some classes from that lib.
Alternatively, you can use some off-sites, like mvnrepository.com, where you may find your library and take a look at the packages list within it. For example, description for Apache Commons Lang library, where you can see the "Packages" section with all the packages within the lib. You may import them, just like:
import org.apache.commons.lang3.*;
One more solution, is to unzip a jar and take a look into it's content to determine the packages structure.
Related
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.
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.
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.
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...
I need to import this library into my project but I don't know how to do it because there isn't a link or something like that.
How can I do it.
Here is the library: https://github.com/nomanr/ZoomTextView
In other libraries I used this code:
implementation 'com.github.barteksc:android-pdf-viewer:2.3.0'
Just copy the ZoomTextView.java file in your project from the link:-
https://github.com/nomanr/ZoomTextView/blob/master/app/src/main/java/noman/zoomtextview/ZoomTextView.java
and use that in creating your view. So, there is no need of implementing anything in your gradle.
We are evaluating FlatBuffers as a potential solution for packing and unpacking various data payloads. I have built flatc.exe, constructed schemas for our data, and generated Java code from the schemas. I am now trying to use the generated code.
This overview states:
Then you can include both FlatBuffers and the generated code to read or write a FlatBuffer.
And does so as follows in the example:
import MyGame.Example.*;
import com.google.flatbuffers.FlatBufferBuilder;
1) Should the generated code be imported as a new module/Java Library, dependency, or something else? How is this done?
2) The import of com.google.flatbuffers.FlatBufferBuilder also does not resolve. Anyone know if this reference has changed?
Appreciate any help you can provide to an Android neophyte on how to import these items.
Thanks.
FlatBuffers doesn't come with integrations for any specific IDE's or package managers (which is something we should fix). For now the easiest is to copy the contents of FlatBuffers java/ folder to where-ever you keep your project's Java code, along with the generated code (in its package directory).
I want to use the StdDraw package, and I've tried many different ways of importing it.
Of course I tried:
import StdDraw;
But, when I look at the documentation from Princeton here, it shows that StdDraw is part of Objects, so I try this:
import java.lang.Object.StdDraw;
However, this results in an error:
error: cannot find symbol in
import java.lang.Object.StdDraw;
I saw this question here but it does not answer this question either.
How do I import StdDraw? thank you.
if you want to use StdDraw you must have
either the sources
or the classes (best zipped up as jar)
as preferred way you use the sources (see http://introcs.cs.princeton.edu/java/15inout/). it says there "To use these libraries, download StdIn.java, StdOut.java, StdDraw.java, and StdAudio.java into the same directory as your program. "
once you did this the imports should be working.
NOTE: all four files are not in packages, so you should 'download' them into the 'standard' package. That means you have to download them to the root package of your project.
by the way: don't import import java.lang.Object.StdDraw; but do just import import StdDraw;
First of all check encoding of your IDE. It should be set to UTF-8. It is important if you are using MS Windows operating system.
Then create StdDraw.java class in the same package as the package of your program you are writing. Remove class declaration, leave only package declaration.
Then visit this page: https://introcs.cs.princeton.edu/java/stdlib/StdDraw.java .
Copy all its contents (Ctr-A, Ctrl-C) and then paste it into StdDraw.java file you created previously.
StdDraw.java has its own main method so try to run it in order to check that the library works correctly. You should see a window with four strange figures :) .
Don't touch StdDraw.java anymore. Now you can easily import StdDraw library and refer to its methods with name of the class.
Enjoy
I have about 100 jar files and I think I want to make a library with them. What is the best way to do this? How does importing work with this. Do I still have to ask for each class or do I just refer to the new library?
More detail
I have the GeoTools9.4 package (in a zip). It has about 100 jar files. When I import these into my project in eclipse, it takes each jar file and stuffs it in and clutters up my structure. So I think I want a library (or a package or a class) I am not sure what the terminology is here.
More detail on how to call the classes in the new library.
Right now here is how I call the classes
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
If I put all of these jar files in a library can I replace the above lines with a new import like
import org.geotools.local
or do I not need to change the way they are called?
I propose you to use a Maven for this stuff.
Maven is a greay build tool, that could take care of problems, like adding dependency jars to a project. Also, GeoTools support Maven and have a nice manual for it (http://docs.geotools.org/latest/userguide/tutorial/quickstart/maven.html)
About last question - when you'll add this libraries, full name of these classes will be the same, so you must import and use them as you import them right now.
This is not usually refered to as "calling" the classes, but rather importing the classes meaning that they become available to the class that uses them.
No matter how you have those classes (in many jars or a single jar) you still need to have the import statements in the beggining of the class file for the source to be compiled
I am not sure how Eclipse "clutters up" your structure. You can place all the jars in a single folder e.g. lib and then import them into your eclipse project from that folder. If you mean that the jars show up in the left pane then there are filters that can hide them. In Eclipse there is the concept of a Working set where you can select what it would be visible and what not.
I hope it helps