如何在Eclipse项目中使用外部类文件 [英] How to Use External Class Files in an Eclipse Project
本文介绍了如何在Eclipse项目中使用外部类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的讲师没有为我们提供教程的.java文件。我的问题是,我如何使用他的类文件在我的eclipse项目,并击败以下错误?
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?
错误:
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$1.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
这是我的代码:
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");
}
}
这里是我的文件夹结构:
随机 /
$
* / bin
* / lec / utils / InputReader
* / src / randomIt.java
And here is my Folder Structure:
Random /
*/ bin
* / lec / utils /InputReader
* / src / randomIt.java
注意:他的类文件是InputReader.class
Note: his class file is "InputReader.class"
推荐答案
我已经玩过一个Eclipse的工作这一个。提供以下内容:
I've had a play with Eclipse to work this one out. Give the following a go:
- 创建以下目录结构(您的桌面会执行)classes / lec / utils
- 将InputReader类文件放在utils目录中。
- 删除您在构建路径中目前拥有的对InputReader的任何引用。
- 使用(右键单击项目)属性 - > Java构建路径 - >库选择添加外部类文件夹,然后选择您在桌面上创建的classes文件夹, 。
- 现在在项目文件夹中的'Referenced Libraries'中,你应该有一个叫做'classes'和一个名为'lec.utils'的包路径,它包含InputReader类。 / li>
- 您可以在自己的类中使用'import lec.utils.InputReader'。
- Create the following directory structure (your desktop will do) classes/lec/utils
- Place the InputReader class file in the utils directory.
- Remove any references you have to InputReader you currently have in your build path.
- 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.
- 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.
- You can use that class using 'import lec.utils.InputReader' in you own class.
希望有助于。
这篇关于如何在Eclipse项目中使用外部类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文