0

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

ZheFrench
  • 1,164
  • 3
  • 22
  • 46

2 Answers2

0

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • If i understand, i need to generate also a .jar with the .war to call just my class to be executed ? That's mean i have some conf to do with maven to get the jar. I will take a look. Tell me if i'm wrong.Thanks – ZheFrench Apr 22 '14 at 10:51
  • I manage to create the jar. It works when i make the call from console. I will make so test a little more. Thanks. Good answer. – ZheFrench Apr 22 '14 at 11:12
  • @ZheFrench - (Sorry ... doing other things). You don't *have* to put the class into a separate JAR file, but it will work if you do. Alternatively, if the class is (was) in the WAR file, you could come up with a suitable classpath that refers to the relevant directory or JAR file in the expanded WAR. – Stephen C Apr 22 '14 at 13:58
  • @ZheFrench - To be frank, it was not clear from your question that you were trying to run a class that was in the WAR. It was just "some class" ... – Stephen C Apr 22 '14 at 14:01
  • Hummm ok sorry :) For instance, t's not working because of my depencies. I was first using maven-jar-plugin to also generate a .jar. But the jar didn't came with the dependencies. I tried maven-shade-plugin but it only did an another war not a .jar. Now i'm playing with maven-assembly-plugin.... Quite hard to find a good path to knowledge ^^ This is the code i'd like to see working http://stackoverflow.com/questions/23219219/classpathxmlapplicationcontext-from-a-runtime-getruntime-exec . For now, i'm lost :) – ZheFrench Apr 22 '14 at 14:06
  • @ZheFrench - If the class you are trying to run is in the WAR, and all of its dependencies are bundled into the WAR, then you are probably better off trying to build / use a classpath that refers to the relevant JARs and / or class directories in the expanded WAR file; i.e. in your webapp's WEB-INF tree in your web container. – Stephen C Apr 23 '14 at 01:00
  • I tried java -cp /Users/JP/git/CleanOmicsTracer/target/CleanOmicsTracer.war com.clb.genomic.lyon.external.MainExternal in console. That did not work. I'll post a png of my hierarchy in target directory. – ZheFrench Apr 23 '14 at 07:42
  • @ZheFrench - No no no. The `-cp` classpath needs to refer to the JAR pathnames and directory pathnames in the webapp directory tree. The `java` command's class loader doesn't know how to look inside a WAR file. – Stephen C Apr 23 '14 at 08:22
  • For instance to make it work my solution is to use String[] command= {"java","-jar", "/Users/JP/git/CleanOmicsTracer/target/CleanOmicsTracer-final.jar"}; This will work fine but with this method i need to launch maven with jar packaging first and maven-shade plugin config in the pom. Then i need to move the jar to save it somewhere because I then relaunch mvn clean install (with modification of packaging project as war no more jar) to get the war. It's a bit cumbersome as i explain there.http://stackoverflow.com/questions/23238199/maven-shade-plugin-make-a-jar-whereas-we-are-in-war-project. – ZheFrench Apr 23 '14 at 08:41
0

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
rxn1d
  • 1,236
  • 6
  • 18