简体   繁体   English

如何将一个 eclipse 项目的方法/变量调用到另一个 eclipse 项目?

[英]How to call method/variable of one eclipse project to another eclipse project?

I have two Projects in eclipse and I simply tried to call method of one project to another but not able to do it even if I set up the whole flow of project setup using properties and even classpath.我在 eclipse 中有两个项目,我只是尝试将一个项目的方法调用到另一个项目,但即使我使用属性甚至类路径设置了整个项目设置流程,也无法做到这一点。

I have project CacheProject which contains CachePackage as a package which has CacheMain.java class.我有项目 CacheProject,其中包含 CachePackage 作为具有 CacheMain.java 类的包。 This CacheMain class contains a public method getName() which should return me a value of one of the varible there as string.这个 CacheMain 类包含一个公共方法 getName() ,它应该将其中一个变量的值作为字符串返回给我。

I have also created jar for the respective CacheProject and added to classpath of another project CacheConsumer1.我还为各自的 CacheProject 创建了 jar 并添加到另一个项目 CacheConsumer1 的类路径中。 also I have added project CacheProject to Right click on CacheConsumer1 -> properties -> java build path -> projects.我还添加了项目 CacheProject 右键单击​​ CacheConsumer1 -> 属性 -> java 构建路径 -> 项目。

在此处输入图片说明

Still I am neither able to access CacheMain nor getName() method of it:我仍然无法访问它的 CacheMain 和 getName() 方法:

在此处输入图片说明

also I am not able to access anything from the CachePackage:我也无法从 CachePackage 访问任何内容:

在此处输入图片说明

Please help with it guys if anybody has already worked with something like this.如果有人已经使用过这样的东西,请帮助它。 Because none of the earlier answer posted in this stackOverFlow post is working for my scenario.因为在此 stackOverFlow 帖子中发布的早期答案都不适用于我的场景。

Actually everything would be easier if you would learn to use Maven or Gradle in order to build your projects and define dependencies.实际上,如果您学会使用 Maven 或 Gradle 来构建项目并定义依赖项,一切都会变得更容易。 But in Eclipse you can just open both projects, then go to the project properties of project CacheConsumer1 and set project CacheProject as a build path dependency:但是在 Eclipse 中,您可以只打开两个项目,然后转到项目CacheConsumer1的项目属性并将项目CacheProject设置为构建路径依赖项:

Java构建路径——添加项目依赖 This will also lead to CacheConsumer1 automatically being rebuilt as soon as there are changes in CacheProject .这也将导致CacheConsumer1CacheProject发生更改CacheConsumer1自动重建。


Update: I just noticed in your screenshot that CacheProject has no src folder or any other folder marked as source folder (the folder icon with the little parcel in it).更新:我刚刚在您的屏幕截图中注意到CacheProject没有src文件夹或任何其他标记为源文件夹的文件夹(带有小包裹的文件夹图标)。 Maybe you just start over, create a new Java project with default Eclipse folder structure and move the class or classes over to that project, creating your Java packages and classes inside the src folder.也许您只是重新开始,使用默认的 Eclipse 文件夹结构创建一个新的 Java 项目,然后将一个或多个类移动到该项目中,在src文件夹中创建您的 Java 包和类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM