简体   繁体   English

eclipse中无法将src/main/java下的java类导入到src/test/java中

[英]Cannot import java classes under src/main/java into src/test/java in eclipse

I am using maven project in Eclipse, where I am not able to access classes under src/main/java in src/test/java class.我在 Eclipse 中使用 maven 项目,我无法访问 src/test/java 类中 src/main/java 下的类。 I could use the classes in maven dependacies jars though.不过,我可以在 Maven 依赖项 jar 中使用这些类。 What am I missing ?我错过了什么?

I have found that sometimes this error appears and by simply going Project -> Clean... the error will stop.我发现有时会出现此错误,只需转到Project -> Clean...错误就会停止。

However if this does not work there is a blog with some more suggestions here然而,如果这不起作用有一些更多的建议,一个博客在这里

Updated 11/07/2017 2017 年 7 月 11 日更新

The original link is no longer available, refer to web archive here原始链接不再可用,请参阅此处的网络存档

Or just have them all here..或者只是把它们都放在这里..

  • 'Clean' Your Eclipse Project: Go to Project > Clean in Eclipse [This seems to work for me] “清理”你的 Eclipse 项目:在 Eclipse 中转到“项目”>“清理”[这似乎对我有用]
  • Refresh your project folder (right click on your project > refresh)刷新您的项目文件夹(右键单击您的项目> 刷新)
  • Re-build your project重新构建你的项目
  • Clean your builds (If using Ant or Maven – clean your builds)清理你的构建(如果使用 Ant 或 Maven – 清理你的构建)
  • Recreate your project in Eclipse在 Eclipse 中重新创建您的项目
  • 'Switch' Workspace – then Switch back (Eg Change to Debug, then switch back to Java) “切换”工作区 – 然后切换回(例如更改为调试,然后切换回 Java)
  • Remove and re-add your JRE:删除并重新添加您的 JRE:
    1. Right Click on your project > properties右键单击您的项目 > 属性
    2. Click on the Libraries tab单击库选项卡
    3. Click on the JRE单击 JRE
    4. Click remove, then OK点击删除,然后确定
    5. Repeat 1-3 again, but add the JRE again再次重复 1-3,但再次添加 JRE

You just need to import them.您只需要import它们。 Eclipse should help you with this: if you try to use the class names, it'll suggest where to import from. Eclipse 应该可以帮助您解决这个问题:如果您尝试使用类名,它会建议从何处导入。

The test classes are a separate package, so the classes will need to be declared public .测试类是一个单独的包,因此需要将这些类声明为public

Absolutely, one can do it..绝对可以的..

First You need to navigate to Build Path-->Configure Build Path--> Source tab首先,您需要导航到 Build Path-->Configure Build Path--> Source 选项卡

then in the Source tab search/check [your Project Name]/src/main/java and change然后在 Source 选项卡中搜索/检查 [your Project Name]/src/main/java 并更改

"contains test sources" from No to Yes and save it. “包含测试源”从“否”到“是”并保存。

This will resolved issue of import packages from "src/test/java" to "src/main/java" successfully这将成功解决从“src/test/java”导入包到“src/main/java”的问题

Project -> Clean项目 -> 清洁

is enough for me to work足以让我工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将 src/main/java 类导入 maven-spring-web 项目中的 src/test/java jUnit 测试。 IDE - 日食 - Cant import src/main/java classes into src/test/java jUnit tests in maven-spring-web project. IDE - Eclipse JAVA - 无法将 src/test/java 导入 src/main/java - JAVA - can't import src/test/java to src/main/java 无法在 eclipse 中将 class src/test/java 导入 src/main/java - can't import class src/test/java into src/main/java in eclipse want to execute a java test class which is in src/test/java from src/main/java but I am unable to import that java class in src/main/java? - want to execute a java test class which is in src/test/java from src/main/java but I am unable to import that java class in src/main/java? Maven项目中src / main / java和src / test / java之间的区别 - Difference between src/main/java and src/test/java in a Maven project IntelliJ IDEA:库在src \\ test \\ java上可用,但在src \\ main \\ java上不可用 - IntelliJ IDEA: library available on src\test\java but not on src\main\java querydsl在src / main / java中生成Q类 - querydsl generate Q classes in src/main/java Eclipse 中 Maven 项目的类路径 - src vs src/main/java - Classpath for Maven project in Eclipse - src vs src/main/java Eclipse将src文件夹永久更改为src / main / java - Eclipse change src folder to src/main/java permanently 如何从src / main / java中的src / test / resources中读取道具 - How to read props from src/test/resources in src/main/java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM