의존성 주입 & maven을 배포하기 위해 스프링이있는 Eclipse에서 웹 응용 프로그램을 개발합니다. 나는 일이 작은 코드를 만들려고 노력 해요 : 이클립스 내부에서 그것을 실행하면Runtime.getRuntime.exec의 ClassPathXmlApplicationContext
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());
}
}
을, 그것을 작동합니다.
하지만 다음처럼 내 웹 응용 프로그램에서 호출 할 때, 그것은 작동하지 않습니다
java -cp /Users/JP/git/CleanOmicsTracer/target/CleanOmicsTracer.jar com.clb.genomic.lyon.external.MainExternal
그것은 작동하지 않습니다
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");
}
내가 같이 콘솔에서 실행하려고 할 때 이상과 던졌습니다 :
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)
가 좋아 난 당신의 지점을 참조하십시오 클래스 경로 와일드 카드를 사용할 수 있습니다. 그러나 올바른 경로는 무엇입니까? 나는 Maven을 사용하여 의존성을 관리한다. 이클립스에서, jar 파일을 WEB-INF/lib 내부의 대상 디렉토리에 새로 설치할 때. 이상하게 보입니다. 배포가 진행되면서 경로가 변경됩니다. – ZheFrench
Maven을 사용하고 있기 때문에 종속 jar 파일을 찾을 수 있도록 관리합니다. [this] (http://stackoverflow.com/questions/2472376/how-do-i-execute-a-program-using-maven)를 참조하십시오. – Reimeus