我使用spring在eclipse中开发一个web应用来处理依赖注入& maven要部署。 我正在努力使这个小代码工作:
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());
}
}
当我在eclipse中运行时,它可以工作。
但是,当我从我的网络应用程序中调用它时,它不起作用:
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");
}
当我尝试从控制台执行时:
java -cp /Users/JP/git/CleanOmicsTracer/target/CleanOmicsTracer.jar com.clb.genomic.lyon.external.MainExternal
它不再起作用并抛出:
线程“main”中的异常java.lang.NoClassDefFoundError:org / springframework / context / ApplicationContext 引起:java.lang.ClassNotFoundException:org.springframework.context.ApplicationContext 在java.net.URLClassLoader $ 1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) 在java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301) 在java.lang.ClassLoader.loadClass(ClassLoader.java:247)
答案 0 :(得分:0)
将Spring jar文件添加到类路径中。对于主jar文件,你可以做
java -cp /path/to/CleanOmicsTracer.jar:spring-full-1.0.1.jar com.clb.genomic.lyon.external.MainExternal
如果您有多个JAR文件,则可以使用类路径通配符
java -cp /path/to/CleanOmicsTracer.jar:lib/* com.clb.genomic.lyon.external.MainExternal