views:

69

answers:

3

Hi all, 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"

+1  A: 

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.

Paul Tomblin
I've tried it, it doesn't work sadly.
Kay
I've placed the lec folder outside the Random folder, and taken the steps you've said, how would i be able to refer to them in my randomIt.java file? import lec/utils.InputReader, import utils.InputReader or something else?
Kay
What is the package name of this InputReader? If it's utils, then you "import utils.InputReader". You never use a slash in an import statement.
Paul Tomblin
A: 

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

geekGod
Your second point makes no sense at all, and he was asking how to build it in Eclipse, not from the command line, so the third point isn't helpful either.
Paul Tomblin
+1  A: 

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

  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.

Hope that Helps.

Binary Nerd