import ...
public class TriggerJob {
String jobStatus = "";
SchedulerMetaData metaData = null;
public void rightNow(HashMap ParamMap) {
AnotherProjectClass anp = new AnotherProjectClass();
anp.display();
}
}

- 9,586
- 6
- 49
- 79

- 2,647
- 4
- 24
- 38
-
2i have two different project, i want to call one projects java class from another. i have taken help of... http://stackoverflow.com/questions/11792812/calling-a-class-in-another-project-eclipse this link – Mitul Maheshwari Jan 21 '14 at 10:20
-
1Yes you can. I can't easily craft an easy answer right now, but sure someone could. – Fabinout Jan 21 '14 at 10:21
-
@MitulMaheshwari Per your link, I tried to import class_from_project_B into class_from_project_A but getting error The import can not be resolved. – vikramvi May 22 '17 at 14:33
-
Found the problem; I didn't specify any package for class in the project which I had included under Projects. Found this issue through https://stackoverflow.com/questions/2335211/what-is-the-default-package-in-which-my-classes-are-put-if-i-dont-specify-it – vikramvi May 22 '17 at 15:39
9 Answers
You can do either this way:
In the dependency you can add the other projects to your project. Right click on project -> properties -> java build path -> projects. Add your project here.
OR
Make the classes of project into jar and add it to other project
Dependencies should be added in classpath
In run time, make sure the JAR files of the referenced projects is added in class path on both the cases.

- 1,736
- 1
- 13
- 22

- 1,123
- 3
- 22
- 38
-
5It is ok in Compilation time. But it fails for me in Runtime(NoClassDefFoundError). How to avoid it? – Sudheer Kumar Nov 24 '14 at 13:45
-
-
This doesn't work. "Cannot be resolved to a type". Even when we reference the project that has this public class. Please add complete instructions. – Philip Rego May 08 '19 at 20:46
Knowing that you using any version of Eclipse, the below steps should help you:
Step #1. Right Click => Project
Step #2. Click Project Properties
Step #3. Click on Java Build Path
Step #4. Click the Projects Tab
Step #5. Click the Add Button
Step #6. Select the Project you want to add
Step #7. Click OK button
Hopefully this help.

- 845
- 2
- 16
- 41
I have done like this in my project:
ClientResponse response=WebServiceClient.invokeGRODService("document","get",documentId);
- invokeGRODService() is a method in WebServiceClient class where URL is mentioned.
- "document" is method level path,"get" is class level path and documentId is parameter to be passed as an input to other class in other project.
- invokeGRODService() is as follows:
public static ClientResponse invokeGRODService(String classLevelPath, String methodLevelPath,Object request){
> LOGGER.info("invokeGRODService()...Start");
> ClientConfig config = new DefaultClientConfig();
> Client client = Client.create(config);
> WebResource service=null;
> try{
> service = client.resource(UriBuilder.fromUri(AppProperties.getProperty(AppConstants.GROD_REST_SERVICE_URL)).build());
> }catch(PropertyNotFoundException pe){
> LOGGER.error("Error getting the--- "+pe);
> }
> try {
> ClientResponse response = service.path(classLevelPath).path(methodLevelPath).type(MediaType.APPLICATION_XML).post(ClientResponse.class,
> request);
> if (response.getClientResponseStatus() != ClientResponse.Status.OK) {
> String errorResponse = response.getEntity(String.class);
> LOGGER.error("RECEIVED ERROR FROM WEBSERVICE.."+errorResponse);
> }
> LOGGER.info("invokeGRODService()...End");
> return response;
> } catch (Exception e) {
> LOGGER.error("Error while calling GRoD web service: ",e);
> }
> return null;
> }
- Mention your URL in "AppConstants.GROD_REST_SERVICE_URL". I have taken it from constant through AppProperties.
ClientResponse response = service.path(classLevelPath).path(methodLevelPath).type(MediaType.APPLICATION_XML).post(ClientResponse.class, request);
- If URL is correct you should get data in response object with status 200(OK).

- 939
- 1
- 7
- 25
-
i wanted exaclty this way in my project too, but its can't find the class. – Mitul Maheshwari Jan 22 '14 at 05:25
-
-
yes, both project are working but still get's an error "class not found error" – Mitul Maheshwari Jan 24 '14 at 11:00
-
Then check your URL whether it is correct. For that test the function that you wanted to call from another project through advance REST client. I think your URL is wrong, that is why you are getting "class not found" error. – pan1490 Jan 27 '14 at 05:33
-
No, @pan1490 i have tried check URL by manually running that, its working fine. – Mitul Maheshwari Jan 27 '14 at 06:05
-
-
Yes, @pan1490, both are under same server,and both projects running also, Even i have tried with taking jar of another project to but can't solved yet. – Mitul Maheshwari Jan 27 '14 at 12:00
I had a similar problem and finally I realized that the problem was that the class in the calling project was not under src
folder, but inside another inner package. When I removed that folder and moved the file to the src
folder, everything worked.

- 1,655
- 3
- 19
- 33
You have to open your project properties, then clcik on "Java Build Path" and select the tab "Projects". Add the project from which you want to import your classes and do a rebuild.

- 13,939
- 5
- 50
- 79
Make the classes of the project A into jar
and add it to the class path of the other project B

- 11,944
- 3
- 37
- 49
-
i have added java build path of project B in project A. okay...i wil try with adding jar of project B – Mitul Maheshwari Jan 21 '14 at 10:38
-
-
it is not working, still gives class not found error, i have added jar of project B. – Mitul Maheshwari Jan 21 '14 at 11:12
This works fine as long as you have imported the project containing the classes.
Assuming your using Eclipse the following steps will work:
- Right Click > Project
- Click Project Properties
- Click Java Build Path
- Click the Projects Tab
- Click the Add Button
- Select the Project
- Click OK

- 93,289
- 19
- 159
- 189
-
yes, i have done the same way bro, nut it gives class not found exception – Mitul Maheshwari Jan 21 '14 at 10:39
-
You can do it in 2 ways.
Export the other project to jar
and import the jar in your project.
OR
In the dependency you can add the other projects to your project. Right click on project --> properties --> java build path --> projects
. Add your project here

- 10,413
- 7
- 36
- 52
-
i have tried that way. but still it can't working, can you give me any other way which i can solve my problem ..? – Mitul Maheshwari Jan 24 '14 at 10:58
-
You have already accepted an answer. Never mind, it is not possible that it is not working unless the class in other project not public – G.S Jan 24 '14 at 11:05
Yes. In the Project Explorer right click on it and select Properties, there go to Java Build Path and select Projects tab. Add your other project here, now you're able to use the classes from it in your current project. But note BOTH have to be open when you run them or debug in eclipse (otherwise you will see red lines telling you a class was not found).

- 440
- 1
- 8
- 28
-
i have done same way, but it gives me class not found exception. – Mitul Maheshwari Jan 21 '14 at 10:36
-