简体   繁体   中英

ClassPathXmlApplicationContext from a Runtime.getRuntime.exec

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM