How to use external class files in an Eclipse project

My teacher did not provide us .java files for the tutorial. My question is: how can I use its 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: its class file is "InputReader.class"

+3
source share
3 answers

I had a game with Eclipse to work on this. Give the following:

  • Create the following directory structure (your desktop will do) classes / lec / utils
  • InputReader utils.
  • , InputReader, .
  • ( ) Properties- > Java Build Path- > " " "classes", , "".
  • " " "classes" "lec.utils", InputReader.
  • , "import lec.utils.InputReader" .

, .

+6

" → ". " ". .

+2

  • randomIt, ( lecs/) import utils.InputReader

  • rnadmIt.java( randomit.java). fie . Sun

  • $cd $ javac -classpath./lec src/randomIt.java

0

Source: https://habr.com/ru/post/1730779/


All Articles