简体   繁体   English

如何在Eclipse的Maven项目中编译和运行Java应用程序?

[英]How to compile and run Java application within Maven project in Eclipse?

I have an existing web application, A, which is a Maven project in Eclipse. 我有一个现有的Web应用程序A,它是Eclipse中的Maven项目。 It is run on a Tomcat 8 localhost server. 它在Tomcat 8本地服务器上运行。 Now I have a Java application, B, that was a separate project on its own. 现在,我有一个Java应用程序B,它是一个单独的项目。 I have imported B into my workspace with A as another project. 我已经将B导入到我的工作区中,而A作为另一个项目。 I am trying to run a main class (called App.java ) which exists in B from a class within A. The class within A is below: 我试图运行一个主类(称为App.java ),该主类从A中的一个类存在于B中。A中的类如下:

public void runOrgReportUtility() throws IOException {
    int k = runProcess("javac /Users/ag/utilities/org-report-utility/src/main/java/com/vs/orgreport/App.java", null);
    if (k==0) {
        log.info("Compiled. Now trying to run class.");
        String commandStr = "java -cp /Users/ag/utilities/org-report-utility/src/main/java/com/vs/orgreport/ App";
        log.info("Command String: ");
        log.info(commandStr);
        runProcess(commandStr);
}

public int runProcess(String command, String[]) throws Exception {
    Process pro = Runtime.getRuntime().exec(command);
    pro.waitFor();
    return pro.exitValue();
  }

When I tried running A, the console gave me a bunch of error: cannot find symbol , for various classes within B. This made me realize that only the App.java was being compiled and run, and not the other dependent classes in B. 当我尝试运行A时,控制台给了我一个error: cannot find symbol为B中的各种类error: cannot find symbol 。这使我意识到,只有App.java被编译和运行,而B中的其他相关类却没有。

How can my second java application be compiled and run when I start up my original web application? 启动原始Web应用程序时,如何编译和运行第二个Java应用程序?

Things I have tried: 我尝试过的事情:

  • Set (original) project references to include secondary Java app 设置(原始)项目引用以包含辅助Java应用
  • Add Java app project to Java build path (in Projects tab) of original project 将Java应用程序项目添加到原始项目的Java构建路径(在“ 项目”选项卡中)
  • Added Java app to Deployment Assembly of original project 将Java应用程序添加到原始项目的部署程序集

Do not compile the application when you want to call it. 要调用该应用程序时,请勿对其进行编译。 This is bad: 这不好:

  1. Compiling with javac is hard due to all the dependencies. 由于所有依赖性,使用javac进行编译非常困难。 As you found out, it is not a simple matter of invoking javac on a single java file. 如您javac ,在单个Java文件上调用javac不是一件简单的事情。
  2. Why wait until you try to run it to find out that you have an error that keeps it from compiling? 为什么要等到尝试运行它后才发现有错误使其无法编译?
  3. Why incur the overhead of compiling it every time you run it? 为什么每次运行时都会产生编译的开销?

You normally would use maven to compile B and probably use the maven-shade-plugin to create a B.jar that contains B and all dependencies (aka a "fat" jar). 通常,您将使用maven来编译B,并可能使用maven-shade-plugin创建一个包含B和所有依赖项(也称为“胖” jar)的B.jar。 Then when A invokes B, you can use the fat jar as the classpath when you invoke the app that it contains. 然后,当A调用B时,可以在调用它包含的应用程序时将胖jar用作类路径。

Other things you might consider: 您可能会考虑的其他事项:

  1. You could probably also include the fat jar in your web application so that you are not dependent on having an external jar that must be in place in order for your web application to be fully functional. 您可能还可以在Web应用程序中包含胖子jar,这样就不必依赖于必须安装外部jar才能使Web应用程序完全发挥作用。
  2. Rather than execute the other application in a separate process, run it in the same JVM by just calling App.main() directly. 与其在单独的进程中执行其他应用程序, App.main()直接调用App.main()在同一个JVM中运行它。 You might want it to run in a separate thread, which is easy to do. 您可能希望它在单独的线程中运行,这很容易做到。
  3. If you really want a separate OS process, use ProcessBuilder rather than Runtime.exec . 如果您真的想要一个单独的OS进程,请使用ProcessBuilder而不是Runtime.exec In many cases, it is an easier API to use and get right. 在许多情况下,它是易于使用和正确使用的API。

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

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