• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

difference between different lib folders in jdk and jre

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to compile an application dependent on few external jar and added them to jdk1.7.0_21\lib firstly and compiled the code. However it threw error class not found.

To my surprise when i added the jars to jre\lib the application compiled successfully. My questions is when the program is being compiled why would java require jars of jre.. as jre is only used for running the java applicatoins..

Below is the list of the directories which i am really consued to work with:
1. jdk1.7.0_21\lib
2. jdk1.7.0_21\jre\lib
3. jdk1.7.0_21\jre\lib\ext
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The jre/lib directory is the one that contains the java jars but you shouldn't be adding anything to the either lib directory. If you want to add general purpose jars etc then put them in the jre/lib/ext dir.

Read this article which explains all about it: http://docs.oracle.com/javase/tutorial/ext/basics/install.html
 
Avinash Haridasu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply! I found good information on the jar's.

Below is what i am trying to do:

1. Added the commons jars to the jdk lib
2. use the javac command to compile, which throws out errors

The code compiles and runs successfully when i add few external api jar's like commons to the classpath or jre/lib/ext.

javac is a command of JDK not JRE so why is the javac command picking up jars from the ext folder of jre when we have an internal lib folder of jdk.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

javac is a command of JDK not JRE so why is the javac command picking up jars from the ext folder of jre when we have an internal lib folder of jdk.


Have you tried looking in the two lib folders and see what they contain and why do you think the jdk/lib folder should contain the java runtime jars?.
 
Avinash Haridasu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

javac is a command of JDK not JRE so why is the javac command picking up jars from the ext folder of jre when we have an internal lib folder of jdk.


Have you tried looking in the two lib folders and see what they contain and why do you think the jdk/lib folder should contain the java runtime jars?.



Yes, i did check two folders present in jdk and Jre.

I am not expecting the jre jars to be present in the jdk/lib folder as all the files present in the jdk is only for compiling the application and for running the application we use jre.

Here i am not trying to run the java file, i am just trying to compile the java file i have which depends on few 3rd party api's.

My only concern is when a file is getting compiled why is the javac checking for libraries (3rd party api's) present in the ext/lib folder of jre instead of the files present in the lib folder of jdk.

I have first added the 3rd party jars to the lib folder of jdk and then compiled the application which resulted in class not found errors. However when i copy the jars to the ext folder of jre the "compiling" is successful and also copying the files to jre/lib also did not work only copying to ext folder worked.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't put JAR files in the JDK or JRE lib folder. The Java compiler is not going to pick up the libraries that you put there automatically, so it will not solve your problem. Besides that, it's just a bad idea to pollute the JDK's or JRE's lib directory with your own JAR files.

Instead, put the JAR files in the classpath. The easiest thing you can do is put all the JAR files that you need in a directory (any directory, that you create yourself) and use the -cp option with a * to include all the JARs in the directory. For example:

javac -cp .;C:\MyProject\lib\* MyProgram.java
 
Avinash Haridasu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information..

How does the javac (not java) pick the libraries it needs, to compile the application given to it.

What i am still not clear is why the javac command is compiling the application when the libraries are added to jre\ext.

My only concern is when we issue a javac command it should search for libraries in the folders present in the jdk, why is the jdk command java getting into the ext folders of jre and then trying to compile the application...

javac is specific to jdk, so it should search for libraries present in -classpath only. But i have not added the jars to classpath instead i have added to jre/ext folder and then compiled the application.
Now the compilation is successful.

I am not understanding why the javac command is picking jars in jre/ext..
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Avinash Haridasu wrote:How does the javac (not java) pick the libraries it needs, to compile the application given to it.


By looking at the classpath.

Avinash Haridasu wrote:What i am still not clear is why the javac command is compiling the application when the libraries are added to jre\ext.


The ext directory is a special directory, the files in there are automatically added to the classpath. This is the extension mechanism. But it's better not to use that, because whatever you put in there is used for any Java program you run, and that can sometimes cause problems. It's not the normal way you add JAR files that you need for your project.
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP's problem reminds me the time when I was a Java beginner and trying to put my jars around JDK/JRE lib folders. That was beginners' mistake. Avoid that.
I think a good practice is, you should bring your external jars alongside with your application (desktop app) in a separate directory (lib or libs) which is under same location of the application's jar file.

This is also for portability, e.g. when you install your app on a new computer, just copy the app's directory and it will run. No one tries to find where the JDK/JRE is and put jars under its lib directory.
 
Avinash Haridasu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to know what all the jar files which got loaded/ added to classpath while the java program is executing..
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you could write your own ClassLoader which kept track of which jar files it had loaded classes from (although I'm not sure how you would handle classes loaded by the bootstrap clas loader), but why do you want to know?
 
Avinash Haridasu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:I guess you could write your own ClassLoader which kept track of which jar files it had loaded classes from (although I'm not sure how you would handle classes loaded by the bootstrap clas loader), but why do you want to know?



I am interested to know the order of loading of jars and when they are loaded..

I mean does all the jars in the lib, classpath, ext get loaded at the same time the jvm is up or do they get loaded on demand like when a class in the jar is invoked
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That depends on the ClassLoader you are using. For instance the URLClassLoader api docs say:

* Finds and loads the class with the specified name from the URL search
* path. Any URLs referring to JAR files are loaded and opened as needed
* until the class is found.


 
What's that smell? Hey, sniff this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic