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
  •  | 
  •  

Question

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

Was it helpful?

Solution

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 ;.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top