How to manage class you want use via Runtime.getRuntime().exec in a web app project with Maven?

StackOverflow https://stackoverflow.com/questions/23215419

  •  07-07-2023
  •  | 
  •  

문제

I have a web app developped with Maven. I have some class that execute function via

Runtime.getRuntime().exec.

I was wondering, where to put them in the project to compile them and then to call them via something like

String[] command={"java MainExternal.class ","test"};

What should be the path ? I'm quite lost... UPDATED :

enter image description here

도움이 되었습니까?

해결책

So many mistakes, it is hard to know where to start :-)

  1. You can put the Maven project / source tree anywhere you like.

  2. You can put the JAR file created by Maven anywhere you like.

  3. You need to put the JAR file on the classpath, and the best way to do that is to use the -cp option.

  4. You need to specify the entry point by giving the fully qualified name of the class ... not a filename. The ".class" suffix is a mistake.

Assuming that the java command is on your PATH, the command array should look something like this:

    String[] command = {"java",
                        "-cp", "/some/path/to/MyProject.jar",
                        "com.acme.MainExternal",
                        "test"};

UPDATE

Given that the class you are trying to run and its dependencies are in the WAR file, you are better off leaving it as-is, and using a classpath like:

"/.../ClassOmicsTracer/WEB-INF/classes:/.../ClassOmicsTracer/WEB-INF/lib/*"

where "/.../ClassOmicsTracer" is the absolute file path to your webapp. If you are on Window, change the : to a ;.

다른 팁

You should consider the relative path to your class file from *.war root. It should contain your class files package, something like:

/com/package/your/MainExternal.class
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top