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

11

179

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?

Variegate answered 14/4, 2010 at 9:19 Comment(3)
I suspect that nothing appeared in your workspace because you didn't open the files yet. Open the files from the project directory, and they will appear. It's just a suspicion, though.Susumu
You are talking about .class files, but eclipse only looks for the .java files (the ones you edit). It will create the .class itself. It is very common not to have the sourcefiles in your workspace. You can also just create a new project, that will make a folder in your workspace, where you can then copy your files into (probably the import function in eclipse will also do this for you)Romeyn
be sure to hit f5 (refresh) after copying any files into an eclipse project if you do that while it is running.Deterrence
T
247

Easiest Method:

  1. 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.
  2. Start a new project in eclipse and name it using that same project name.
  3. 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
  4. 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à!

Translation answered 14/2, 2012 at 16:33 Comment(7)
If you look at Eclipse help it appears they also suggest this way.Mastoiditis
beware that if your source is already in the default location and you uncheck "use default location" and browse to your folder, eclipse will complain that there is already a project there and not allow you to proceed.Clammy
Step 3 is wrong. If you untick "use default location" it won't work, as drevicko said.Arbitrator
yeah, in step 3 I had to make sure that 'use default location' is checked to make it work.Classmate
A gotcha -- some versions of project creation have DIFFERENT OPTIONS. For example I did not get the "use default location" checkbox (and got stuck) when I tried to create my project as PHP, but as a generic Eclipse project I was able to get the above working (you can edit the project Natures afterward if needed, but that's a whole different topic...)Blowup
This doesnt work if I am trying to import a set of java files instead of a project.Indiscerptible
One more constraint to keep in mind: Eclipse doesn't allow the workspace folder to be the same as the project folder (i.e. 'ProjectName' folder in the answer above). I moved my 'ProjectName' folder to be under the 'WorkspaceName' folder in order to get it to work. (I presume that the ProjectName folder can be elsewhere as well - it just shouldn't be the same as the WorkSpaceName folder).Comedo
M
8
  1. Right-click in the package explorer and select New - Java Project
  2. Create the new project Game
  3. Open the new project in the package explorer - you should see only the source folder called src (there's nothing inside yet)
  4. Open a file Explorer (e.g. Windows Explorer) and drag your sources
  5. Drag them to Eclipse and drop them inside the new src folder - if asked select "Copy files"
  6. 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"
Microscopy answered 20/9, 2011 at 7:51 Comment(0)
G
4

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.

  1. Create a new project from the menu File->New-> Java Project
  2. If you are going to add a new pakcage, then create the same package name here by File->New-> Package
  3. 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.

Gargantuan answered 10/5, 2012 at 1:57 Comment(0)
C
3

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:

  1. Create a folder called "ProjectName" on the workspace of Eclipse.
  2. Copy or move your folder with the .java files to the "ProjectName" folder.
  3. Create a new Java Project called "ProjectName" (with the Use default location marked).
  4. Press <Enter> and that's it.
Centi answered 15/9, 2014 at 20:55 Comment(0)
H
2

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.

Hales answered 1/7, 2014 at 7:50 Comment(0)
R
0

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.

Romeyn answered 14/4, 2010 at 9:23 Comment(2)
In the "Project" drop down menu "Open project" is not activated. I think it's because I have no projects at the moment. In the "Navigate" drop down menu I see no options related with projects. I see there "Go to", "Open type", "Open Type in Hierarchy" and so on.Variegate
Normally you should have a docked window on the left that shows the projects in your workspace. If this isn't there you can go to the menu: window -> show view -> package explorer.Romeyn
A
0

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...

Ardeen answered 20/8, 2011 at 19:4 Comment(0)
H
0

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

  1. Rename the full folder with an appended "Original" or "Backup.
  2. Create the Eclipse project with the name of the full folder before the folder was renamed.
  3. 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.

Hinman answered 22/9, 2013 at 1:30 Comment(0)
T
0

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.

Transitive answered 18/12, 2013 at 8:35 Comment(0)
U
0
  1. Create a new project..
  2. Right Click on your project..
  3. Select Build path --> Configure Build Path
  4. 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

Upstanding answered 30/6, 2014 at 10:18 Comment(0)
O
-1

Follow this instructions from standard eclipse docs.

  1. From the main menu bar, select command link File > Import.... The Import wizard opens.
  2. Select General > Existing Project into Workspace and click Next.
  3. Choose either Select root directory or Select archive file and click the associated Browse to locate the directory or file containing the projects.
  4. Under Projects select the project or projects which you would like to import.
  5. Click Finish to start the import.
Odyl answered 2/10, 2014 at 15:15 Comment(1)
This seems like how to import an existing project, as opposed to create a new project from existing sourceGestation

© 2022 - 2024 — McMap. All rights reserved.