Directories rendered as packages in Intellij Idea 13? - java

After a rebuild of my android project i saw that the src folder and other folders(assets, libs etc) have turned into packages.(Packages in Intellij have a folder icon with a ball inside it)
Due to this, I also got errors in all files inside src folder. (for example see SearchActivity.java , 1st line).
I am new to Intellij so apologies if this sounds rudimentary.

As of 2019.2.2, it's easiest to do File > Project Structure..., double click Modules, and then under the "Sources" tab right click the folder and un-mark it as a source folder.

It looks like the Shopper folder has been set to being a source root, instead of the src folder. To fix, turn off Flatten Packages and Compact Empty Middle Packages, right-click on src, go to Mark Directory As, and then Sources Root. Then un-mark the project folder.
You can also do this from Project Settings.

Related

Intellij IDEA 2020.1 Copy Resources to Output with Package

I'm using IntelliJ IDEA 2020.1 CE. I have a non-Maven Java project that builds and works fine. I use the built-in build system. The resources are all correctly copied from the source resources folder to the project's output directory; however, since I use a package, e.g., org.acme, I'd like the resources to actually be copied to the output/org/acme folder, so that they end up with the .class files.
Is this possible? Do I need to perform a post-build step? Other?
Thanks,
John
The good people at JetBrains helped me out on this one, so I thought I'd share it here.
To copy Java project resources to a directory relative to the project output directory (where the .class files go by default), use the Relative Output Path for the directory marked as your resources directory.
https://www.jetbrains.com/help/idea/content-roots.html
For example, let's suppose you have a Java project and you put your classes into a package called org.acme. Let's also assume your resources are in a resources/ directory and that your project's output directory is called target/.
What will happen by default is that your .class files will end up in target/org/acme/, but your resources will end up in target/. If you want to copy your resources to the same place as your .class files (it makes loading them easier at times), you can set your Relative Output Path to org/acme/
In the IntelliJ IDE, from the main menu:
File -> Project Structure
Select Modules from the left-hand pane
Select Sources from the main panel
Select your resources/ directory
Click on the pencil icon next to the resources directory
Add your Relative Output Path
Next time the project builds, the resources will be copied to this relative path under the primary output directory.
I hope this was useful!
John

My IntelliJ had maven prob about package(folder)

screenshot of my problem
IJ recognizes main.java as a package, not a normal folder.
This is maven project.
How can i solve this problem?
You have incorrectly marked src as a source root (notice that is is blue) - right click and "unmark" it as a source root.
Then, locate the src\main\java folder and right click on the java folder -> mark as source root.
Once you have done this, intellij should recognise your folder structure correctly.
You might also want to mark resources as a resource root.

src folder causing errors in package declarations in Eclipse

In my project, my src folder is under X/Y/A.B.C/src.
Under src, I have folders A/B/C/*.java files and A/B/C/D/.*java files, etc.
I'm getting my package names all starting with src, for example src.A.B.C.
My packages should start with A. And this causes an error in every class because my package declaration doesn't start with src.
I've tried fooling around in the .classpath and with the java build path, but can't get the package names changed.
You are using one folder up as your source folder.
Right click on the folder, Go to build path, Click Remove from build path.
Now, go one folder down, right click on that, go to build path, and click Use as source folder.
For example :
Wrong src folder selected.
Right click on Custom and Remove from Build Path
You will have something like this.
Right click on src and add Use as source
BANG!!! This is what you wanted.

How to import a Java program into Eclipse?

I have some java source code. It an just an archive which is four folders.
Folders MAIN, DATA, TAGS, USER_INTERFACE. Each folder contains a few class files.
I see how everything works together, but can't see to get the program to import correctly or run. I did find a MANIFEST.MF.
What is the proper way to import such a project?
Create an Eclipse project. Under the project settings, select the "Java Build Path", and add each source folder (and remove the default src folder that Eclipse may add for you).
If necessary, you may set the output/build folders for each source directory, and clear Eclipse's default bin output directory. Check the "Allow output folders for source folders" to enable this, then set each source folder's output folder.
(You may or may not want to keep compiled classes in individual directories.)
If there are class files in the project that aren't generated from source in the project, in the same dialog, select the "Libraries" tab, and click the "Add Class Folder" button to add dependencies from within the project.
It doesn't seem that your source code is an eclipse-importable project (it would have to have a .classpath and .project file in the top level directory for that).
You should instead create a new Java project. For simplicity, set the project's location to be the parent directory of your MAIN, DATA, etc. directories. Then after the project is created, configure a source folder for each of MAIN, DATA, etc. (right-click the project, choose Properties > Java Build Path, then work in the Source tab). Your source folders should then show up in the navigator and the project should be able to be compiled.
Create a hello world project in eclipse and understand the project structure in eclipse.
And then manually import the files it is simple and avoids a lot of confusion especially for somebody new to the environment.

How to create a project from existing source in Eclipse and then find it?

I have created several .java files. All of them are located in one directory. I used a text editor to write these files. Now I want to switch to Eclipse. How can I do it? I have tried many ways. None of them works.
ADDED:
I think the common way is to have source in the eclipse folder called "workspace". So, how do I get my files in these directory. Should I use eclipse to create a new project from existing source and Eclipse will put all file to the workspace? Or I should manually copy all my files to the workspace? Where should I put my class files than? Should I create a subdirectory? With which name?
ADDED 2:
When I try to create a project with name "game", the eclipse writes me that a project with such name already exist. But how can I open this project?
ADDED 3:
In my "workspace" I have created a subdirectory called "game". I copied all my .java file into this subdirectory. Then with Eclipse I created a new project with the name game. As a result, Eclipse created .classpath and .project files in the directory "game". It also created bin and src subdirectories. And now I think it is not the correct way to go. The source files are supposed to be in the "src" directory. Right? And at the moment all my .java files are in the "workspace\game".
ADDED 4:
I did it other way around. With Eclipse I have created a new project with the name "game". As the result, Eclipse created a folder called "game" into folder "workspace". In "game" the folder I found "src" folder. I copied all my .java files into this folder. But now in the "Package Explorer" I cannot open "src" folder. So, how can I access my source files from Eclipse? Why Eclipse does not want to open the "src" folder?
Easiest Method:
Put all source files into one directory named after your project. i.e. "ProjectName" You can keep this directory in your workspace or it can be somewhere else.
Start a new project in eclipse and name it using that same project name.
Uncheck the "use default location" box and find the directory where your project is unless your project is already in the workspace - then you must not uncheck the "use default location" box
Click 'next'.
Eclipse should be smart enough to figure out what's going on. After clicking next, it will show you all of the files it found in that directory. It will just automatically add those files to your project. VoilĂ !
Right-click in the package explorer and select New - Java Project
Create the new project Game
Open the new project in the package explorer - you should see only the source folder called src (there's nothing inside yet)
Open a file Explorer (e.g. Windows Explorer) and drag your sources
Drag them to Eclipse and drop them inside the new src folder - if asked select "Copy files"
Eclipse should put the files into the default package, if that's not correct you can edit the offending files (marked with a red cross) by opening them in Eclipse, selecting the package declaration (usually line 1), pressing Ctrl + 1 and selecting the appropriate option (e.g. "Move xy to package com.game"
This answer is going to be for the question
How to create a new eclipse project and add a folder or a new package into the project,
or how to build a new project for existing java files.
Create a new project from the menu
File->New-> Java Project
If you are going to add a new pakcage, then create the same package name here by
File->New-> Package
Click the name of the package in project navigator, and right click, and import...
Import->General->File system (choose your file or package)
this worked for me I hope it helps others.
Thank you.
The easiest method is really good but you don't get a standard Java project, i.e., the .java and .class files separated in different folders.
To get this very easily:
Create a folder called "ProjectName" on the workspace of Eclipse.
Copy or move your folder with the .java files to the "ProjectName" folder.
Create a new Java Project called "ProjectName" (with the Use default location marked).
Press <Enter> and that's it.
There are two things
1- If its already a Eclipse Project, then simply go to File->Import->General->Existing Project into Workplace
2- Otherwise define project type e.g. Java, Web etc
Create a new project of type you define into your workplace. Copy Paste source , lib and other necessary files. refresh, compile and run project in eclipse.
In the package explorer and the navigation screen you should now see the project you created. Note that eclipse will not copy your files, it will just allow you to use the existing source and edit it from eclipse.
There are several ways to add files to an existing Java project in Eclipse. So lets assume you have already created the Java project in Eclipse (e.g. using File -> New -> Project... - and select Java project).
To get Java files into the new project you can do any of the following. Note that there are other ways as well. The sequence is my preference.
Drag the files into the Navigator view directly from the native file manager. You must create any needed Java packages first. This method is best for a few files in an existing Java package.
Use File -> Import... - select File System. Here you can then select exactly which files to import into the new project and in which Java package to put them. This is extremely handy if you want to import many files or there are multiple Java packages.
Copy the fires directly to the folder/directory in the workspace and then use File -> Refresh to refresh the Eclipse view of the native system. Remember to select the new project before the refresh.
The last one is what you did - minus the refresh...
While creating a project from a full folder may or may not work within the workspace, there's a condition outside of the workspace that prevents starting a new project with a full folder.
This is relevant if you use numerous folder locations for sources, for example an htdocs or www folder for web projects, and a different location for desktop Java applications.
The condition mentioned occurs when Eclipse is told to create a new project, and given a full folder outside the workspace. Eclipse will say the folder isn't empty, and prevent creating a new project within the given folder. I haven't found a way around this, and any solution requires extra steps.
My favorite solution is as follows
Rename the full folder with an appended "Original" or "Backup.
Create the Eclipse project with the name of the full folder before the folder was renamed.
Copy all the relabeled full folders contents into the new project folder.
Eclipse should make a new project, and update that project with the new folder contents as it scans for changes. The existing sources are now part of the new project.
Although you had to perform three extra steps, you now have a backup with the original sources available, and are also able to use a copy of them in an existing project. If storage space is a concern, simply move/cut the source rather than fully copy the original folder contents.
If you creating a new project based on an existing Maven structure :
Create the project using a general project wizard and give the project the same name as just created.
If you try to create the project as a Maven project via m2e will receive an error that project/pom already exists.
Create a new project..
Right Click on your project..
Select Build path --> Configure Build Path
Under source tab choose link source, your .java files containing folder..
I am suggesting this since none of the methods that you tried have worked ---FYI
Follow this instructions from standard eclipse docs.
From the main menu bar, select command link File > Import.... The Import wizard opens.
Select General > Existing Project into Workspace and click Next.
Choose either Select root directory or Select archive file and click the associated Browse to locate the directory or file containing the projects.
Under Projects select the project or projects which you would like to import.
Click Finish to start the import.

Categories