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)
0

2 Answers 2

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.

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

5 Comments

First I compile A.java, then I compile B.java
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.
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
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.
Just in case anyone came here. I'm pretty sure that this question is outdated now. So you should see this link: stackoverflow.com/questions/5757189/… 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)
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

2 Comments

Can you tell us how do you compile? You can edit the question to show us. You cannot edit someone else's answer for commenting.
javac A.java then javac B.java

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.