Eclipse: project directory isolated from source directory - java

I have Java code managed by a Git repository. Currently, the directory structure is
myProject |
-- src
-- bin
-- lib
-- .git
Currently, each developer creates its own development environment under his desired tool (Eclipse/Netbeans). I would like to create a Eclipse project for this code and add it to my source repository, so that new Eclipse developers don't need to create a new project and set the dev environment manually. I'd like to have this:
myProject
|
-- src
-- bin
-- lib
-- .git
-- eclipse
|
-- .settings/
-- .project
-- .classpath
From what I've read in the web, I don't need to add the .metadata directory to the repository. Also, I know I could have dependency problems in some situations if I add the .classpath folder to the repository, but I prefer to not support these problematic situations (eg different Java compilers) than forcing the dev to manually set the classpath. Finally, I don't want to use Maven in this project.
Concisely, my requirements are
- I don't want to put Eclipse project files in the root folder
- I don't want to duplicate the code inside eclipse folder (that's what happens when I try to import existing code into an isolated project folder). I want it to reference the source files I have in the folder named 'src'
- I want to configure Eclipse to put the compilation output (.class files) in the 'bin' folder.
That latter item I was able to set when creating the project, but I wasn't able to configure Eclipse to reference the source code (not duplicate it) without choosing a source parent directory as the project folder.
Any help is much appreciated.

You will need to create a project in the place you want it, and then use 'linked source' directories to point to your actual source code.
more information:
Store eclipse .project files outside the project directory

Create your directory structure above with the eclipse subdirectory.
Create a new Dynamic Web Application (or whatever, assuming based on .settings)
Uncheck Use default location and specify your eclipse directory, hit Next
Remove the lone source folder
Leave Generate web.xml... unchecked
File > New > Folder and click Advanced>>
Use Link to alternate location (Linked Folder) to create src and bin directories that are linked to the top-level src and bin.
Right-click on the project and use File > New > Source folder to add the linked src folder with an output directory of bin
Move WebContent to WebContent.bak and create a new WebContent directory that is linked to the top level.
Move the contents of WebContent.bak to WebContent
Manually edit eclipse/.settings/org.eclipse.wst.common.component to change WebContent.bak to WebContent (is there a setting for this? couldn't find it)
Refresh the project
Right-click the project and select Java EE Tools > Generate Deployment Descriptor Stub

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

How do I say to Eclipse where to save my compiled packages to another path?

So I have this Java Eclipse project where I use a custom package:
Project/
src/
defaultpackage/
*.jar
mypackage/
class1.java,
class2.java ...
How do I say to Eclipse where to compile all mypackage classes as mypackage.jar in a different output folder? I know that by default Eclipse will create the jar's inside the project directory.
My idea is to have a folder for my compiled packages out of the project structure, actually create myjarlib folder, so everytime I compile a different package will create a / ... / myjarlib / newpackage.jar only if I specified this for the newpackage.
In your structure example, the 'src' folder is a source folder. You can configure where eclipse emits the compile-on-save generated class files on a per-source-folder basis.
You cannot configure this on a per-package basis.
So, change it at the source folder level. If you want one package to go in directory A, and the other package to go to directory B, make 2 source folders, and configure for each. Example:
Project
src
main
pkg1
SomeJavaCode.java
test
pkg2
SomeTestCode.java
Here Project/src/main is a source package (containing the source for class pkg1.SomeJavaCode), as is Project/src/test. right click on the project and select 'properties' to modify which directory(ies) are source folders. Also in the 'properties' of any project (not the source folder), go to section 'Java Build Path', pick the "Source" tab, click on the sourcefolder you want to edit, mark the 'allow output folders for source folders' option (by default, eclipse sends the compile-on-save results of all source folders to the same folder, this checkbox lets you configure it on a per-folder basis), and configure it.

How do you create a 'java' directory under the auto-generated 'src' directory in Eclipse?

I have create a project under eclipse. Eclipse automatically creates a directory named 'src'.
The Source control system I am using imposes a directory structure which begins src/java/com/company_name/package_name/java_class_name.java .Other packages which are dependent i.e. use the java files in other eclipse projects refer to the classes with import com.company_name.package_name.java_class_name.
My question is in order for the above to work I need to create a 'java' directory under the autogenerated 'src' and link this to the directory where my files are starting 'com'. If I select "project-properties-Java Build Path-Add Folder-link source" then specify my directory structure 'src/java' it creates a java directory at the same level as my src directory. If I select add folder it presents me with existing folders only which don't include 'java'. I cannot see a way to resolve this.
Right click on the src folder and select New / Other... / Folder (not Source Folder). Type the name 'java' into the dialog then click OK.
But, that's not what you want. What you really want is to remove the source folder 'src' then add a new source folder 'src/java'. This is so that the package names are 'com.blah...' and not 'java.com.blah...'.
So, first delete the src folder.
right click on src
select Delete
confirm
Next, create a new source folder.
Right click on the project
select New / Source Folder
type in 'src/java' (no quotes)
click OK
Now you have a new source folder with the proper path, and the package names will all be as expected in Eclipse.
Modified alternative to Zagrev's answer:
You still need to delete the src directory from Eclipse project
Manually create the Java subdirectory.
Then use Build Path-> Link Source... and link it to the created directory.

How to import a Java program into Eclipse?

I have some java source code. It an just an archive which is four folders.
Folders MAIN, DATA, TAGS, USER_INTERFACE. Each folder contains a few class files.
I see how everything works together, but can't see to get the program to import correctly or run. I did find a MANIFEST.MF.
What is the proper way to import such a project?
Create an Eclipse project. Under the project settings, select the "Java Build Path", and add each source folder (and remove the default src folder that Eclipse may add for you).
If necessary, you may set the output/build folders for each source directory, and clear Eclipse's default bin output directory. Check the "Allow output folders for source folders" to enable this, then set each source folder's output folder.
(You may or may not want to keep compiled classes in individual directories.)
If there are class files in the project that aren't generated from source in the project, in the same dialog, select the "Libraries" tab, and click the "Add Class Folder" button to add dependencies from within the project.
It doesn't seem that your source code is an eclipse-importable project (it would have to have a .classpath and .project file in the top level directory for that).
You should instead create a new Java project. For simplicity, set the project's location to be the parent directory of your MAIN, DATA, etc. directories. Then after the project is created, configure a source folder for each of MAIN, DATA, etc. (right-click the project, choose Properties > Java Build Path, then work in the Source tab). Your source folders should then show up in the navigator and the project should be able to be compiled.
Create a hello world project in eclipse and understand the project structure in eclipse.
And then manually import the files it is simple and avoids a lot of confusion especially for somebody new to the environment.

How to create a project from existing source in Eclipse and then find it?

I have created several .java files. All of them are located in one directory. I used a text editor to write these files. Now I want to switch to Eclipse. How can I do it? I have tried many ways. None of them works.
ADDED:
I think the common way is to have source in the eclipse folder called "workspace". So, how do I get my files in these directory. Should I use eclipse to create a new project from existing source and Eclipse will put all file to the workspace? Or I should manually copy all my files to the workspace? Where should I put my class files than? Should I create a subdirectory? With which name?
ADDED 2:
When I try to create a project with name "game", the eclipse writes me that a project with such name already exist. But how can I open this project?
ADDED 3:
In my "workspace" I have created a subdirectory called "game". I copied all my .java file into this subdirectory. Then with Eclipse I created a new project with the name game. As a result, Eclipse created .classpath and .project files in the directory "game". It also created bin and src subdirectories. And now I think it is not the correct way to go. The source files are supposed to be in the "src" directory. Right? And at the moment all my .java files are in the "workspace\game".
ADDED 4:
I did it other way around. With Eclipse I have created a new project with the name "game". As the result, Eclipse created a folder called "game" into folder "workspace". In "game" the folder I found "src" folder. I copied all my .java files into this folder. But now in the "Package Explorer" I cannot open "src" folder. So, how can I access my source files from Eclipse? Why Eclipse does not want to open the "src" folder?
Easiest Method:
Put all source files into one directory named after your project. i.e. "ProjectName" You can keep this directory in your workspace or it can be somewhere else.
Start a new project in eclipse and name it using that same project name.
Uncheck the "use default location" box and find the directory where your project is unless your project is already in the workspace - then you must not uncheck the "use default location" box
Click 'next'.
Eclipse should be smart enough to figure out what's going on. After clicking next, it will show you all of the files it found in that directory. It will just automatically add those files to your project. VoilĂ !
Right-click in the package explorer and select New - Java Project
Create the new project Game
Open the new project in the package explorer - you should see only the source folder called src (there's nothing inside yet)
Open a file Explorer (e.g. Windows Explorer) and drag your sources
Drag them to Eclipse and drop them inside the new src folder - if asked select "Copy files"
Eclipse should put the files into the default package, if that's not correct you can edit the offending files (marked with a red cross) by opening them in Eclipse, selecting the package declaration (usually line 1), pressing Ctrl + 1 and selecting the appropriate option (e.g. "Move xy to package com.game"
This answer is going to be for the question
How to create a new eclipse project and add a folder or a new package into the project,
or how to build a new project for existing java files.
Create a new project from the menu
File->New-> Java Project
If you are going to add a new pakcage, then create the same package name here by
File->New-> Package
Click the name of the package in project navigator, and right click, and import...
Import->General->File system (choose your file or package)
this worked for me I hope it helps others.
Thank you.
The easiest method is really good but you don't get a standard Java project, i.e., the .java and .class files separated in different folders.
To get this very easily:
Create a folder called "ProjectName" on the workspace of Eclipse.
Copy or move your folder with the .java files to the "ProjectName" folder.
Create a new Java Project called "ProjectName" (with the Use default location marked).
Press <Enter> and that's it.
There are two things
1- If its already a Eclipse Project, then simply go to File->Import->General->Existing Project into Workplace
2- Otherwise define project type e.g. Java, Web etc
Create a new project of type you define into your workplace. Copy Paste source , lib and other necessary files. refresh, compile and run project in eclipse.
In the package explorer and the navigation screen you should now see the project you created. Note that eclipse will not copy your files, it will just allow you to use the existing source and edit it from eclipse.
There are several ways to add files to an existing Java project in Eclipse. So lets assume you have already created the Java project in Eclipse (e.g. using File -> New -> Project... - and select Java project).
To get Java files into the new project you can do any of the following. Note that there are other ways as well. The sequence is my preference.
Drag the files into the Navigator view directly from the native file manager. You must create any needed Java packages first. This method is best for a few files in an existing Java package.
Use File -> Import... - select File System. Here you can then select exactly which files to import into the new project and in which Java package to put them. This is extremely handy if you want to import many files or there are multiple Java packages.
Copy the fires directly to the folder/directory in the workspace and then use File -> Refresh to refresh the Eclipse view of the native system. Remember to select the new project before the refresh.
The last one is what you did - minus the refresh...
While creating a project from a full folder may or may not work within the workspace, there's a condition outside of the workspace that prevents starting a new project with a full folder.
This is relevant if you use numerous folder locations for sources, for example an htdocs or www folder for web projects, and a different location for desktop Java applications.
The condition mentioned occurs when Eclipse is told to create a new project, and given a full folder outside the workspace. Eclipse will say the folder isn't empty, and prevent creating a new project within the given folder. I haven't found a way around this, and any solution requires extra steps.
My favorite solution is as follows
Rename the full folder with an appended "Original" or "Backup.
Create the Eclipse project with the name of the full folder before the folder was renamed.
Copy all the relabeled full folders contents into the new project folder.
Eclipse should make a new project, and update that project with the new folder contents as it scans for changes. The existing sources are now part of the new project.
Although you had to perform three extra steps, you now have a backup with the original sources available, and are also able to use a copy of them in an existing project. If storage space is a concern, simply move/cut the source rather than fully copy the original folder contents.
If you creating a new project based on an existing Maven structure :
Create the project using a general project wizard and give the project the same name as just created.
If you try to create the project as a Maven project via m2e will receive an error that project/pom already exists.
Create a new project..
Right Click on your project..
Select Build path --> Configure Build Path
Under source tab choose link source, your .java files containing folder..
I am suggesting this since none of the methods that you tried have worked ---FYI
Follow this instructions from standard eclipse docs.
From the main menu bar, select command link File > Import.... The Import wizard opens.
Select General > Existing Project into Workspace and click Next.
Choose either Select root directory or Select archive file and click the associated Browse to locate the directory or file containing the projects.
Under Projects select the project or projects which you would like to import.
Click Finish to start the import.

Categories