sk92
sk92

Reputation: 1

Ambiguity with package statement in java

Consider the following code:

package com.a.b;

class Test {
    public static void main(String[] args) {
         System.out.println("Hello to the world of packages");   
    }
}

I've compiled the program like so:

javac Test.java

This created Test.class file in the current working directory. How should I run the program now? I tried:

java Test

but I'm getting a "No class def found error". If I compile this way:

javac -d . Test.java

It's creating directory structure, then I'm able to run through

java com.a.b.Test

If I can compile without directory hierarchy, why can't I execute?

Upvotes: 0

Views: 160

Answers (4)

Andrew Li
Andrew Li

Reputation: 57964

In your first command:

javac Test.java

It compiles fine because there are no errors and places the Test.class file in the current directory, because that's where Test.java resides, as per the documentation:

If the -d option is not specified, then javac puts each class file in the same directory as the source file from which it was generated.

The source file is in the current directory so the class file is placed in the current directory. Now when you execute like so:

java Test

There's an error because you must use the fully qualified class name. You specified a package, and even though it's in the current directory, you still must use the fully qualified name to specify where it is. Without the full name, Java can't find it an execute it, thus the error. You can do the following:

java com.a.b.Test

Java will find it appropriately and all will execute fine.


Now, I would recommend using the -d option to correctly place your class files for organization. I would also recommend actually creating the packages you specify, so put your Test.java file in the directory ./com/a/b.

Upvotes: 3

anacron
anacron

Reputation: 6721

You must always run your Java program with the fully qualified class name:

It doesn't matter whether you compile it using the javac -d option.

So, in your case, it will be:

java com.a.b.Test

It is a best practice to compile it with the -d option and store .class files in the proper directory structure as per the package name.

Update

Also, ensure your current directory is in the class-path.

Try running this:

java -cp . com.a.b.Test

Update 2

When the -d option is not used with javac, the class files are created in the current directory. You will have to move/copy the class file(s) manually to the appropriate directory and then execute them. The -d option is a short cut of achieving this.

Upvotes: 2

user3900146
user3900146

Reputation: 65

1) Add your class to the java build-path 2) Run it with the full path.

Upvotes: -1

talex
talex

Reputation: 20465

Java search for classes in classpath. By default classpath contains only currrent directory.

When Java search for class it iterates thru classpath element. If element is directory it search file with name of the class end extension .class in some subdirectory. To be precise this subderectory is found by replasing dots to directory separation simbol (/ or \ depending on your operation system) and resolving result.

Upvotes: 0

Related Questions