Java 如何从 Eclipse 中的现有源创建一个项目然后找到它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2636201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:10:45  来源:igfitidea点击:

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

javaeclipseproject

提问by Roman

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.

我创建了几个 .java 文件。所有这些都位于一个目录中。我使用文本编辑器来编写这些文件。现在我想切换到 Eclipse。我该怎么做?我尝试了很多方法。它们都不起作用。

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?

我认为常见的方法是在名为“工作区”的 eclipse 文件夹中拥有源代码。那么,我如何在这些目录中获取我的文件。我应该使用 eclipse 从现有源创建一个新项目,Eclipse 会将所有文件放入工作区吗?或者我应该手动将所有文件复制到工作区?我应该把我的类文件放在哪里?我应该创建一个子目录吗?用什么名字?

ADDED 2:

添加 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?

当我尝试创建一个名为“game”的项目时,eclipse 告诉我已经存在一个具有这样名称的项目。但是我怎样才能打开这个项目呢?

ADDED 3:

添加 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 .classpathand .projectfiles in the directory "game". It also created binand srcsubdirectories. 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".

在我的“工作区”中,我创建了一个名为“game”的子目录。我将我所有的 .java 文件复制到这个子目录中。然后使用 Eclipse 我创建了一个名为 game 的新项目。其结果是,Eclipse的创建.classpath.project目录中的“游戏”文件。这也造就binsrc子目录。现在我认为这不是正确的方法。源文件应该在“src”目录中。对?目前我所有的 .java 文件都在“工作区\游戏”中。

ADDED 4:

添加 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?

我是反过来做的。使用 Eclipse,我创建了一个名为“game”的新项目。结果,Eclipse 在文件夹“workspace”中创建了一个名为“game”的文件夹。在“游戏”文件夹中,我找到了“src”文件夹。我将所有 .java 文件复制到此文件夹中。但是现在在“包资源管理器”中,我无法打开“src”文件夹。那么,如何从 Eclipse 访问我的源文件?为什么 Eclipse 不想打开“src”文件夹?

采纳答案by peter

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'.
  1. 将所有源文件放在一个以您的项目命名的目录中。即“ProjectName”您可以将此目录保留在您的工作区中,也可以在其他地方。
  2. 在 eclipse 中启动一个新项目并使用相同的项目名称命名它。
  3. 取消选中“使用默认位置”框并找到您的项目所在的目录,除非您的项目已经在工作区中- 那么您不能取消选中“使用默认位置”框
  4. 点击下一步'。

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à!

Eclipse 应该足够聪明,可以弄清楚发生了什么。单击下一步后,它将显示在该目录中找到的所有文件。它只会自动将这些文件添加到您的项目中。瞧!

回答by Thirler

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.

在包浏览器和导航屏幕中,您现在应该会看到您创建的项目。请注意,eclipse 不会复制您的文件,它只会允许您使用现有源并从 eclipse 中对其进行编辑。

回答by Tonny Madsen

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

有多种方法可以将文件添加到 Eclipse 中的现有 Java 项目。所以让我们假设您已经在 Eclipse 中创建了 Java 项目(例如使用File-> New-> Project...- 和 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.

要将 Java 文件放入新项目,您可以执行以下任一操作。请注意,还有其他方法。顺序是我的偏好。

  • Drag the files into the Navigatorview 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-> Refreshto refresh the Eclipse view of the native system. Remember to select the new project before the refresh.
  • Navigator直接从本机文件管理器将文件拖到视图中。您必须首先创建任何需要的 Java 包。此方法最适合现有 Java 包中的几个文件。
  • 使用File-> Import...- 选择File System。然后,您可以在此处准确选择要将哪些文件导入到新项目中以及将它们放入哪个 Java 包中。如果要导入多个文件或有多个 Java 包,这将非常方便。
  • 将 fires 直接复制到工作区中的文件夹/目录,然后使用File->Refresh刷新本机系统的 Eclipse 视图。记得在刷新前选择新项目。

The last one is what you did - minus the refresh...

最后一个是你所做的 - 减去刷新......

回答by Daniel

  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"
  1. 在包资源管理器中右键单击并选择新建 - Java 项目
  2. 创建新项目游戏
  3. 在包资源管理器中打开新项目 - 您应该只看到名为 src 的源文件夹(里面还没有任何东西)
  4. 打开文件资源管理器(例如 Windows 资源管理器)并拖动您的源
  5. 将它们拖到 Eclipse 并将它们放在新的 src 文件夹中 - 如果询问选择“复制文件”
  6. Eclipse 应该将文件放入默认包中,如果这不正确,您可以通过在 Eclipse 中打开它们,选择包声明(通常是第 1 行),按 Ctrl + 1 并选择有问题的文件(用红叉标记)来编辑它们适当的选项(例如“将 xy 移动到包 com.game”

回答by leonkadruk

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.

如何创建一个新的eclipse项目并在项目中添加文件夹或新包,或者如何为现有的java文件构建一个新的项目。

  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)
  1. 从菜单 File->New->Java Project 创建一个新项目
  2. 如果你要添加一个新的包,那么在这里通过 File->New->Package 创建相同的包名
  3. 在项目导航器中单击包的名称,然后右键单击,然后导入... Import->General->File system(选择您的文件或包)

this worked for me I hope it helps others. Thank you.

这对我有用,我希望它可以帮助其他人。谢谢你。

回答by leonkadruk

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.

如果您对源使用多个文件夹位置(例如,用于 Web 项目的 htdocs 或 www 文件夹,以及用于桌面 Java 应用程序的不同位置),则这是相关的。

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.

当 Eclipse 被告知要创建一个新项目并在工作区外提供一个完整的文件夹时,就会发生上述情况。Eclipse 会说该文件夹不为空,并阻止在给定文件夹中创建新项目。我还没有找到解决这个问题的方法,任何解决方案都需要额外的步骤。

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.
  1. 使用附加的“原始”或“备份”重命名完整文件夹。
  2. 在重命名文件夹之前,使用完整文件夹的名称创建 Eclipse 项目。
  3. 将所有重新标记的完整文件夹内容复制到新项目文件夹中。

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.

Eclipse 应该创建一个新项目,并在扫描更改时使用新文件夹内容更新该项目。现有资源现在是新项目的一部分。

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.

虽然您必须执行三个额外的步骤,但您现在拥有可用原始源的备份,并且还可以在现有项目中使用它们的副本。如果存储空间是一个问题,只需移动/剪切源而不是完全复制原始文件夹内容。

回答by blue-sky

If you creating a new project based on an existing Maven structure :

如果您基于现有的 Maven 结构创建一个新项目:

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.

如果您尝试通过 m2e 将项目创建为 Maven 项目,则会收到项目/pom 已存在的错误。

回答by Shibi

  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..
  1. 创建一个新项目..
  2. 右键单击您的项目..
  3. 选择构建路径 --> 配置构建路径
  4. 在源选项卡下选择链接源,您的 .java 文件包含文件夹..

I am suggesting this since none of the methods that you tried have worked ---FYI

我建议这样做,因为您尝试过的方法都没有奏效---仅供参考

回答by Ijaz Ahmed

There are two things

有两件事

1- If its already a Eclipse Project, then simply go to File->Import->General->Existing Project into Workplace

1- 如果它已经是一个 Eclipse 项目,那么只需转到 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.

2- 否则定义项目类型,例如 Java、Web 等 创建一个您在工作场所中定义的类型的新项目。复制粘贴源、lib 和其他必要的文件。在 Eclipse 中刷新、编译和运行项目。

回答by carlos_lm

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.

最简单的方法确实很好,但是您没有得到标准的 Java 项目,即 .java 和 .class 文件分开在不同的文件夹中。

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 locationmarked).
  4. Press <Enter>and that's it.
  1. 在 Eclipse 的工作区中创建一个名为“ProjectName”的文件夹。
  2. 将包含 .java 文件的文件夹复制或移动到“ProjectName”文件夹。
  3. 创建一个名为“ProjectName”的新 Java 项目(标记为使用默认位置)。
  4. <Enter>,就是这样。