• Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime classpath and compile time classpath  RSS feed

 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciate your clearing that up.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!