Unable to create more than one package in src folder - java

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.

Related

How to include all files (including ones in subfolder) in sourcepath of a JAVA project?

I am trying to create a Java project on VS Code, and I wish to add all of the files inside the src folder. Layout of the project:
As you can see, there are multiple subfolders inside src, and I wish to add all of them in the java.project.sourcePaths inside the settings.json file so the files can run. If I leave it as "java.project.sourcePaths": ["src"],, I got errrors which my objects created cannot be resolved to a type.
I got this temporarily fixed by changing the direcotry into src/PA 1/, but I do not know how to include all subfolders of src. This means everytime I create a new subfolder, I need to come back to add it in java.project.sourcePaths.
Is there a way to include all subfolders inside src? I tried src/**/ but it did not work.
PS: Can any one explain why Java is not recognizing files inside the subfolder?
I don't think you need to add all the subfolders of src to "java.project.sourcePaths". "java.project.sourcePaths": ["src"] is enough.
The codes under the subfolders of src should be structured by the package and import, both relative to src.

How to create subfolders with a build path in eclipse

I want to create two main folders under src folder. When i try this it creates in normal way as seen in image.
How can i create folders in this way.
src/main/java
src/main/resource
I have tried searching google and other websites. Nothing seems working. Also the option when you press ctlr+F10 doesn't seem to solve my problem.
main>Folder --Right click--Build Path and select
use as a source folder

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

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

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.

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!

Categories