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.
Related
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
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.
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!
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.
This is a total newbie question, but I can't figure it out so I figured that I would ask it here and see what happened.
Here's the problem: For my java programming class, we are supposed to download a .class file created by our instructors containing a custom-made class with methods that we are supposed to use in an assignment. I already know all of the code to create the class within my actual program, but I can't figure out which directory to place the .class file in. I have tried the src and bin folders inside the project directory, and even placed it directly into the project directory, but nothing seems to work. My instructors say to just put it in the same directory as my java program, but they are using jGRASP. What am I doing wrong?
You can place the class file anywhere, just be sure to add its directory to your project's build path. Right click on the project, then properties at the bottom then build path, til you find add class folder.
You have to create a project, or open your project if you have it already, then, while you are in the Java view, select File > New > File. It will open a window, you can create your file there and choose in which project folder do you want to put it. If in that window you press the button "Advanced >>" it will give you the option to "Link to file in the system", check it, and then you can browse your folders to the downloaded file.
I don't know if this solves your problem, it's what I understood from your question.
Your instructors class file must be in the build-time and the run-time class paths for your project to use it. Instructions assume that you are on a PC.
Run-time class path setup
Create a directory (perhaps C:\JavaRoot\classes). I will refer to this as the Classes directory.
If one does not already exist on your PC, create an environment variable named CLASSPATH
Add the Classes directory to your CLASSPATH envoronment variable
Download the class in question and put it in the Classes directory.
Build-time class path setup
Right mouse on your project in eclipse.
Select "Properties".
Select "Java Build Path".
Select the "Libraries" tab.
Click the "Add a Class Folder" button.
Browse to and select the Classes directory you created during "Run-time class path setup" above.
Click the "OK" button
For Eclipse use the following:
Project > Properties > Java Build Path > Libraries (Tab) > Add External Class Folder (Button)
If you have a configured project in Eclipse and you manually added a Java class file in some of the project directories, you just need to refresh the project for Eclipse to recognize it:
Right click in your project (in "Project Explorer" view) -> Refresh