在 Eclipse 中导入类/java 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20170470/
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-12 23:42:04  来源:igfitidea点击:

Importing class/java files in Eclipse

javaeclipse

提问by Manish Giri

I'm new to Java Programming using Eclipse, so would really appreciate your help for this question.

我是使用 Eclipse 进行 Java 编程的新手,因此非常感谢您对这个问题的帮助。

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.

我在大学和家里都做过 Java 编程。在家里,我在一个项目(Java 参考)下拥有我所有的 Java 类。现在,在大学里,java 程序是在实验室机器上编译的。我真的很想在我的笔记本电脑上的一个项目中拥有所有这些程序。

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.

我最近从我们在大学实验室中完成的程序中复制了所有 .class 和 .java 文件,因此我可以将它们放在笔记本电脑的“Java 参考”项目下。我在笔记本电脑上复制了 java 项目的“bin”和“src”文件夹下的 .class 和 .java 文件。但是,我无论如何都看不到通过 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.

从我在这里读到的内容来看,似乎可以通过导入选项将整个项目(在其他地方创建)复制到一个地方,但在我的情况下,我只有 .class 和 .java 文件。

采纳答案by Guy Gavriely

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.

在Eclipse中新建一个java项目并将.java文件复制到它的src目录下,如果你不知道这些源文件应该放在哪里,右键单击项目的根目录并选择new->class来创建一个测试类并查看它的.java文件放在哪里,然后把其他文件放在同一个目录下,你可能需要根据新的项目目录结构调整那些源文件中的包。

if you use external libraries in your code, you have two options: either copy / download jar files or use mavenif 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...

如果您在代码中使用外部库,您有两个选择:复制/下载 jar 文件或使用maven如果您使用 maven,您必须首先在 maven 项目中创建项目,将 java 项目创建为 maven 项目无论如何要走的路,但那是另一篇文章......

回答by m0skit0

First, you don't need the .class files if they are compiled from your .java classes.

首先,如果它们是从 .java 类编译的,则不需要 .class 文件。

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.

要导入文件,您需要创建一个空的 Java 项目。他们要么一一导入(新建 -> 文件 -> 高级 -> 链接文件),要么直接将它们复制到相应的文件夹/包中并刷新项目。

回答by Abhijeet

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.

我有同样的问题。但是我所做的是导入 .java 文件,然后转到 Search->File-> 然后将包名更改为它应该属于的任何包,这样我修复了很多 java 文件,否则这些文件需要去到每个文件并手动更改它们。

回答by Vahidn

You can import a bunch of .java files to your existing project without creating a new project. Here are the steps:

您可以将一堆 .java 文件导入现有项目,而无需创建新项目。以下是步骤:

  1. Right-click on the Default Packagein the Project Manager pane underneath your project and choose Import
  2. An Import Wizard window will display. Choose File system and select the Next button
  3. You are now prompted to choose a file
  4. Simply browse your folder with .java files in it
  5. Select desired .java files
  6. Click on Finishto finish the import wizard
  1. 右键单击项目下方项目管理器窗格中的默认包,然后选择导入
  2. 将显示导入向导窗口。选择文件系统并选择下一步按钮
  3. 现在会提示您选择一个文件
  4. 只需浏览包含 .java 文件的文件夹
  5. 选择所需的 .java 文件
  6. 单击完成以完成导入向导

Check the following webpage for more information: http://people.cs.uchicago.edu/~kaharris/10200/tutorials/eclipse/Step_04.html

查看以下网页了解更多信息:http: //people.cs.uchicago.edu/~kaharris/10200/tutorials/eclipse/Step_04.html

回答by Shu Zhang

import class folder does not work for me, but add jar worked!

导入类文件夹对我不起作用,但添加 jar 起作用了!

1. put the class folder under the project folder

1.将class文件夹放在项目文件夹下

2. Zip the class folder

2. 压缩类文件夹

3. Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars".

3. 突出项目名称,点击顶部工具栏中的“项目”,点击“属性”,点击“库”选项卡,点击“添加外部jar”。

4. Add the zip file. Done!

4. 添加压缩文件。完毕!