aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on jar files and classpath. 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 "Question on jar files and classpath." Watch "Question on jar files and classpath." New topic
Author

Question on jar files and classpath.

Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58
Hi everyone,

I have the following directory structure:

test
|-- GetJar.class
|-- GetJar.java
|-- myApp
| |-- Foo.class
| `-- Foo.java
`-- test.jar

And the test.jar file content is this:

test/myApp/Foo.java
test/myApp/Foo.class
test/test.jar
test/GetJar.java
test/GetJar.class

The GetJar.java and Foo.java code is the following:



And the current directory is test.

I didn't know that if you have a package sentence in the class file you are trying to get from the jar you had to write the package's name before the class' name in the java command. I thought the class search within a jar file was automatic.. This command will find the GetJar.class in the jar file but will fail to find Foo.class:
java -cp test.jar test.GetJar

My question is, is there a way to get Foo.class from the jar file in the same command? I know I could get it right adding ':.' to the -cp, but I would like to figure it out if I can do it only with the jar file.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

I'm not perfectly sure what you are trying to say here, but I'm quite sure that the java command that you used will not work. There are many things that I can see wrong or you are confused with them.

1. If you want to use the GetJar.class file which is not in the jar file, then the classpath at runtime will have to include the current directory i.e. :. at the end
2. You are running the java GetJar command from the test directory, so it will not be able to find the GetJar.class, you need to run the command from the parent directory of test directory
3. The Foo class in your jar file is in test folder, for JVM to be able to find that class, it needs to be in the myApp folder directly under the jar file.

What you need to do is

1. Create a jar file which contains this structure\

Now create this directory structure

Now from the directory anyDirectory i.e. the parent directory of test directory, run this command


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Alex Serna
Ranch Hand

Joined: Sep 18, 2009
Posts: 58

In the first place, thank you for your response. I'm not getting something right about the rules concerning with jar files and I'm trying to find out what it is.

1. If you want to use the GetJar.class file which is not in the jar file, then the classpath at runtime will have to include the current directory i.e. :. at the end


But GetJar.class is in the jar file. It's the last line test/GetJar.class

2. You are running the java GetJar command from the test directory, so it will not be able to find the GetJar.class, you need to run the command from the parent directory of test directory


I see what you mean. If I want to use GetJar.class from the directory tree I have to run the 'java' command from the parent directory since GetJar.class is in the test package. But my intention is to use only the class files within the jar(if it's possible).

3. The Foo class in your jar file is in test folder, for JVM to be able to find that class, it needs to be in the myApp folder directly under the jar file.


Ok, this might explain most of it. Does that mean that if I have a class file with a package sentence the jar file should be like the parent directory for the package structure?
So, if I have a file within the jar that has a package sentence like com.mypackage.es it should appear in the jar like this 'com/mypackage/es/myfile.class' and not 'dir1/com/mypackage/es /myfile.class, right? If it works like that I can't use the same jar for packages with different roots, can I?.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Alex Serna wrote:So, if I have a file within the jar that has a package sentence like com.mypackage.es it should appear in the jar like this 'com/mypackage/es/myfile.class' and not 'dir1/com/mypackage/es /myfile.class, right?

Yes you got it this time

If it works like that I can't use the same jar for packages with different roots, can I?.

You can have jars with more than one roots, so in your jar you can have this structure

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on jar files and classpath.