1
package src.sheet1.question1;

class OneFourTwoOne {

    public static void main(String[] args) {

        assert args.length == 1;

        int x = Integer.parseInt(args[0]);

        System.out.print(x);

        while(x != 1) {
            x = next(x);
            System.out.print(" " + x);
        };

    }

    static int next(int x) {

        return ((x % 2) == 0) ? (x / 2) : (3*x + 1);

    }

}

when I input :java OneFourTwoOne in the terminal, here occurs an error: Error: Could not find or load main class OneFourTwoOne How can I run it?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
overpro
  • 49
  • 8

2 Answers2

3

Java cannot run your class because it is in a package. Java expects the package name to be matched by the path to your file.

Create a directory structure that looks like this:

src
  +-sheet1
     +-question1
        +-OneFourTwoOne.java

and compile your class into OneFourTwoOne.class. Now switch the parent directory of src, and execute the command

java src.sheet1.question1.OneFourTwoOne
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Java cannot run your class because it is in a package. Java expects the package name to be matched by the path to your file.