Adn*_*nan 2 java spring maven applicationcontext
我是春天和maven的新手.我有一个使用Spring的简单hello world项目.项目构建成功但我在运行时遇到以下错误:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
Run Code Online (Sandbox Code Playgroud)
这是主要的应用程序:App.java
package com.test.SpringMaven2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
System.out.println( "Hello World!" );
ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld hello = (HelloWorld) context.getBean("helloWorld");
hello.setName("Adnan");
hello.getName();
}
}
Run Code Online (Sandbox Code Playgroud)
这是HelloWorld bean
package com.test.SpringMaven2;
public class HelloWorld{
private String name;
public void setName(String name){
this.name = name;
}
public void getName(){
System.out.println("Hello, " + name);
}
}
Run Code Online (Sandbox Code Playgroud)
这是基于注释的配置文件:
package com.test.SpringMaven2;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig{
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>SpringMaven2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringMaven2</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
我在CMD中运行以下命令来运行应用程序:java -cp {nameofresultjarfile} com.test.SpringMaven2.App
但是上面的错误消息
有什么建议?
谢谢
您pom.xml创建一个jar文件,其中只包含项目中定义的类.但是不包括应用程序所需的所有其他依赖项(spring-core,spring-context-support).
如果您的应用程序是在eclipse中启动的,那么eclipse会将这些必需的jar文件集成到应用程序的类路径中,因此它可以运行.
如果你的应用程序是从CMD启动的,那么在类路径中找不到spring类,你得到一个java.lang.NoClassDefFoundError.
当从CMD(类路径)启动应用程序时,可以手动命名所有必需的jar,但更容易创建一个所谓的自包含jar文件,其中包含所有这些文件.这可以使用maven-assembly-plugin完成.
可以在此处找到如何创建自包含jar文件的示例.
现在可以从CMD启动自包含jar中的应用程序:
java -jar name_of_your_project -jar-with-dependencies.jar
| 归档时间: |
|
| 查看次数: |
19476 次 |
| 最近记录: |