0

If I compile and run the program. I don't have any issues.

public class HelloWorld {
    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }
}

If I add the line package ch01.sec01; it complies correctly with javac. However when I try to run it using java I get:

Error: Could not find or load main class HelloWorld

I have tried the following.

export CLASSPATH=/usr/lib/jvm/java-9-openjdk-amd64/bin:/usr/lib/jvm/java-1.9.0-openjdk-amd64/bin
3
  • 1
    We'll need the complete command line you used. Your classname is not HelloWorld it's ch01.sec01.HelloWorld. Commented Jan 25, 2018 at 23:58
  • I used javac HelloWorld.java Commented Jan 26, 2018 at 0:00
  • When you have a package ch01.sec01 the class file is written to cg01/sec01/.... You must use the full name. Commented Jan 26, 2018 at 0:24

1 Answer 1

2

That is why when you use a package in your code, that path must be the actual path of your java file (That means that your code is supposed to be in a directory called sec01 which is inside directory ch01).
With that being set, when running code inside a package, you need to include the path in the command. To do so, after you have compiled your code with javac, navigate to the root of the path (outside ch01 directory) and type

java ch01.sec01.HelloWorld

This should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.