Move class to a new/other package in intelliJ - java

I installed intelliJ and created a package of name "com.company" and created some classes and interfaces in that package. Now I created a new package "myCalculator" and created some classes as well. But now I see my new package is not having the main class, so I need to import the package manually. How do i bring my main class to my new package in IntelliJ. Thank You.

You can just drag your files from package to package. Behind the scenes Java packages are just folders, so its is just the same as moving files between folders.

Use refactor option.
Select a class of a package
right click on it and select refactor
choose move option and enter the desired destination package name

Related

Packages in java not compile

Good morning everyone!
I did a project x but all my files were out of order so I decided to group them by folders.
foldera
---ClassA.java
---ClassB.java
folderb
---Class1.java
Main.java
The problem arises when I try to compile, since in the main it appears that the classes I made are not found
I thought this could be solved by putting in the classes
package src.foldera.ClassA;
And in the others the same
package src.foldera.ClassB;
And
package src.folderb.Class1;
So in all classes
And in the main put
import src.foldera.*;
import src.folderb.*;
But I keep getting the same error even though I put the packages
It should be noted that I did not create the folders in the code editor, rather I did it in the same Windows 10 File system
What is this about? Thanks!
Assuming a standard setup where the src folder is the root of your source hierarchy, the statements should be:
package foldera;
package folderb;
The imports should be similarly shortened to:
import foldera.*;
import folderb.*;
You have to define the package in which the class is in.
As an example in classA:
package src.foldera;
And in class1:
package src.folderb;
import src.foldera.ClassA;

Intellij class Import being added to object not the top of class

I've just started a new IntelliJ project. When I try and import a java class with the IntelliJ import shortcut, the import is being added to the object and not to the top of the class like it normally would.
Where in the settings can I change this?
See Preferences/Settings > Editor > Code Style > Java if this option ...
Use fully qualified class names
... is ticked then IntelliJ will always use fully qualified class names. If you disable this option then Intellij will include the import statement and refer to the class by its 'simple' name.
Here's a screenshot showing this configuration item:
If the accepted answer is not working for anyone, Try the below points too.
Check whether the class file is in correct package and resolve if there is any issues in the class file.
Click File -> Invalidate Caches/ Restart and Click Invalidate and Restart button, Once it restarted U'll be able to import properly.
I was facing this issue with my custom / generated(protobuf) java packages.
Solution (TDLR)
Whenever you face this issue, just check the package or class that getting imported doesn't have any error(red marking).
If so try to fix that.
I was having an issue with package naming and its actual location. By generated-sources -> Right Click -> Mark Directory As -> Sources Root corrected my issue.
Check whether generated-sources or folder under the generated sources need to mark us Sources Root.
My issue was with generated java files (Adding this just to help someone else with same issue)
Intellij shows red mark for Import with wildchar(import pkg1.subpkg1.*;). Like editor was unable to recognizing 'import all' under a package.
But direct import to the inner package was fine.
On top of that an resolving issue with import class add full package to the Class itself, instead of adding import statement on top the java file.
This issue didn't affect the code compilation, only in editor it was showing red mark.

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 make package inside master package in Android project using Eclipse?

I read here - How to create new packages in an Android project how to make a package, but I need to make a package inside master package com.example.me.
Every time I try to make new package there, it makes package in src folder, not in com.example.me package.
I mean something like this:
<com.example.me
<package1
<Class1
<package2
if you want to create a package inside the com.example.me , you need to write the new package name as com.example.me.Package1 while adding the New Package

The new class in my second package in the same project doesn't run in Netbeans

I've made a new package in the same project, and I can't seem to run the class in the new package. When I click on Run while in the new class, it doesn't run. Any ideas, on how to run this new class? I'm new to Netbeans and Java. Files look like this:
JavaApplication1 (project name)
Source Packages
javaapplication1
Test.java
javaapplication2
NewClass.java
Does the NewClass have main metod? If yes you have 2 options.
You can select main class for procject by rigth clickong on project and selecting Propperties. And then in Run node select main class as shown in image blow.
2.) You can run specific file from ide by rigth clickin on file and select run file as shown in image below.
Looks like your second package is not considered as source for building you need to change project settings so that second package also gets built

Categories