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?

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

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

Note: his class file is "InputReader.class"

解决方案

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

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.

Hope that Helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值