简体   繁体   English

如何从控制器[dispatchServlet]或任何Java类调用另一个MavenProject的class mainMethod?

[英]how to call Class mainMethod of another MavenProject from a controller[dispatchServlet] or any java class?

anyBody please share the knowledge. anyBody请分享知识。

i am facing this Exception 我正面临这个例外

java.lang.ClassNotFoundException java.lang.ClassNotFoundException

from this project A 来自这个项目A

package com.demo.feed;
@WebServlet("/run")
public class ProjA {
  String[] args={};
  new com.om.demo.ProjB().main(args);
}

i have to call main method of maven Project B 我必须调用Maven项目B的主要方法

package com.om.demo;
public class ProjB {
    public void main( String[] args )
    {
        hello();
    }
    static void hello() {
            System.out.println("some text");
        }

} }

I tried these things adding project B to project A and after that on run configuration i have added classPath Variables.. 我尝试过将B项目添加到A项目中,然后在运行配置中添加了classPath变量。

Needed output in console : some text 控制台中需要的输出:一些文本

In the pom of Project A you will have to add Project B as a dependency and then try running. 在项目A的pom中,您必须将项目B添加为依赖项,然后尝试运行。 Before running you should make sure to run mvn clean install on Project B 在运行之前,应确保在Project B上运行mvn clean install

In pom.xml in dependencies section of project which contains ProjA class, you have to put: 在包含ProjA类的项目的dependencies部分中的pom.xml中,您必须输入:

    <dependency>
        <groupId>com.om.demo</groupId>
        <artifactId>projb</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

where projb is project which contains class ProjB. 其中projb是包含类ProjB的项目。 And then do mvn install on project which contains ProjB class. 然后在包含ProjB类的项目上执行mvn install

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

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