我们可以在eclipse中从我们的项目中调用另一个项目java类吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21255265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we call another project java class from our project in eclipse
提问by Mitul Maheshwari
import ...
public class TriggerJob {
String jobStatus = "";
SchedulerMetaData metaData = null;
public void rightNow(HashMap ParamMap){
AnotherProjectClass anp = new AnotherProjectClass();
anp.display();
}
}
采纳答案by Mayur
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.
在依赖项中,您可以将其他项目添加到您的项目中。右键单击项目-> 属性-> java 构建路径-> 项目。在此处添加您的项目。
OR
或者
Make the classes of project into jar and add it to other project
将项目的类做成jar并添加到其他项目中
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.
在运行时,请确保在这两种情况下都将引用项目的 JAR 文件添加到类路径中。
回答by G.S
You can do it in 2 ways.
Export the other project to jar
and import the jar in your project.
您可以通过 2 种方式做到这一点。
将另一个项目导出到jar
您的项目中并将该 jar 导入到您的项目中。
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
在依赖项中,您可以将其他项目添加到您的项目中。Right click on project --> properties --> java build path --> projects
. 在此处添加您的项目
回答by Axel
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.
您必须打开项目属性,然后单击“Java 构建路径”并选择“项目”选项卡。添加要从中导入类的项目并进行重建。
回答by Nambi
Make the classes of the project A into jar
and add it to the class path of the other project B
制作 的类project A into jar
并将其添加到另一个的类路径中project B
回答by Kevin Bowersox
This works fine as long as you have imported the project containing the classes.
只要您导入了包含类的项目,这就可以正常工作。
Assuming your using Eclipse the following steps will work:
假设您使用 Eclipse,以下步骤将起作用:
- Right Click > Project
- Click Project Properties
- Click Java Build Path
- Click the Projects Tab
- Click the Add Button
- Select the Project
- Click OK
- 右键单击 > 项目
- 单击项目属性
- 单击 Java 构建路径
- 单击项目选项卡
- 单击添加按钮
- 选择项目
- 单击确定
回答by kacpr
Yes. In the Project Explorerright click on it and select Properties, there go to Java Build Pathand select Projectstab. 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).
是的。在Project Explorer 中右键单击它并选择Properties,然后转到 Java Build Path并选择Projects选项卡。在此处添加您的其他项目,现在您可以在当前项目中使用它的类。但请注意,当您运行它们或在 Eclipse 中调试时,两者都必须打开(否则您会看到红线告诉您未找到类)。
回答by pan1490
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:
- invokeGRODService() 是 WebServiceClient 类中的一个方法,其中提到了 URL。
- “document”是方法级别的路径,“get”是类级别的路径,documentId 是作为输入传递给其他项目中的其他类的参数。
- invokeGRODService() 如下:
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.
- 在“AppConstants.GROD_REST_SERVICE_URL”中提及您的 URL。我已经通过 AppProperties 从常量中获取了它。
ClientResponse response = service.path(classLevelPath).path(methodLevelPath).type(MediaType.APPLICATION_XML).post(ClientResponse.class, request);
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).
- 如果 URL 正确,您应该在响应对象中获取状态为 200(OK) 的数据。
回答by TiredOfProgramming
Knowing that you using any version of Eclipse, the below steps should help you:
知道您使用的是任何版本的 Eclipse,以下步骤应该对您有所帮助:
Step #1.Right Click => Project
第1步。右键单击 => 项目
Step #2.Click Project Properties
第2步。单击项目属性
Step #3.Click on Java Build Path
第 3 步。单击 Java 构建路径
Step #4.Click the Projects Tab
第四步。单击项目选项卡
Step #5.Click the Add Button
第 5 步。单击添加按钮
Step #6.Select the Project you want to add
第 6 步。选择要添加的项目
Step #7.Click OK button
第 7 步。单击确定按钮
Hopefully this help.
希望这有帮助。
回答by user1419243
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.
我遇到了类似的问题,最后我意识到问题是调用项目中的类不在src
文件夹下,而是在另一个内部包中。当我删除该文件夹并将文件移动到该src
文件夹时,一切正常。