there is one package "UserPackage " which contains java file Main.Java
and inside UserPackage there is another Package named "EqualsAndHashCode" and inside that package there is one java file named EqualTest.java
so dir structure is something like this
---->Parent directory which is going to contain jar file.....
now i am trying to run the commands mentioned before on this structure and i am storing class files of these packages in another directory "UserClasses"
and after that i creates one jar file containing all the class files contained in UserClasses package and then when i invoke this command
java -cp MyJar.jar UserPackage.Main
got it .....thanks a lot for creating my doubt..
so what i understand is we always have to create jar file in the package containing the classes..in short parent directory should not be involved in jar file so that we can access desired classes and packages....
ankur trapasiya wrote:
but sadly saying i understood basic working of jar but this example is still out of mind ...why it was not working in previous case ???
...please explain .....
Unfortunately, you are confusing the matter by telling us different facts. The name of the jar file and the location of the classes directory has changed from your initial post.
To answer you question.... the JVM uses the directory structure of the jar file to find your classes. So, it must be laid out exactly like it's package names -- you cannot change the directory structure, or in your case, have an extra directory in your structure.