[英]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: 我尝试过的事情:
Do not compile the application when you want to call it. 要调用该应用程序时,请勿对其进行编译。 This is bad: 这不好:
javac
on a single java file. 如您javac
,在单个Java文件上调用javac
不是一件简单的事情。 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: 您可能会考虑的其他事项:
App.main()
directly. 与其在单独的进程中执行其他应用程序, App.main()
直接调用App.main()
在同一个JVM中运行它。 You might want it to run in a separate thread, which is easy to do. 您可能希望它在单独的线程中运行,这很容易做到。 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.