从命令行运行spring项目 [英] run the spring project from command line

查看:630
本文介绍了从命令行运行spring项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在STS(春天源工具)创建了一个春天项目在STS工作(编译和执行)罚款,但是当我运行类文件从终端我得到这样的错误:


线程main中的异常java.lang.NoClassDefFoundError:com / XXXX / XXXX /操作

引发者:java.lang。 ClassNotFoundException:com.XXXX.XXXX.Operations


下面的参考是我的.classpath文件:

 <?xml version =1.0encoding =UTF-8?> 
< classpath>
< classpathentry kind =srcoutput =target / classespath =src / main / java/>
< classpathentry kind =srcoutput =target / test-classespath =src / test / java/>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINER /
org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType / J2SE-1.5/ >
< classpathentry kind =conpath =org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER/>
< classpathentry kind =outputpath =target / classes/>
< classpathentry exported =truekind =varpath =M2_REPO/>
< / classpath>


解决方案

您的错误表明Operations.class文件不在目标/ classes文件夹中(或者您的程序包名称中有打字错误)。



首先确保您的类是您期望的:

  cd [project root] / target / classes 
ls com / XXXX / XXXX / Operations.class

如果这证明文件存在,您应该能够使用您提到的java命令:

  java com.XXXX.XXXX.Operations 

确保不要在末尾添加.class,因为Java将假定您正在尝试在操作包中加载名为class的文件。



如果文件丢失(我预计会发生),您需要在STS中执行构建( Project - > Build Project




更新


$ b b

您在评论中提到您收到错误:

 线程mainjava.lang中的异常。 NoClassDefFoundError:org / springframework / context / ApplicationContext 

这是一个非常不同的问题,在你的java命令中引用Spring库,所以Java不知道在哪里找到它们。



我建议你在尝试运行它之前用STS打包程序。这是Java开发人员构建和部署应用程序的典型方式。 本文提供了有关如何执行此操作的简单教程(STS是



添加 - 您更新的问题表明您还在此应用程序中使用Maven,在这种情况下,您可以使用mvn package来构建项目的Eclipse。请阅读本教程,开始使用Maven: http:/ /maven.apache.org/guides/getting-started/maven-in-five-minutes.html



如果您真的想要运行程序,包装;您需要在命令中引用全部 Jar文件(这只是一个示例,将spring.jar替换为您正在使用的正确Jar文件):

  java -classpath。; ../../ lib / spring.jar com.XXXX.XXXX.Operations 


I have created one spring project in STS (spring source tool) which works (compile and execution) fine in STS, but when I am run the class file from terminal I am getting an error like this:

Exception in thread "main" java.lang.NoClassDefFoundError: com/XXXX/XXXX/Operations
Caused by: java.lang.ClassNotFoundException: com.XXXX.XXXX.Operations

For reference below is my .classpath file:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java" />
<classpathentry kind="src" output="target/test-classes"path="src/test/java" />
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/
org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5" />
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
<classpathentry kind="output" path="target/classes" />
<classpathentry exported="true" kind="var" path="M2_REPO" />
</classpath>

解决方案

Your error suggests that the "Operations.class" file is not in the target/classes folder (or you have a typo in your package name).

First ensure your class is where you expect it:

cd [project root]/target/classes
ls com/XXXX/XXXX/Operations.class

If this proves the file is present, you should be able to use the java command you mentioned:

java com.XXXX.XXXX.Operations

Make sure you do not add ".class" at the end, as Java will assume you are trying to load a file called "class" in the "Operations" package.

If the file is missing (I expect it will be), you will need to execute a build in STS (Project -> Build Project) to create it.


Update

You mention in the comments that you are getting the error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

This is a very different problem, it means you have not referenced the Spring libraries in your java command, so Java doesn't know where to find them.

I recommend you package the program with STS before attempting to run it. This is the typical way Java developers build and deploy applications. This article provides a simple tutorial on how to do this (STS is a variant of Eclipse).

Addition - your updated question suggests you are also using Maven in this application, in which case you can use "mvn package" to build the project instead of Eclipse. Read this tutorial to get started on using Maven: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

If you REALLY want to run the program without packaging it; you will need to reference ALL the Jar files you are using in the command (this is only an example, replace "spring.jar" with the correct Jar files you are using):

java -classpath .;../../lib/spring.jar com.XXXX.XXXX.Operations

这篇关于从命令行运行spring项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆