Intellij - when the folder structure is different than the package name - java

My package name does not contain my full folder structure.
For instance - in IntelliJ I have created a test class:
and its package was initialized with package com;
How do I set which is the starting folder for the package name?
What is the difference between blue and yellow/gold folder color in the IntelliJ project window?

How do I set which isw the starting folder for the package name
File -> Project Structure. On the left, select Modules.
In the rightmost window, you will have a tab named "Sources". There you will have the opportunity to change your source/test directories. Note that a directory must exist before it is selectable this way.
Although I wouldn't depart from the setup you currently have, it's pretty standard.
What is the difference between blue and yellow/gold folder color in intelliJ project window
You will see that in the tab above ;)

Related

Java classes are in the same package (different directories) but they can't access each other

I have two files:
MyProject/src/main/java/foo_package/bar_package/MainClass.java
MyProject/src/gen/java/foo_package/bar_package/OtherClass.java
In both of those classes the very first line is:
package foo_package.bar_package;
If I call:
OtherClass foo = new OtherClass();
It cannot resolve symbol OtherClass. Why is that?
What I've tried:
Rebuild project
Invalidate cache/Restart
Reimport project
Delete .iml files and .idea folder and import everything again
It looks like there is a problem with the path of the last class:
MyProject/src/main/java/foo_package/bar_package/MainClass.java
MyProject/src/gen/java/foo_package/bar_package/OtherClass.java
If your classes have the same package (package starts after ../java/) but they are not part of the java build path, then the IDE won't recognise them as valid.
Try moving your OtherClass.java to the package where the MainClass.java is. Doing this should eventually solve your problem.
PS: be aware about the source folders of your project (most of the time main is the source folder by default and it's enough but there may be other source folders, generally added manually).
Make sure that your IDEA source folder is java, not src (for both java folders inside /gen/ and inside /main).
The sources root is marked as a blue directory in "Project" window (Alt + 1).

create source file inside a folder within a package in eclipse

I have a package where I'm putting all of my code.
However, I would now like to create a folder\package inside of that package like a child package.
I was unable to make a child package in eclise by folllowing the convention ExistingPackagename.ChildPackagename. So I went to my file system, made the desired structure. When I refresh, I see that my file system has the structure I want, but on eclipse, it still shows the Parent and the child packages at the same level. It shows my packages in the same level as
Parent
Parent.Child1
Parent.Child2
Parent.Child3
In reality there is no such thing as a sub-package in Java - each package is a completely separate entity, with the names being seemingly hierarchical only for convenience. For example, items with default visibility are not visible in sub-packages, despite what one might expect.
If your problem has more to do with presentation and aesthetics than substance, then perhaps what you are looking for is the hierarchical package presentation setting in the Eclipse Package Explorer: click on the little downward triangle/arrow at the top right of the package explorer and select "Hierarchical" in the "Package presentation" submenu:
You are probably in the "Project Explorer" view. Select Window -> Show view -> Package Explorer
Also, you don't have to create child packages from the file system. Just right click the current package, select New -> Package

Eclipse Organize Packages into Folder Hierarchy

I have a bunch of packages in an Eclipse project they have names like:
edu.xxx.proj.app
edu.xxx.proj.demo
edu.xxx.proj.utils
Is there a way in Eclipse to automatically collapse them into a folder structure? I would like them to be as follows on the workbench:
edu__
|_xxx__
|_proj__
|_app
|_demo
|_utils
Click the little down pointing triangle in the package manager and go to "package presentation". From there select "hierarchal" and that should take care of it.

Java package convention

I'm developing an Android project which currently has 4 packages:
com.myapp.app.activities
com.myapp.app.db
com.myapp.app.ws
com.myapp.app.utils
Would I be able to create an additional package which is just
com.myapp.app
?
Eclipse isn't letting me create this package. It tells me a package with this name already exists.
If I start a new project and create a package called "com.testing.app" and then create a new package called "com.testing.app.activities" afterward, it works fine.
For Android developers:
What I'm wanting to do is extend the Application class and have it in a separate package. Suppose com.myapp.app can't be used, what's a good name for this new package?
Eclipse won't let you create this package because it already exists.
Packages in Java are represented in the filesystem as hierarchical folders: com.myapp.app.activities is in the com/myapp/app/activities folder. com/myapp/app already exists, so you can't create this package.
In Eclipse, juste create a new class, and in the "Package" section, precise you want to create it in the com.myapp.app package. This should work.
The package com.myapp.app already exists. You can create a class named com.myapp.app.MyClass, you'll see it right in the app package.
Another thing you can do is changing the layout of your packages from a flat layout to a hierarchical layout :
Resources :
help.eclipse.org : Project Explorer view
Eclipse by default hides empty packages. In the package explorer view, click at the small arrow in the right top: View Menu. Choose Customize View. In the Filters tab you need to uncheck Empty packages. Now empty packages will be visible in the package explorer.
It would appear that if I manually place a file into the directory and refresh the package explorer in Eclipse, the new com.myapp.app package appears

Can I put a java package into another package?

Using Netbeans, I want to put a package into another package. For example in Visual Studio 2008, I can have folder called "Nodes", and another folder inside of Nodes called "Expressions". How do I do this in Netbeans? I think a package in Java is equivalent to a folder in C#.
For a package within a package, put the parent name, a period, and then the name of the child's package like so: Nodes.Expressions.
It will appear as it's own separate package in an IDE perhaps, but the folder hierarchy will be as you desire: Nodes/Expressions/[classes etc]
You can create subpackages in java. If your package is called nodes, adding an expressions folder inside of it will create a nodes.expressions package.
FYI in Java, it is customary to use all lower case for package names.
Kevin is correct in his answer about packages.
Heres Netbeans specific steps for adding a new package:
In your Projects view, go to the parent package under "Source Packages".
Right click on the Package and select New>Java Package (if Java Package doesn't appear in the list, select Other... and then pick Java>Java Package)
Fill out the New Java Package wizard with name of the child package
You can have any number of subpackages, e.g. abc.def.ghi.jkl.
You can simply put the source in a folder within the current package structure. You reference it by adding a dot and the new package name to the end of the existing package name. In Netbeans, in the new file wizard, where the package name is referenced, you can input the new package by choosing an existing package and adding the .newpackagename to the end. Netbeans will then create the directory structure for you.

Categories

Resources