Is there a way to "redirect" packages?
Is there any possibility to tell Android to get the class-files from e.g. com.java.rmi instead of java.rmi?
Some additional libraries depend on this package so I repacked it with jarjar - otherwise dalvik-exc would occur.
Just change your import statements. If you are using Eclipse, you can just remove the java.rmi import lines and press Control-Shift-O (Organize Imports). It will ask you which package to import.
Edit: yes, you have to do this in all files that use said package.
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 have a lot of java files in my project so I decided to create packages and organize those files. After putting them in seperate packages like
com.myproject.android.activity
com.myproject.android.adapter
etc...
eclipse wants me to import the R file. From different SO questions I know "Never import the R file". However without that eclipse shows error messages that R.java is missing and wants to import it.
I already did a eclipse restart, clean, and android->fix with no success. My resource files are without errors. Is it safe to import the R file. Any suggestions?
Yes, you can import R files, they are sometimes needed to be imported, e.g. when you define them in a library and you need to use them in the package that uses that library.
You can also use full names - this may make the code more clear even if longer.
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
I found this question -> Import custom libraries in Java
And #Andy Thomas-Cramer said that the classes in "stdlib.jar" from "An introduction to programming in Java" have no packages, so they are in the default package.
Isn't this a bad practice? If you have something with no package the IDEs' auto-completion is quite slower. And also this means that we could not use any of the classes, in that jar, from classes with packages different then the default?
Can someone please tell me how we could deal with this?
EDIT:
I have 2 jars and I put them in Referenced libraries, they both have a bunch of classes in default package. When I create class in different package then the default - lets say org.myquestion I can't access the classes from the jars anymore.
This is something that really bugs me... First I can't create my own package and use anything from the jars. Second my IDE's (I use eclipse) auto-complete goes terrible - I guess it searches to meany classes at once... What I want to do is to put somehow the jars in some namespace... and to be able to access them like org.someones.libs.SomeClass
It certainly is bad practice to use the default package. A package groups classes and provides them with access protection (protected, package private) and functions as a unique namespace.
You can always use classes from every package, them being default or not, you can always mix.
Download the jar source code, And built it to jar by yourself and added the package name whatever your like.That's will solve your problem.
Importing classes inside JAR files that are in the default package
I ran into the exactly same problem as you did. The problem is the jar file "stdln.jar" has no named package, say, only with default package.
You cannot import a class from a default package, basically, since the import operation needs the package name:
import packagename.*;
So there are only two way to fix this problem:
the easier one: Do not create a package in your src folder and use default package two! Every class in stdln.jar would be imported to your src automatically.
Like this:
enter image description here
try to create your own jar file with a named package and copy all the class file into your newly-created jar file.
Since the stdln.jar is only used for education, so which you are gonna choose does not really matter. In real development, we never use default named package since it's not really a good practice, always leading to some confusing stuff.
Hope this would help you!
I am very new to Java.
I would like to use some features from package called daj
The tutorial code has the following lines.
import daj.*;
import java.util.*;
import java.lang.Math;
import Msg;
But first and fourth lines produce red underscore such that program cannot compiles.
How can I resolve this problem?
You have to add the packages to the classpath. If you are using an IDE as Eclipse or Netbeans, go to the project options and find the "Add library/jar" and they will take care of you.
You will need to add these libraries to the classpath both to compile and to run your program.
Also, the usual way of ordering imports is:
first, java. packages
second, javax. packagaes
third party packages
your packages
You need to have them in your build path.
That daj package or jar file should be in your package or current directory or in classpath.