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

Runtime classpath and compile time classpath

 
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled a program with a JAR in the classpath. Also ran it with the JAR passed with classpath. For instance I used MySQL .jar.

So I feel it was required in both classpath. I have heard the Servlet API jar is also an example of what is needed at compile time.

With that out of the way, have I the right idea of what is to be needed when? If my above assertion is wrong, could you answer my question and point me in the right direction?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you have a compile-time dependency on a jar (you have to add it to the classpath when compiling), you will also need to have that jar in the classpath when you run the program. Java never statically links code into your application.

Sometimes, a jar is not needed at compile time, but only at runtime. An example are JDBC drivers: you should not have the JDBC driver jar in the classpath when compiling, but only when running the program.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper. My takeaway is that JDBC driver is not critical at compile time because javac did not complain when it was put in classpath.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the case of JDBC drivers, you should not put it in the classpath when compiling; it should only be on the classpath while running your program.

The whole idea of drivers with JDBC is that your program does not need to have a compile-time dependency on any specific JDBC driver. You program against the JDBC API, which is independent of the specific brand and version of database that you're going to use. This enables you to change the JDBC driver later, without recompiling your code. You can even switch to a completely different database (for example, an Oracle database instead of a Microsoft SQL Server database) without recompiling your program.

So, do not put the JDBC driver on the classpath when you compile your program; only when you run your program.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Appreciate your clearing that up.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic