当从 eclipse 作为独立应用程序运行时,这一切都有效。
我的Java主要方法:
public static void main(String[] args) throws MessagingException, IOException, RequestErrorFault, UnexpectedErrorFault, ServerErrorFault {
ApplicationContext context = new FileSystemXmlApplicationContext("src" + File.separator + "main" + File.separator + "webapp" + File.separator + "WEB-INF" + File.separator + "spring" + File.separator + "appServlet" + File.separator + "cron-context.xml");
CheckStatus checkStatus = context.getBean(CheckStatus.class);
List<Integer> requestIds = checkStatus.getRequests();
checkStatus.processRequests(requestIds);
}
输出:
java -cp /html/myProj/API/target/classes:/html/myProj/API/target/API-1.0.0-BUILD-SNAPSHOT/WEB-INF/lib com.api.spring.management.CheckStatus
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
我已传入类路径上的 lib 文件夹,其中所有 spring jar 都存在。我也尝试通过 mvnexec:java 运行它,但似乎都不起作用。
最佳答案
除此之外:
Try with java -cp "/html/myProj/API/target/classes:/html/myProj/API/target/API-1.0.0-BUILD-SNAPSHOT/WEB-INF/lib/" com.api.spring.management.CheckStatus. (note the / at the end) – Tunaki
事实证明,我的一个依赖项没有设置为在 war 中导出,所以在我修复后,一切都正常了。
关于java - 使用 Spring 从命令行运行 Java cron 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742254/