简体   繁体   English

在Eclipse中导入类/ java文件

[英]Importing class/java files in Eclipse

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. 我在大学和家里都做过Java编程。 At home, I have all my Java classes under a single project-(Java Reference). 在家里,我将所有Java类都放在一个项目中 - (Java Reference)。 Now, in college, the java programs are compiled on the lab machines. 现在,在大学里,java程序是在实验室机器上编译的。 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. 我最近从大学实验室的程序中复制了所有.class和.java文件,所以我可以在笔记本电脑的“Java Reference”项目下使用它们。 I copied the .class and .java files under the "bin" and "src" folders of the java project, on my laptop. 我在我的笔记本电脑上复制了java项目的“bin”和“src”文件夹下的.class和.java文件。 However, I don't see anyway to import them through Eclipse. 但是,我还没有看到通过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文件。

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. 要导入文件,需要创建一个空的Java项目。 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. 您可以逐个导入它们(新建 - >文件 - >高级 - >链接文件),也可以直接将它们复制到相应的文件夹/包中并刷新项目。

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 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... 如果你在代码中使用外部库,你有两个选择:复制/下载jar文件或使用maven如果你使用maven你必须首先在maven项目创建项目,创建java项目作为maven项目是无论如何要走的路,但那是另一个帖子......

You can import a bunch of .java files to your existing project without creating a new project. 您可以将一堆.java文件导入到现有项目中,而无需创建新项目。 Here are the steps: 以下是步骤:

  1. Right-click on the Default Package in 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 只需使用.java文件浏览您的文件夹即可
  5. Select desired .java files 选择所需的.java文件
  6. 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 有关更多信息,请访问以下网页: http//people.cs.uchicago.edu/~kaharris/10200/tutorials/eclipse/Step_04.html

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文件,否则需要去到每个文件并手动更改它们。

import class folder does not work for me, but add jar worked! 导入类文件夹对我不起作用,但添加jar工作!

1. put the class folder under the project folder 1.将类文件夹放在项目文件夹下

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.突出显示项目名称,单击顶部工具栏中的“项目”,单击“属性”,单击“库”选项卡,然后单击“添加外部罐”。

4. Add the zip file. 4.添加zip文件。 Done! 完成!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM