changing java project module hierarchy - java

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.

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.

Eclipse error: “The import ... cannot be resolved”

I know I am asking the very popular question. But I can not find the solution to the problem. I have a sandbox to which I added a code of the unit test MulticurveBuildingDiscountingDiscountAUDTest.java file and commented it.
Then I added the main method and I could successfully run the program (print something in a console).
Finally, I uncommented the code of the MulticurveBuildingDiscountingDiscountAUDTest.java file and I saw the following error:
The import com.opengamma.analytics.financial.instrument.index.GeneratorSwapFixedONMaster cannot be resolved.
And further in the code:
GeneratorSwapFixedONMaster cannot be resolved
I know that this import is located in the og-analytics src/test/java location, which I believe is not listed anywhere in the build path. I believe the problem is with a build path options and specially with classes like GeneratorSwapFixedONMaster which were created specially for tests. I have been playing around with cleaning, rebuilding projects, reinstalling and as a result updating the JRE. I have visited these Import *** cannot be resolved [duplicate] and these Eclipse error: “The import XXX cannot be resolved” questions.
Do you know what shall I do to cure the following error?
I have many problems with other imports from the original MulticurveBuildingDiscountingDiscountAUDTest.java file as well.
Update: #1 is a location of my file. #2 is the location of classes this project uses. The MulticurveBuildingDiscountingDiscountAUDTest.java file is taken from the src/test/java
Update 2: one may see that in Libraries I have included all the dependencies I might need (at least I do not know what else to add). The Maven Dependencies contains the hole og-analytics package:
You included the source (src) folder og-analytics/src/main/java which contains the *.java files instead of the classes (bin or classes) folder with the *.class files (in your case, probably og-analytics/target/classes).
But instead using Add Class Folder... you should add the project og-analytics in the tab Projects. Or even better, in the Maven pom.xml file add the dependency to the project og-analytics like you did for og-util.
I know that this import is located in the og-analytics src/test/java location, which I believe is not listed anywhere in the build path.
Perfectly explains your problem. In order to import any class, you must either have the source in your build path, or some directory that contains a compiled version of that class. It is that simply.
The answer is: get clear on your project setup. If you intend to use classes from somewhere, you have to make them available somehow. Otherwise it will not work. In your case: if your tests want to make use a certain thing - then you should probably add that "thing" to your test project (you should avoid putting test-only stuff to your "product" projects).
That is all there is to this.

Removing Eclipse package name

Why do I get package name in the top of the page whenever I try to create a java class?
I have written some codes in the past using Eclipse but it had no package name like this. I am just wondering why?
You can see the following.
Your class in contained in the package named asd. This is also shown on the left hand side: project "asd" / package "asd" / Class "Ex1.java"
If you want to get rid of the package, you need to move your class to "default package". Right click on the class -> Refactor -> Move -> choose (default package)
However, I would recommend to work with packages and name them properly. Putting classes into "default package" is considered bad practice.
As you can see in package explorer
you placed your class inside asd package. This happened when you created your class via File->New->Class wizard. If you don't specify any package then by default it will use project name.
So when you confirm such configuration such setup Ex1.java will be placed inside asd folder (package), which means that your code will need at its start information about name of package.
If you don't want to have this package asd; line you can for instance
clean that section from wizard when you create package,
or even remove that line from your code and when Eclipse will ask you how to fix this situation
place cursor in line with problem (first line)
select Edit->Quick fix, or press Ctrl+1)
and chose Move Ex1.java to default package.
But you should avoid using default package. For instance when you export classes from your project to Jar file you will not be able to import them in new projects.
For more info about packages visit official tutorial:
What Is a Package?
Lesson: Packages
All Java classes have specified their address in the beginning of the document. They say
package package_name;
If the class is in the default package (directly in 'src' folder), then there is no package written in the beginning of the class...
You can see here, and try on your own:

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

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

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.

Categories