André
André

Reputation: 183

How Java know where is the dependency jar?

I've made a simple maven project, here is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>HelloWorld</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>HelloWorld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>1.5.9.RELEASE</version>
    </dependency>
  </dependencies>
</project>

It is a single file project, so this is the code:

package com.example;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main (String[] args) {
        System.out.println("Hello World!");
    }
}

I run mvn package and then java -cp target/HelloWorld-1.0-SNAPSHOT.jar com.example.App and everything works, i see "Hello World!" in my console.

So here's my question: how does Java know where is org.springframework.boot.autoconfigure.SpringBootApplication if i didn't specified the jar containing this class in classpath?

Upvotes: 0

Views: 45

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

It doesn't. It's an annotation, and Java can run classes containing annotations that are not available in the classpath (by design).

This is what allows sharing classes between client and server, for example, that are annotated because the server needs the annotations, but which are useless at client-side.

Upvotes: 2

Related Questions