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

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.

Related

Importing class/java files in Eclipse

I'm new to Java Programming using Eclipse, so would really appreciate your help for this question.
I have doing Java programming both at college and home. At home, I have all my Java classes under a single project-(Java Reference). Now, in college, the java programs are compiled on the lab machines. And I would really like to have all these programs in a single project, on my laptop.
I had recently copied all the .class and .java files from the programs we did in college labs, so I could have them under the "Java Reference" project in my laptop. I copied the .class and .java files under the "bin" and "src" folders of the java project, on my laptop. However, I don't see anyway to import them through Eclipse.
From what I read here, it looks like having an entire project(created somewhere else) could be copied into one place, through the import option, but in my case, I only have the .class and .java files.
First, you don't need the .class files if they are compiled from your .java classes.
To import your files, you need to create an empty Java project. They you either import them one by one (New -> File -> Advanced -> Link file) or directly copy them into their corresponding folder/package and refresh the project.
You can import a bunch of .java files to your existing project without creating a new project. Here are the steps:
Right-click on the Default Package in the Project Manager pane
underneath your project and choose Import
An Import Wizard window will display. Choose File system and select the Next button
You are now prompted to choose a file
Simply browse your folder with .java files in it
Select desired .java files
Click on Finish to finish the import wizard
Check the following webpage for more information:
http://people.cs.uchicago.edu/~kaharris/10200/tutorials/eclipse/Step_04.html
create a new java project in Eclipse and copy .java files to its src directory, if you don't know where those source files should be placed, right click on the root of the project and choose new->class to create a test class and see where its .java file is placed, then put other files with it, in the same directory, you may have to adjust the package in those source files according to the new project directory structure.
if you use external libraries in your code, you have two options: either copy / download jar files or use maven if you use maven you'll have to create the project at maven project in the first place, creating java projects as maven projects are the way to go anyway but that's for another post...
I had the same problem. But What I did is I imported the .java files and then I went to Search->File-> and then changed the package name to whatever package it should belong in this way I fixed a lot of java files which otherwise would require to go to every file and change them manually.
import class folder does not work for me, but add jar worked!
1. put the class folder under the project folder
2. Zip the class folder
3. Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars".
4. Add the zip file. Done!

creating multiple src directories in eclipse

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.

Netbeans and source folders

I am working on a large project and I have configured multiple source package folders.
Project -> Properties -> Sources
However, when I import a library from this other source folder it says it doesn't exist so obviously with source folder is only liked in during compile time.
How do I get my project to reference these source files to work within my project without creating class files etc, and keeping both source directories separate from one another?
Thanks
If I understand your question correctly, you wish to make the source files from one NetBeans (Java Application) project A visible in another like project B.
In project B:
Go to Properties >> Libraries
Go to the Libraries category
Go to the Compile tab
Choose 'Add Project...' and select project A.
Regardless of whether the sources in A have been compiled, you should then be able to use them (e.g., name auto-complete, javadoc peek) when editing source code in B.
Make sure package names are correctly defined!

How to create a folder in Eclipse?

I'd like to create a folder under a package in Eclipse... The purpose of this folder is merely for organizational purposes. Meaning, I don't want it to be another package. Every time I try to add a folder under a package, it just creates a package instead. I'd like to have the following structure:
project/src/package1/someClass.java
project/src/package1/someFOLDER/anotherClass.java
project/src/package1/package2/anotherFOLDER/oneOtherClass.java
Is it possible to do this without adding a package? I come from a .NET/C# and C++ background... here I'd just add a folder and the reference to that class would be updated in the project.
How can I just add an organizational folder in eclipse? Thanks
Actually, folders are packages in Java so your question doesn't really make any sense in a Java context.
The term 'package' might be misleading... a package is definitely not the same as an assembly in .NET. You can think of 'package' more as a namespace, which in the Java world happens to be determined by the directory path.
For non-source files, you can create a new folder outside of your source folder, it will then be treated just like any other folder and not a package.
WTF are you supposed to do when you need multiple classes to be package private in a large project? Just look at your 25-30 class files all in the same directory?
That is a problem with Java's package system. Every package is a directory, and sub-packages are just different packages (no special visibility rules).
The most coarse visibility level is package-private, so that, yes, you have to lump your 25-30 files into the same package to avoid universally public visibility.
OSGi addresses this issue by introducing bundles, which can choose to not make packages visible to the outside. This gives you "project-private" packages.
Update: Also, you can reduce the number of files by putting related classes into the same source file. Only public classes need to have their own source file (I do prefer to have one file per class, though, public or not).
Well packages ARE folders.
If you want Eclipse to not build the contents of the package/folder, right-click on the project, select Properties, and under Java > Build Path edit the inclusion/exclusion filters.
If you're creating the folders to separate classes for organizational purposes, but still want them to be in the same package for access purposes, you can create multiple source folders and have them build into the same location. So, the folder hierarchy will look slightly different:
project/concern1_src/package1/someClass.java
project/concern2_src/package1/anotherClass.java
project/concern3_src/package1/package2/oneOtherClass.java
As I said, all three source folders can build into the same target directory. Or they can build into different target directories, if you like. The separate source folders will also carry over to the Package Explorer.
In eclipse the src folder is the main Source folder. First you just remove from the build source by right click the src folder and choose build path-> "Remove from build path". After that create the folder under src and right click the subfolder (package1) and again select buildpath->Use as Source Folder. Now you get src/package1.

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