Dani Petrick
Dani Petrick

Reputation: 359

Compilation error when using Java Package?

I know this question is pretty basic, but after several times googling I can't find the answer. I am new in Java. Today I learn about java package. I have class A like so:

package hello;

public class A {

}

and I also have class B that used class A :

package hello;

public class B {
    public static void main(String[] args) {
        A a = new A();
    }
}

class A and B i place in "hello" folder. When I compile B, i got error like this:

B.java:5: error: cannot find symbol
                A a = new A();
                ^
  symbol:   class A
  location: class B
B.java:5: error: cannot find symbol
                A a = new A();
                          ^
  symbol:   class A
  location: class B
2 errors

Edit: In cmd I type

>>javac A.java

>>javac B.java

    B.java:5: error: cannot find symbol
                    A a = new A();
                    ^
      symbol:   class A
      location: class B
    B.java:5: error: cannot find symbol
                    A a = new A();
                              ^
      symbol:   class A
      location: class B
    2 errors

I try compile using this command:

>>javac *.java

>> java B
Exception in thread "main" java.lang.NoClassDefFoundError: B (wrong name: hello/
B)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        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)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Upvotes: 0

Views: 1301

Answers (2)

Alexander Lin
Alexander Lin

Reputation: 136

Try compiling both together: javac *.java while being inside hello directory.

If you compile B without A, java doesn't know about A, so you get the errors. If you compile both (using *.java), javac can link properly.

To run, try this: Go one level out of hello (as in you can see hello the folder). Then type java hello.B. It should work. The reason for this is because since we have packaged it under hello, java expects the FQCN (fully-qualified-class-name), telling it that B is found in folder hello.

Upvotes: 1

Narendra Pathai
Narendra Pathai

Reputation: 41975

Its not compile error but execution error.

Run it like this java hello.B

This is because your Main class B is in package hello. So for referring to that you will have to say hello.B

Upvotes: 1

Related Questions