• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Question on jar files and classpath.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Alex Serna
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Beauty is in the eye of the tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic