在没有“声明的包com”的情况下处理main和test。在Eclipse中,pkgY与预期的包dirX.src.main.java.com.pkgY不匹配。

时间:2023-01-18 11:19:46

Every Java project I download has main and test folders. Both contain java and resources sub-folders. Some projects even add more levels: for instance dir1, dir2, dir3 and etc in the root, where each dirX contains src with main and test subfolders. How do I handle them? If I just drop the content of the project root into the default src, Eclipse will attribute classes into dirX.src.main.java.com.pkgY obviously resulting in "The declared package com.pkgY does not match the expected package dirX.src.main.java.com.pkgY" error. Am I supposed to add every dirX/src/java directory as a source separately in buildpath, as this answer suggests? What about the corresponding tests? Thanks.

我下载的每个Java项目都有主和测试文件夹。它们都包含java和资源子文件夹。一些项目甚至添加了更多的级别:例如dir1、dir2、dir3等,在根目录中,每个dirX都包含有主和测试子文件夹的src。我怎么处理它们?如果我将项目根的内容删除到默认的src中,Eclipse将把类归为dirX.src.main.java.com.pkgY,显然会导致“声明的包com”。pkgY与预期的包dirX.src.main.java.com.pkgY“错误”不匹配。我是否应该将每一个dirX/src/java目录作为源单独添加到buildpath中,就像这个答案所暗示的那样?那么相应的测试呢?谢谢。

2 个解决方案

#1


0  

This structure is Maven's Standard Directory Layout. In your case, you need to add both src/main/java and src/test/java (right click on java folder and go for use this as source folder) and resources as well to classpath through configure buildpath option. Refer this maven link for more details on this.

这个结构是Maven的标准目录布局。在您的案例中,您需要添加src/main/java和src/test/java(右键单击java文件夹,然后使用这个作为源文件夹)和资源,通过配置buildpath选项来访问类路径。有关这个的更多细节,请参考此maven链接。

#2


0  

Make sure you installed m2e (http://download.eclipse.org/technology/m2e/releases), and if you're doing JavaEE stuff with Eclipse WTP, you should also probably add m2eclipse-wtp (https://github.com/sonatype/m2eclipse-wtp/wiki)

确保安装了m2e (http://download.eclipse.org/technology/m2e/release),如果您使用Eclipse WTP来处理JavaEE,您还应该添加m2eclipse-wtp (https://github.com/sonatype/m2eclipse-wtp/wiki)。

Then, you have 2 solutions :

然后,有两个解:

  • either right click on each project and Configure > Convert to Maven project
  • 右键单击每个项目,并配置>转换为Maven项目。
  • remove the projects from your workspace and do an "Import as existing maven project"
  • 从您的工作区中删除项目,并执行“导入为现有maven项目”

m2e will take care of configuring Eclipse based on your pom.xml configuration.

m2e将负责配置基于pom的Eclipse。xml配置。

#1


0  

This structure is Maven's Standard Directory Layout. In your case, you need to add both src/main/java and src/test/java (right click on java folder and go for use this as source folder) and resources as well to classpath through configure buildpath option. Refer this maven link for more details on this.

这个结构是Maven的标准目录布局。在您的案例中,您需要添加src/main/java和src/test/java(右键单击java文件夹,然后使用这个作为源文件夹)和资源,通过配置buildpath选项来访问类路径。有关这个的更多细节,请参考此maven链接。

#2


0  

Make sure you installed m2e (http://download.eclipse.org/technology/m2e/releases), and if you're doing JavaEE stuff with Eclipse WTP, you should also probably add m2eclipse-wtp (https://github.com/sonatype/m2eclipse-wtp/wiki)

确保安装了m2e (http://download.eclipse.org/technology/m2e/release),如果您使用Eclipse WTP来处理JavaEE,您还应该添加m2eclipse-wtp (https://github.com/sonatype/m2eclipse-wtp/wiki)。

Then, you have 2 solutions :

然后,有两个解:

  • either right click on each project and Configure > Convert to Maven project
  • 右键单击每个项目,并配置>转换为Maven项目。
  • remove the projects from your workspace and do an "Import as existing maven project"
  • 从您的工作区中删除项目,并执行“导入为现有maven项目”

m2e will take care of configuring Eclipse based on your pom.xml configuration.

m2e将负责配置基于pom的Eclipse。xml配置。