16

HI i have 3 java files

a.java  
b.java  
c.java  

I managed to generate .class files for both a and b using

javac example/a.java  
javac example/b.java  

but when i do the same for c.java I get the error error: cannot find symbol b and c

Any suggestions on how i could solve this problem ?

All the java files are in the same folder

3
  • 2
    do you want to show us the code of c.java? Commented May 7, 2012 at 0:41
  • Actually a and b were suppositions and c is actually Order.java Here is the code Customer,Address,Shipping and Item are the other objects which compiled without any trouble. Since i couldnt paste the code here i posted the code at link Commented May 7, 2012 at 0:48
  • Related link: How the Java Launcher Finds User Classes. Commented Sep 8, 2020 at 3:42

2 Answers 2

24

You have to have classes a and b in your classpath when you try to compile class c. This allows the compiler to verify that they exist, figure out what methods they have, etc.

javac is pretty sensitive to package names and classpaths. The easiest thing to do is to compile all three at the same time like so javac example/a.java example/b.java example/c.java.

If you go to the parent directory of example (let's call it src), then you can run the following:

javac -cp src src/example/c.java

The reason you have to do it this way is because your classes have their packages listed as example. Because of your package name, javac is looking for the example directory in its classpath, where it expects to find a.class and b.class.

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

2 Comments

java -cp example c.java (Invalid syntax and path)
javac example/a.java example/b.java example/c.java or javac *.java worked
2

Presumably you're not in the example/ directory when you run javac. Try

javac -cp example c.java

Or just cd into that directory. The classpath is not automatically resolved for the classes c.java depends on.

2 Comments

If his package is example that may make a problem
@Amir Afghani Have a look at the last post at link

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.