Java: How to move classes to another package in Idea Intellij? - java

I have the following file sctructure:
name1/name2/name3/name4 (must be project's root and git root)/src/main/java/com/fileinside1/fileinside2/fileinside3/init/woodenStone
Inside the last folder (woodenStone) my .java files are located.
The IntelliJ Idea makes everything look like this:
name4
src
main.java.com.fileinside1.fileinside2.fileinside3
init.woodenStone
SomeFile1.java
SomeFile2.java
And inside every file there is a
package main/java/com/fileinside1/fileinside2/fileinside3/init/woodenStone;
as the first line.
So, the files are located in package which starts with main folder. But I need the package to be
com/fileinside1/fileinside2/fileinside3/init/woodenStone,
to get a little deeper inside.
So far I only manage to do so by manually changing the package name in .java files, Alt+Entering it and selecting "Move to package com/fileinside1/fileinside2/fileinside3/init/woodenStone".
But I have more than 50 files and would really appreciate a way to do it for all files at once.
Could anyone please tell me if there is such a way?
EDIT: Just found out that even the way I use doesn't really work right. Instead of moving file's into deeer project's folder, Idea just creates a second com folder inside src (How does it even manage to create a floder with the same name?).

Open the Project panel and select the Project view (top left).
Make sure that the main directory isn't marked as a source root. You may need to unmark it by right clicking and selecting Mark Directory As -> Unmark...)
Make sure that your java directory is marked as a source root (right click, Mark Directory As -> Sources Root).
Select all the files in the woodenStone directory.
F6 -> Move Specified Files -> to package

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

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.

Unable to create more than one package in src folder

I'm following this tutorial https://danielniko.wordpress.com/2012/04/17/simple-crud-using-jsp-servlet-and-mysql/ and I'm slightly stuck. I would like to create the 4 packages in the src folder, but Eclipse will only allow me to create one.
I create the first one, It adds it to the src folder:
I create the next package, It adds it outside of the src folder:
I move it into the src folder, It creates a copy and put it in libraries:
Any idea what I can do to stop this happening? I want to put 4 packages in the src folder as per the tutorial but I don't know what is happening. I've been using Eclipse for a while and this has never happened.
Apologies, it does put them in the src folder, but I have to refresh the project for it to do so.

Runnable JARs missing Images/Files (Resources)

When I export my code as runnable JAR from eclipse all the files that I've set it to grab such as button images and other files are missing even though they are actually in the JAR. I've added getClass().getResource in front of the files but then when I try to run the JAR nothing even happens, any suggestions?
Seems like you not putting your stuff in the right sense. In order to make it work, follow these steps :
Right-Click your Project in Project Explorer Tree.
Go to New -> Source Folder and then provide any Name to the Source Folder.
Now manually add your stuff to this Source Folder so created by you, like if you want to add images then make a New Folder, by manually visiting this Source Folder through File System.
Name this New Folder as images and copy your images to this Folder.
Now go back to your Eclipse IDE and Refresh your Project from the Project Explorer, by Right Clicking your Project, here you be able to see your added content now after refreshing.
Now in order to access, say any image, you will use.
getClass().getResource("/images/yourImageName.extension");
which will return one URL object. Do remember the first forward slash, in this case, since whatever is inside your Source Folder is accessed with the help of this, in simpler terms. Now when you will Run your project, the content of this Source Folder will be automatically added to the bin folder and when you will create a Runnable Jar, then the stuff inside your Source Folder can be accessed as it is.
The path needs to be right for the resource.
For "foo.gif" being at the root of the jar, you must refer to it using "/foo.gif".
If the program works correctly after a complete clean and rebuild, but fails as a jar, you most likely do not have the files included in the jar.
Try to put the folders in the jar the same way that you got them in the program. Put in the same resources in the same places that you have them in the project. The jar will reference to them the same way as in your compiler did.
You need to get the images using stream like this -
this.class.getClassLoader().getResourceAsStream("test.jpg") and make sure the images are present in the jar which you are referencing.
As nIcE cOw said, you just need to create a Source Folder in you Project Explorer Tree.
All the files inside that folder will be in the root project folder.
To refer to them, you must write your projects name slash the file name as it:
getClass().getResource("ProjectName/image.extension");
I hope this helps!

How to take package .java files into a Eclipse Project

I've got a copy of a java package, with example implementation. The package is in a structure called com.java.project (folders, containing myriads of .java files) and there's also a few example files (example1.java, example2.java)
The example files have 'import com.java.project' calls in them, that's all well and good. However, I can't seem to work out how to get Eclipse finding the package contents (currently returns "Type not found" for any calls to the project objects.
I've tried placing the com structure in the same level as my example1.java, but that doesn't work. I have read that I need to compile the package into a .jar to get it to work w/Eclipse, but that doesn't seem to want to behave, none of the files compile.
In Eclipse you can right click on the Project Explorer and select "New -> Java Project"
Then you can import your java files by right clicking your project, selecting import from file system. If the class file shows an error, open the class file, the class name should have red curly braces, click on the error tick mark and Eclipse will give you an option to move the file to the correct package which in turn creates the folder structure for you.
1.Can you do a check on the build path of your Java project? (Right-click the project -> Properties -> Java Build Path -> Source tab), and see if the root folder contains the code is properly specified here.
2.Maybe a Project->Clean will help as well.
Hii,
Just Create a new project with File -> New menu
you need to open Java Perspective and there you will find a package named "src" you need to copy your "com" directory and paste it at the src folder.
I hope this will work for you.

Categories