• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How java finds jar contents imported in a source file.

 
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused about how java imports libraries from a jar file inside a source. Let's say I have the following code on Something.java file,


And the Something.java is inside the following structure...



To compile this code(Something.java) from command line I want to use the following command,

And for the compilation to be successful, I'll have to be just above bin-folder because...
- > Only then my default class path will be (.) and java would automatically include A.jar and B.jar(the other way is to add the jar files using -cp argument, which I want to avoid in this scenario)
- > And for the import org.mycompanyX to work I'll have to be in the (.) directory.

Are my assumptions correct about how java find jar contents imported in a source file?
 
Sheriff
Posts: 28397
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Quazi Irfan wrote:- > Only then my default class path will be (.) and java would automatically include A.jar and B.jar(the other way is to add the jar files using -cp argument, which I want to avoid in this scenario)



No, this is incorrect. The Java compiler will never automatically include any jar files in your classpath. If you want the compiler to be able to access files in a jar, then you have to put that jar into your classpath. Usually you'd use the -cp argument... I don't see why you would want to avoid that.
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is that Java uses the ClassPath to find resources. This can be set from the command line using the "-cp" option, or using the CLASSPATH environment variable.
 
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doc should help you understand some options > http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#options
 
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or this Tutorial might help.
 
Quazi Irfan
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:The Java compiler will never automatically include any jar files in your classpath. If you want the compiler to be able to access files in a jar, then you have to put that jar into your classpath. Usually you'd use the -cp argument... I don't see why you would want to avoid that.



Yes, but the default class path is (.), and if the jar files are on the default classpath, then those would get automatically include into my program...right?

Liutauras Vilda wrote:This doc should help you understand some options > http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#options


Campbell Ritchie wrote:Or this Tutorial might help.



I skimmed over both, but I couldn't find what I am looking for.

I guess the source of my confusion is how I can access contents of jar file without referencing the jar file itself.

For exampe, say I have a jar file with following structure,



I can access to ClassA using code import org.ClassA or import org.* (considering my jar and my source file are on the directory same level), but why I don't have to use the name of the jar file MyJar?
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...but why I don't have to use the name of the jar file MyJar?



It's what's in the Jar file that's important, not the name of the Jar file. Why? Because that's the way it was setup. Did you know that java.util is in a jar file called rt.jar? No? Well, that's because you don't have to. Isn't that nice?
 
Paul Clapham
Sheriff
Posts: 28397
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Quazi Irfan wrote:

Paul Clapham wrote:The Java compiler will never automatically include any jar files in your classpath. If you want the compiler to be able to access files in a jar, then you have to put that jar into your classpath. Usually you'd use the -cp argument... I don't see why you would want to avoid that.



Yes, but the default class path is (.), and if the jar files are on the default classpath, then those would get automatically include into my program...right?



No. Recall that I said

The Java compiler will never automatically include any jar files in your classpath.



 
Paul Clapham
Sheriff
Posts: 28397
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Quazi Irfan wrote:I guess the source of my confusion is how I can access contents of jar file without referencing the jar file itself.



You do that by having the jar file in your classpath. Then the compiler, when it sees you want to import a class, will go through all the jar files in your classpath and look for that class.
 
Quazi Irfan
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:It's what's in the Jar file that's important, not the name of the Jar file. Why? Because that's the way it was setup. Did you know that java.util is in a jar file called rt.jar? No? Well, that's because you don't have to. Isn't that nice?



That really cleared things up. Thank you.

Two more questions, say I am using a jar names A.jar from a friend, and I am rolling my own jar B.jar



I was under the assumption that when using package syntax package A at the top of ever classes of A - it's encapsulating every classes of A.jar inside A namespace to avoid collision.

Now If I want to access classes from both jar using import command - how would I do that?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The jar file names have nothing to do with import statements or namespaces.
Both the compiler and runtime look through jar files in order until they find a class that matches.

So, if your two jar files both had org.containers.SomeClass in them then whichever jar file is encountered first when compiling or running will be the one used.

This can happen with large projects accidentally importing different versions of another project...
 
Quazi Irfan
Ranch Hand
Posts: 146
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:The jar file names have nothing to do with import statements or namespaces.



But I knew the folders structures are used to maintain package hierarchy in java. i.e. If I have the containers class in package org, I'll have to put the containers class inside org folder.

And to create jar, I am accessing the package structure like this, jar cf A.jar org/contains/Someclass.class

Dave Tolls wrote:...whichever jar file is encountered first when compiling or running will be the one used.

This can happen with large projects accidentally importing different versions of another project...



This sounds really inconvenient. Can you redirect me to a more detailed document explaining this.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic