• Post Reply Bookmark Topic Watch Topic
  • New Topic

How java finds jar contents imported in a source file.  RSS feed

 
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • 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: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or this Tutorial might help.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • 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: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!