Java 如何在 Eclipse 项目中使用外部类文件

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

How to Use External Class Files in an Eclipse Project

javaeclipseclass

提问by Kay

My lecturer didn't provide us with the .java files for a tutorial. My question is, how would i use his class files in my eclipse project, and defeat the following error?

我的讲师没有为我们提供教程的 .java 文件。我的问题是,我将如何在我的 eclipse 项目中使用他的类文件,并克服以下错误?

Error:

错误:

  Exception in thread "main" java.lang.NoClassDefFoundError: lec/utils/InputReader
 at randomIt.main(randomIt.java:17)
    Caused by: java.lang.ClassNotFoundException: lec.utils.InputReader
     at java.net.URLClassLoader.run(Unknown Source)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.net.URLClassLoader.findClass(Unknown Source)
     at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
     at java.lang.ClassLoader.loadClass(Unknown Source)
     ... 1 more

Here is my code:

这是我的代码:

 import java.util.Random;
    import lec/utils.InputReader;

    public class randomIt {

 public static void main(String[] args) {
  Random generator = new Random();
  InputReader myReader = new InputReader();
  //Pick a number randomly between 1 and 10!
   int number = generator.nextInt(10)+1;
  //Ask user to guess...!
   System.out.println("Take a guess (1 to 10)");
   if (number == myReader.readInt()){
    System.out.println("You win");
   }
   else {
    System.out.println("It was " + number + ", tough Luck");
  }
 }

And here is my Folder Structure:
Random/
*/ bin
* / lec / utils /InputReader
* / src / randomIt.java

这是我的文件夹结构:
Random/
*/ bin
* / lec / utils /InputReader
* / src / randomIt.java

Note: his class file is "InputReader.class"

注意:他的类文件是“InputReader.class”

回答by Paul Tomblin

In the project configuration menu, there is a "Build Path->Configure Build Path" menu item. Within that, there is an option to add an "external class folder". Put all the provided class files in a folder, and add that folder to the build path.

在项目配置菜单中,有一个“Build Path->Configure Build Path”菜单项。其中,有一个选项可以添加“外部类文件夹”。将所有提供的类文件放在一个文件夹中,并将该文件夹添加到构建路径中。

回答by rajeshnair

You should make the following changes

您应该进行以下更改

  1. Modify your randomIt class to have following include line (no lecs/ ) import utils.InputReader

  2. Modify the filename as rnadmIt.java (and not randomit.java) . The class name and fie name has to be exactly same. Also per Sun convention the class should start with a capital letter

  3. $ cd Random $ javac -classpath ./lec src/randomIt.java

  1. 修改您的 randomIt 类以具有以下包含行(无 lecs/ ) import utils.InputReader

  2. 将文件名修改为 rnadmIt.java(而不是 randomit.java)。类名和文件名必须完全相同。此外,根据 Sun 约定,该类应以大写字母开头

  3. $ cd 随机 $ javac -classpath ./lec src/randomIt.java

回答by Binary Nerd

I've had a play with Eclipse to work this one out. Give the following a go:

我玩过 Eclipse 来解决这个问题。试一试:

  1. Create the following directory structure (your desktop will do) classes/lec/utils
  2. Place the InputReader class file in the utils directory.
  3. Remove any references you have to InputReader you currently have in your build path.
  4. Using (right click on project) Properties->Java Build Path->Libraries select the 'Add external class folder' and select the 'classes' folder you created on your desktop and click OK.
  5. Now in the 'Referenced Libraries' in the project folder you should have one called 'classes' and a package path under that called 'lec.utils' which contains the InputReader class.
  6. You can use that class using 'import lec.utils.InputReader' in you own class.
  1. 创建以下目录结构(你的桌面就可以)classes/lec/utils
  2. 将 InputReader 类文件放在 utils 目录中。
  3. 删除当前在构建路径中对 InputReader 的所有引用。
  4. 使用(右键单击项目)Properties->Java Build Path->Libraries 选择“添加外部类文件夹”并选择您在桌面上创建的“类”文件夹,然后单击“确定”。
  5. 现在,在项目文件夹中的“Referenced Libraries”中,您应该有一个名为“classes”的文件和一个名为“lec.utils”的包路径,其中包含 InputReader 类。
  6. 您可以在自己的课程中使用“import lec.utils.InputReader”来使用该课程。

Hope that Helps.

希望有帮助。