Home > OS >  can root directory of a classpath in java or spring have a package name?
can root directory of a classpath in java or spring have a package name?

Time:10-09

this seems a simple question, but im not able to get it to work I have a simple spring boot java project. java.srcDirs is set to src. I'm assuming that classpath also has src now.

Can I put code files directly into src directory ? or must I create a subdirectory?

what is happening is that if i create a subdirectory demo, then i can set package demo; in code files sitting in demo. but i cannot set any package name to files in src directory. If i leave it blank or "", i get errors as

declared package "src" does not match the expected package ""

if I leave out package name entirely, then spring boot errors out saying

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.


2022-10-01 13:17:51.010  WARN 7999 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/home/user/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.4/1848e5e8e474cc880a7708f03f750736665d4157/spring-boot-autoconfigure-2.7.4.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
2022-10-01 13:17:51.021  INFO 7999 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-10-01 13:17:51.036 ERROR 7999 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/home/user/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.4/1848e5e8e474cc880a7708f03f750736665d4157/spring-boot-autoconfigure-2.7.4.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)

is it part of java standard that packages should be in subdirectories of srcdir/classpath ? quite confused here.

CodePudding user response:

You can put the following class in the source root directory.

package demo;

public class MyClass {
}

When you compile the code, the class MyClass will be put into the output directory in the directory demo.

As this works it's not recommended. Package structure and directory structure should match.

  • Related