creating multiple src directories in eclipse - java

I am doing this tutorial, which asks me to create multiple source directories in eclipse. Can someone explain how to do that? I am describing the steps I have taken so far as follows:
The image below shows the tutorial's directory structure on left, and my current directory structure on right:
As you can see, I need to create the following folders under Java Resources:
src/test/java
src/test/resources
src/main/resources
However, when I right click on the Java Resources folder and select new source folder, I get the following dialog box, for which I have clicked on the Browse... Folder Name button to open the subdialog which is also shown:
At this point, none of the choices seem to be what I want. So how do I create the three new src subdirectories that are specified by the tutorial?

Right-click your project, select Build Path and Configure Build Path.... In the Source tab, if src/main/resources or src/test/java appear, remove them. This might be a bug with the Maven plugin, I don't know. They appear like they are there, but aren't really.
Then use Add Folder... to add the folders you need. Do this by selecting a folder (to add folders to), like src/main, clicking Create New Folder... and using the folder name resources (or as appropriate).

You close the "Existing Folder Selection" dialog (since you don't want an existing folder, but a new one), and you enter src/test/java in "Folder Name". Then you click Finish. You repeat this operation for every new folder you want.
Of course, you could also use your file manager to create the folders, and select them in Eclipse.

Related

What is the best way to rearrange files in a big project in Intellij?

I am new to IntelliJ IDE so i might ask silly question.
I am using IntelliJ IDE and working on a big project. I want to change the name of 1 directory(A to B) and want to move few files of this directory in to another directory(C), what is the best way of doing it so that I don't need to change the package name inside .java file(i.e IDE handle all those things)?
Right click the directory in the project explorer ->refactor->rename A to B.
Then drag and drop the files you want to move into the C package using the project explorer.
Intellij will automatically change the package definitions at the top of the .java files for you.
Or use shift+F6 for renaming directory.
For moving files between directories
Just select file (If you want to move multiple file , select files by pressing ctrl + select) and move to destination directory

Linking external source folder with name "src" in an eclipse project

This is NOT a question about linking source in eclipse java project
Is it possible to add an external folder named "src" in eclipse as the source folder.
The problem here is to keep the external folder name as "src" and not any other name; to do that I tried deleting src and bin folder from the standard eclipse java project and then tried to "Link Source" but that does not work.
Is there any other way I can make this happen?
That is make the Eclipse java project's src actually point to an external folder named "src"?
Similar issues with the out put folder "bin" as well.
System Information
OS: Windows 8, 32 bit
Eclipse: 3.7
Thanks.
You need to use Eclipse to remove the existing source folder from the project configuration, then delete it from the file system, then you can add the externally linked source folder as "src".
Right-click on the project and bring up "Properties..."
Click on "Java Build Path" and then the "Source" tab to show a list of the source folders on the build path.
Select "yourproject/src" and click the "Remove" button. This will remove it from the project (and the .classpath file).
Come out of it and physically delete the folder.
Go back to that dialog box and this time click "Link source...". It should work now. It works for me.
To make link with external folder, we need to rename the folder if any folder already exists with same name like "src" or better delete that folder. After that you can make link with the external folder using the anyone of the following method:
Method: 1 - (Make link manually via code):
1. Open the .project file from root folder of your workspace.
2. Include the below code with your file path:
<linkedResources>
<link>
<name>folder_name_list_in_your_project</name>
<type>2</type>
<location>folder_path_to_include</location>
</link>
</linkedResources>
3. Save the file and refresh the project to include the external folder in your project.
Method: 2 - (Make link manually via UI):
1. Right click the project, select "Build Path -> Configure Build Path".
2. Choose "Source" tab.
3. Click "Link Source" button.
4. Click "Browse" to choose the folder.
5. Enter the folder name in "Folder name" field to list the external name in your project.
6. If you need to add the pattern for include and/or exclude file, click "Next" and enter the pattern. Then click "Finish".
7. Otherwise click "Finish".
It works for me and hope that my steps also help you.
It is somewhat ugly, but you can create a hardlink in your filesystem. In the Windows command line, use mklink /H src d:\path\to\external\src.
I know that the following will hardly be useful, but: Think of a different approach.

Adding .java files into eclipse?

I am so new to this, this is probably a stupid question but...
Basically my lecturer has given us a bunch a of .java demos. They are all inside a single file called 'demos'. I was wondering how would I import the 'demos' folder into Eclipse so I can use the folder expand it and select and drag any program I want to run into the work space.
Right click in project, Import... > General > Existing Projects into Workspace, chose your project.
Or:
Create a new project, drag and drop example.java files.
Extract your demos folder somewhere. Right click on the project browser, then:
New -> Java Project
Enter a project name then press Next
Click "Link additional source folder" in the bottom half of this page
Select your source folder
Finish
Now your folder will appear in your project, and you can edit files as usual. Keep in mind that editing / adding / removing files there will modify the actual files in that source folder.

Creating a separate Folder in the same package... [ECLIPSE]

Is it possible in Eclipse to create a separate Folder in a package? When select "new --> folder", my eclipse version always creates a package. But i want the new folder to be in the old package.
How can this be achieved?
Thanks in advance...
Patrick
What you might want to do, if you aren't putting code in that folder, is remove the folder from the build path. Right-click on the offending folder in eclipse and choose 'exclude from build path.'
Or, you may want a different effect:
Right click on your project and choose add new source folder.
Then in that folder, add the correct package folder structure.
You can have files in the same package located in different folders.
You need to exclude the folder from the source path
In a source folder, all 'new' folders will be considered a new package. It's part of the Java spec that makes all folders from a root to be part of the package structure.
As others have said, this is standard java behaviour and shouldn't really be worked around.
You are probably better off creating another folder outside of the source folder.
However there is a way to do it in Eclipse, be warned that this might come back to haunt you if you later try to build outside Eclipse as other tools won't honour these settings.
You can exclude a folder from the source path by opening the project properties.
Selecting Java Build Path->Source
Select the appropriate source folder and select the Excluded: child item
Hit the Edit... button.
Select the Add button next to the Exclusion patterns: pane
Add your folder and Finish/OK back to the editor.
The matching resources will now be excluded from compilation within Eclipse. It will still appear as a package in the editor though.
You want to create a new child package (bar) of the existing package. (com.foo)
Select new package and name it "com.foo.bar". Eclipse will correctly place it under com.foo.
FYI: Folders under any "source" folder are shown as packages. There is no physical difference except that they appear on the build path. As shown elsewhere you can exclude them but it's easier to just not put them under /src/... to begin with.

How do I include .class files in my project in Eclipse? (Java)

Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the file in my project so I can instantiate objects from it and use it. The file is TokenizerImpl.class, and I want to use it like this: TokenizerImpl tokenizer = new TokenizerImpl(foo);
I put the file in my project folder, and Eclipse says that "TokenizeImpl cannot be resolved as a type", which I assume means it cannot find the class or source. I tried putting it in the "bin" folder of the project and got the same error. Google search and SO search didn't seem to answer this, so I will give it a shot. How do I do this, oh wise ones?
Edit: Oh dear, I found the problem was something else entirely. These solutions worked fine, but I just forgot to create the Tokenizer interface that TokenizerImpl implements. Doh. Thanks for all your help though, I did learn a lot about eclipse.
You can add a directory containing the class files to the Eclipse project, only if it is inside one of your Eclipse projects, either in a generated directory or in one you have created.
This can be done by adding the class folder to the Java build path of the application. You can set this in the Project properties, by visiting Java Build Path -> Libraries -> Add Class Folder. Keep in mind, that you will have to specify the root folder containing the class files in their packages.
Therefore, if you wish to have the compiler access com.stackoverflow.Example.class present in the classes directory under project A (but not in the build path of project A), then you should add 'classes' as a class folder, and not classes/com/stackoverflow as a class folder.
Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder
The folder must contain a package hierarchy, i.e. if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.
You could also JAR the class files that you want to add and add the JAR file to the build dependencies. To me this is the cleanest solution. Internally the JAR file has to have the correct directory structure, of course.
Right click into your project and select Java Build Path to add new dependencies.
Copying it into the bin folder won't work very well because it is meant to hold the result of compiled sources. As soon as you clean anything the file will be gone.
or put everything into a jar file and add this as an external jar.
Other people have now given better answers. This "answer" was mainly to get information from the OP because the original question didn't really tell us, fully, what had been tried. There are now two answers that truly answer the question in a long-term way.
My original answer is left below for context.
Did you copy it to the bin folder within Eclipse or outside Eclipse? If you did this outside Eclipse then you have to right click on the "bin" folder and select "refresh" for Eclipse to see the new file.
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
zip the class folder.
Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars", add the zipped file

Categories

Resources