File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in compilation with classpath of jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in compilation with classpath of jar file " Watch "Problem in compilation with classpath of jar file " New topic
Author

Problem in compilation with classpath of jar file

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

here is the link of the source code for my program. since it is dealing with packages so i have put source code here...

http://rapidshare.com/files/433650472/Source.zip


now firstly i compiled this file using

javac -d UserPackageClasses UserPackage/Main.java UserPackage/EqualsAndHashCode/EqualsTest.java

compiled fine and all class and directory structure got put in UserPackageClasses....

now i run this code using

java -cp UserPackageClasses UserPackage.Main

//runs fine

now i created one jar file like this

jar -cf UserClasses.jar UserPackageClasses

one jar file got created in current directory

but ClassNotFoundException occurs when i am using classpath in java statement like this

java -cp UserClasses.jar UserPackage.Main

can anyone give solution to this problem please??

thanks in advance....


OCPJP(83%)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

ankur trapasiya wrote:
now i created one jar file like this

jar -cf UserClasses.jar UserPackageClasses

one jar file got created in current directory



BTW, I just confirmed the jar command. You need to run this instead....

cd UserPackageClasses
jar -cf ..\UserClasses.jar *

This will make sure that the UserPackageClasses directory doesn't get into the jar file.


[EDIT: Sorry accidentally deleted the reference asking for a "jar tvf" call. I wanted to do a post and did an edit by accident]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

hey henry .... it is not working ..

http://rapidshare.com/files/433659926/Error.zip


here is snapshot of the runtime FileNotFoundException that i got by running

"jar -tvf UserClasses.jar "
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40


If you would like my help, then provide the details here... as I don't want to create any accounts on a file sharing website.

Henry
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

okk..

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.....
--->UserPackage
----> Main.java
-------->EqualsAndHashCode
--------> EqualsTest.java
---->UserClasses
--->contains classes

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

I accidentally did an edit instead of a reply. See above.

Basically, you created the jar file wrong. If you did exactly what you said you did, then you accidentally added the top directory into the jar file.


BTW, you never answered my question... why is the output from the jar listing of the jar file that you created? And since you seemed to have renamed your jar file, the output of....

jar -tvf MyJar.jar

Henry
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

hey jar file is not created using the command

jar -cf ..\MyJar.Jar* //gives error
jar -cf ..\MyJar.jar UserClasses // works but jar file is not created


now let me tell you the contents of the previously created jar file

META.....
....
UserClasses/
UserClasses/EqualsAndHashCode/
UserClasses/EqualsAndHashCode/EqualsTest.class
UserClasses/UserPackage/
UserClasses/UserPackage/Main.class

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

hey i got the output using your command

cd UserClasses // i didn't do previously that's why error was there
jar -cf MyJar.Jar *


now
cmd > cd..
cmd > java -cp MyJar.Jar UserPackage.Main
//runs fine and gives desired output

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 .....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

ankur trapasiya wrote:
jar -cf ..\MyJar.jar UserClasses // works but jar file is not created


Actually it does. It replaces the jar file that you created previously that is located one directory higher.

ankur trapasiya wrote:
now let me tell you the contents of the previously created jar file

META.....
....
UserClasses/
UserClasses/EqualsAndHashCode/
UserClasses/EqualsAndHashCode/EqualsTest.class
UserClasses/UserPackage/
UserClasses/UserPackage/Main.class



This is not correct. The class that you are trying to run is "UserPackage.Main", so the Main class needs to be in the UserPackage dir, which needs to be a top level directory in the jar file.

Henry
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

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....

Thanks again.....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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.

Henry
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

completely got it..... .

thanks a lot Mr.Henry...
 
GeeCON Prague 2014
 
subject: Problem in compilation with classpath of jar file