Reputation: 322
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:
Upvotes: 1
Views: 984
Reputation: 6497
Do not compile the application when you want to call it. This is bad:
javac
on a single java file.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:
App.main()
directly. You might want it to run in a separate thread, which is easy to do.ProcessBuilder
rather than Runtime.exec
. In many cases, it is an easier API to use and get right.Upvotes: 2