Hi Team,

I have two java Files

  1. Print (Package A)

  2. Apple (Package B.C) (Jave file is inside C:\Z\A\B\C\

I have compiled and kept Print.class file inside C:\Z\A\B\A\

Now i have made a Jar file of Print.class named MyJar.jar and kept it in Z\A\

When i try to compile Apple which needs Print class Method, It throws error.

The Program and error or below . Please advice.

C:\Z>javac -cp a/b/MyJar.jar -d A\ A\b\c\Apple.java
A\b\c\Apple.java:3: error: package A does not exist
import A.*;
^
A\b\c\Apple.java:10: error: cannot find symbol
Print obj = new Print();
^
  symbol:   class Print
  location: class Apple
A\b\c\Apple.java:10: error: cannot find symbol
Print obj = new Print();
                ^
  symbol:   class Print
  location: class Apple
3 errors



package B.C;

import A.*;

public class Apple {


public static void main(String[] sam) {

Print obj = new Print();

obj.print();


}


}





package A;

public class Print {



public void print() {


System.out.println("Printing the output");

}



}

** Command i used to create jar file is below **

C:\Z>jar -cf A\MyJar.jar A\B\

C:\Z>jar -tf A\Myjar.jar
META-INF/
META-INF/MANIFEST.MF
A/B/
A/B/A/
A/B/A/MyJar.jar
A/B/A/Print.class
A/B/C/
A/B/C/Apple.class
A/B/C/Apple.java
A/B/C/Orange.class
A/B/C/Orange.java
A/B/Orange.class
A/B/Orange.java

The package A has to be in a place that's in your classpath. If you have C:\Z\A\B\A\Print.class then A.Print is in C:\Z\A\B, so that's what has to be in the classpath.

Ps: Having two directories called A, one inside the other is perfectly legal, but pretty much guaranteed to cause confusion and mistakes.

Hi James,

But i Created a jar file for Print.class and kept it in Z\A directory.

So i no longer need the Print.class File right ? I only need the location of Jar File in classpath right? I read in book that we can put the jar file in any foler as the jar file internally has the dirctory structure of the package.

So if if give the below command, it should work right ,

javac -cp A/MyJar.jar A\B\C\Apple.java

Please clarify

Yes, i n that case the jar should contain a folder A with the class file in that, and the classpath should include the jar file.

My Jar file contains folder named A and print.class file inside it.
Please see the below command. But still when i try to compile it it throws error.

C:\Z\A>jar -tf MyJar.jar
META-INF/
META-INF/MANIFEST.MF
A/B/
A/B/A/
A/B/A/MyJar.jar
A/B/A/Print.class
A/B/C/
A/B/C/Apple.class
A/B/C/Apple.java
A/B/C/Orange.class
A/B/C/Orange.java
A/B/Orange.class
A/B/Orange.java



C:\Z\A>javac -cp MyJar.jar -d . B\C\Apple.java
B\C\Apple.java:3: error: package A does not exist
import A.*;
^
B\C\Apple.java:10: error: cannot find symbol
Print obj = new Print();
^
  symbol:   class Print
  location: class Apple
B\C\Apple.java:10: error: cannot find symbol
Print obj = new Print();
                ^
  symbol:   class Print
  location: class Apple
3 errors

C:\Z\A>

** FYI Jarfile resides in the current directory so i Just gave the name of the jarfile in the classpath field **

This question has already been answered. Start a new discussion instead.