简体   繁体   中英

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. It is run on a Tomcat 8 localhost server. Now I have a Java application, B, that was a separate project on its own. I have imported B into my workspace with A as another project. 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:

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.

How can my second java application be compiled and run when I start up my original web application?

Things I have tried:

  • Set (original) project references to include secondary Java app
  • Add Java app project to Java build path (in Projects tab) of original project
  • Added Java app to Deployment Assembly of original project

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. As you found out, it is not a simple matter of invoking javac on a single java file.
  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). Then when A invokes B, you can use the fat jar as the classpath when you invoke the app that it contains.

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.
  2. Rather than execute the other application in a separate process, run it in the same JVM by just calling App.main() directly. 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 . In many cases, it is an easier API to use and get right.

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