0

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)
Dani Petrick
  • 359
  • 2
  • 4
  • 13

2 Answers2

1

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.

  • First I compile A.java, then I compile B.java – Dani Petrick Nov 16 '13 at 05:32
  • did you try either of the two commands I or Narendra suggested? namely, javac -cp . or javac *.java instead of your two commands? The point is that you cannot compile them separately, since one has dependencies on the other. Our commands should resolve this dependency since both are done 'together', so to speak. – Alexander Lin Nov 16 '13 at 05:35
  • Yes i did. I compile using command like javac *.java. It's run without error warning, but when I run class B, I got many error – Dani Petrick Nov 16 '13 at 05:38
  • So 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. – Alexander Lin Nov 16 '13 at 05:49
  • Just in case anyone came here. I'm pretty sure that this question is outdated now. So you should see this link: https://stackoverflow.com/questions/5757189/how-do-i-run-java-class-files for running the scripts. It says that you should state where is the classpath by going like `java -cp C:\Users\ImAUserBoiiis\hello B` (`-cp` is the same as `-classpath`) – ZiyadCodes Sep 01 '22 at 18:03
1

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

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120