• 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
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Compiletime and Runtime Dependencies

 
Ranch Hand
Posts: 440
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have certain queries regarding compile time and runtime dependencies. I understand that compile time dependencies are required to compile the code and runtime while executing the code.

1. Would we need all compile time dependencies at run time?

2. In what cases, Runtime dependencies are required? How it would be possible to have additional dependencies at runtime which were not required at compile time?

3.  If the code is compiled successfully, then, why do we need those dependencies at run time?
 
Marshal
Posts: 72905
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Loos at the sort of code that can throw exceptions. If you get an IOException, that tells you that the IO isn't working, or your file can't be read, or something. You don't know anything about such things at compile time.
 
Vaibhav Gargs
Ranch Hand
Posts: 440
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell. Just to add, by dependencies i mean the jar files.
 
Marshal
Posts: 26591
81
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
Sure. Your code may call methods from classes in some external library, say for example Apache POI. So the compiler needs to see the Apache POI jar to make sure you used the correct class names and method signatures and so on. And in turn the Apache POI jar can (and does) have dependencies on other jars. You're not going to be referencing classes from those other jars directly, so the compiler doesn't need to know about them.

But when you run your code, and it calls methods from the Apache POI jar, those POI methods will in turn call methods from classes in those other dependent jars. So yes, those jars need to be in the classpath at run time or your application is broken.

This isn't just an obscure corner of the Java world, either. It's extremely common, so common that applications have been produced to ensure that when your application has dependencies, you also have copies of the dependencies of those dependencies, and onwards recursively to infinity. Maven is the name you'll commonly see referenced when you want to collect up all of the recursive dependencies. In fact if you search the web for "Apache POI dependencies" you'll find pages which tell you to just use Maven to deal with it.
 
Saloon Keeper
Posts: 23689
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To amplify on what Paul said, Maven allows you to define what primary jar requirements your project is dependent on. When you define a JAR dependency in a Maven POM file, it has a scope.

The most commonly-used scope is "compile", which causes the dependency to be made available to both compiling and to be included into the Maven-produced product for runtime use.

However, one you'll see especially in web applications is "provided". Which says I need to know about this JAR to compile, but it's provided at runtime by the webapp server, so I don't need it to be included in the Maven product (WAR file). And, in fact, doing so might may the WAR not execute properly.
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic