For example, I have a class called Foo
inside Foo.java
which will use a class called Bar
inside ../Bar.java
. Is there any
Add your class to classpath..
javac -cp "path to your Bar.class" Foo.java
You will need to import that class in your Foo.java
also.. Better use a package, and give the classpath till the directory containing your package..That way you will be able to give different namespaces to your classes..
So, if your package is: - pkg1.pkg2.Bar
and you have saved your .java
to a directory named Demo
, then your classpath should contain path till Demo
.. And your classes will actually be under two more directory pkg1/pkg2/Bar.class
inside Demo
directory..
Demo+
|
+-- B.java (`Under package pkg1.pkg2)
|
+--pkg1+
| |
| +--pkg2+
| |
| +-- B.class
|
+-- A.java (`Under no package`) - Add - import pkg1.pkg2.B
|
+-- A.class (javac -cp . A.java) - Will search the package pkg1.pkg2 in current directory
Even though .
is not needed there, you can replace it with any path, if your B.class
is somewhere else..