How to change the path where R.java class is generated [duplicate] - java

In the manifest I have:
... package="com.domain.app.multimedia"
which then names the application/activity with:
... activity android:name=".MultiMedia"
Eclipse, in turn, generates R.java in the package/path:
... com.domain.app
This package/path name may be a legacy of prior package renamings/refactoring - don't know.
I presumed (a mistake, or not) that R.java generation would follow the package name declared in the manifest. It would be a treat to find out how the gen chose the path/package name it uses. And more to the point, what is the rule for the manifest package name (other than the standard precaution of uniqueness, and relating to an owned domain).
Otherwise, I can live with this (an easy solution where forcing what appears to be an arbitrary import statement solves it all as far as getting a runtime).
Cheers,
Richard

Generally speaking, when you change the package name in your AndroidManifest.xml file, you are prompted with a question if you'd like to change the configuration to reflect the new package.
If you haven't clicked yes, you can always right click on the project -> Android Tools -> Rename application package

Related

Error `The declared package "part2.stage2" does not match the expected package ""` with correct package name

So I have a Competitor.java file on path: /home/john/javaStuff/CMTR/src/part2/stage2/Competitor.java. I have VSCode open in the CMTR. So CMTR is my current working directory if that info is necessary.
The only content I have in the CompetorList.java file is
package part2.stage2;
It seems that the error expects me to not have the package keyword there. If I remove it, it works fine indeed. But I want to use packages. After some Googling, Java does not require a default package, so I don't know why it wants me to have no package. The file path matches and all other packages work, just not this. Here's the tree of of structure:
I have other Java files in different stages eg. stage 1 with the same layout and they all work including that of part1.stage1. But part2.stage2 is just misbehaving.
It's because in the file ManagerTest, the package is declared as package testing;. So the extension inferred the source root to be src/part2/stage2.
What you can do is:
Change the package name to:
And then reload VS Code window.
Or, if you want to disable the auto source root inference and want to specify it by your own. You can leverage the setting java.project.sourcePaths
Remember to set it in the WORKSPACE scope.

Do I always have to type package name in Java?

Today I started learning Java.
I saw that package automatic gets included in .Java file.
I was wondering if it always need to be included?
Consider specify a common package for all the types within a same project.
In Java is common to start a project with a specific package setting. A package creates a namespace to disambiguate the types that it includes, to play nicelly with other projects that may or may not be in the same classpath. Normally, the package is bound to a URL of the project.
Think of Java packages like C++ namespaces.
A huge project/product written in Java can depend on lots and lots of projects, each described in a different package.
Organizations like Apache have lots of projects, organized under a common package pattern: org.apache.<<name_of_the_project>>.
Consider starting your project with a package named: com.user3552670; or something like your personal site, so persons that will consume your project can relate to the creator.
Yes and no.
It's used to specify the package of the class, read more here.
You could create a class without a package, but your code will look bad..
They exists to avoid conflicts, example between your code and default java package.
If packages doesn't exists, you can't create a class named ArrayList because already exists in Java.
Some IDEs force the fact that, if your .java file is in com/a/b/c folder his package should be com/a/b/c (If i don't remember wrong, IntellIJ IDEA do that)
Yes and no.
It must be there, but the IDE takes care of it (I don't use Netbeans, but I'd bet that it can do it, too). When moving files between packages, it has to be updated, but again, the IDE does it all.

Compile-time created class is shown as non-existent in IntelliJ

Motivation:
I'd like to try if compile-time annotation processing fits my problem. It needs to work out of the box, no compiler arguments etc.
Current state:
I have:
The annotation
An annotation processor
A .jar containing both of these and a javax.annotation.processing.Processor file containing the FQCN of my processor in META-INF/services
What should happen:
It should autodetect the processor
It should process the annotation and create a new class (WiredAnnotated)
I should be able to use this class in one step of compilation (not multiple phases)
I wan't the editor to accept this class is generated (e.g. AndroidAnnotations manages this as well)
What actually happens:
It autodetects the processor
It creates a new class (in out/production/*/generated/)
I am able to use this class
The source code looks right
When decompiling it looks ok too
The editor cannot resolve the class (see screenshot)
What I tried:
Restarting IntelliJ
Invalidating caches
Checking for output of the annotation processor
Screenshot:
When compiling, it actually works as expected. I guess it has something to do with inspecting the wrong directories.
Does anyone have an idea/clue on what I'm doing wrong? Did I miss information which could help you help me?
Thanks in advance, Till
Well, you need to add you out/production/*/generated/ to projects source folder. So, IntelliJ will know about your generated classes.
You can make it via Right click on directory > Mark directory as source root.
or
Project structure (F4) > Modules > Sources tab > Source folders should contain all directories with your source codes, generated one inludes.
In android there is a gen dir in root folder, but notice, it glows blue or green which means it marked as Source folder, it is also visible in Project structure > Modules. It contains R, BuildConfig and Manifest.

changing java project module hierarchy

when I create a new java project in intellij the module is: com.example.ProjectName .
how can I use my own domain?
like:
us.mysite.ProjectName
I've already tried to look for it and didn't found the answer.
This is the name of the java package. In order to change the package, the package foo.domain.project line must be changed along with the name of the directory in which the code lies. As seen here packages can be created with varying names through IntelliJ(including possibly when creating a new project), or a file can change:
package foo.domain.project;
to
package bar.dom.proj;
and move sources from the src/foo/domain/project directory to the /bar/dom/proj directory.
There is lot of useful information available on Netbeans refactoring wiki. Here is how you can rename your package
Step1 : Right-Click on the package name, in the Refactor menu select
Rename (Ctrl+R)
Step2 : In “New Name” enter the new name that you desire for the
token. Then click on Refactor button complete the operation.
Optionally you preview the effect of the refactoring.

Jars with default package

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!

Categories