简体   繁体   中英

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.

I have project CacheProject which contains CachePackage as a package which has CacheMain.java class. This CacheMain class contains a public method getName() which should return me a value of one of the varible there as string.

I have also created jar for the respective CacheProject and added to classpath of another project CacheConsumer1. also I have added project CacheProject to Right click on CacheConsumer1 -> properties -> java build path -> projects.

在此处输入图片说明

Still I am neither able to access CacheMain nor getName() method of it:

在此处输入图片说明

also I am not able to access anything from the 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.

Actually everything would be easier if you would learn to use Maven or Gradle in order to build your projects and define dependencies. 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:

Java构建路径——添加项目依赖 This will also lead to CacheConsumer1 automatically being rebuilt as soon as there are changes in CacheProject .


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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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