0

I developp a web app in eclipse with spring to handle dependency injection & maven to deploy. I'm trying to make work this little code :

public class MainExternal {


public static void main( String[] args ) throws Exception{


        @SuppressWarnings("resource")           ApplicationContext appContext = new ClassPathXmlApplicationContext( "classpath*:webConfiguration/applicationContext.xml");          ProjectBo projectBo                  = (ProjectBo) appContext.getBean("projectBo");

        System.out.println("-> Im 'in ");

        /************* PRINT OUT  *************/

        Project project = projectBo.findByNameOfStudy("Profiler");


        List<User> listUser = (List<User>) projectBo.findUsers(project);

        for (User myUser : listUser) {

                        System.out.println("User :"+myUser.getFirstname());

        }   
}

When i run it inside eclipse, it works.

But when i call it from my web app like followed, it doesn't work :

public void throwAnalyse(){

    System.out.println("->I call my function");

    try {

    String[] command= {"java","-cp", "/Users/JP/git/CleanOmicsTracer/target/CleanOmicsTracer.jar", "com.clb.genomic.lyon.external.MainExternal"};

    Process p = Runtime.getRuntime().exec(command);

    ////////////////////////////////////////////
    //test for remote command
    String line;
    BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            while ((line = bri.readLine()) != null) {
                System.out.println(line);
            }
            bri.close();

            while ((line = bre.readLine()) != null) {
                System.out.println(line);
            }
            bre.close();

     p.waitFor();

    } catch (IOException e) {  e.printStackTrace();

    } catch (InterruptedException e) {  e.printStackTrace(); }


    System.out.println("End of programme");
}

When i tried to execute from console as :

java -cp /Users/JP/git/CleanOmicsTracer/target/CleanOmicsTracer.jar com.clb.genomic.lyon.external.MainExternal

It doesn't work anymore and throws :

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

ZheFrench
  • 1,164
  • 3
  • 22
  • 46

1 Answers1

0

Add the Spring jar files to your classpath. For the master jar file you could do

java -cp /path/to/CleanOmicsTracer.jar:spring-full-1.0.1.jar com.clb.genomic.lyon.external.MainExternal

If you have multiple JAR files, you could use a classpath wildcard

java -cp /path/to/CleanOmicsTracer.jar:lib/* com.clb.genomic.lyon.external.MainExternal
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Ok i see your point. But what is the correct path to use ? I use Maven to manage my dependencies.In eclipse, when i made a clean install the jar are into the target directory inside WEB-INF/lib. Looks weird, and more , the path will change on deployement , no ? – ZheFrench Apr 22 '14 at 12:26
  • Since you're using Maven I would let that manage finding the dependent jar files. See [this](http://stackoverflow.com/questions/2472376/how-do-i-execute-a-program-using-maven) – Reimeus Apr 22 '14 at 12:31