0

I try to write a simple Spring 3 console application. I Cant get this application to run, i always get the error, that there is no main method. My system is an Ubuntu 12.04 with openjdk-7 installed, and the sts 2.9.2-release. A simple hello world runs without any problems Edit:(i tested a other project to prove a simple hello world would run).

The Project is managed over maven and i got no errors so far.

I try to reproduce an exsample of a book as follow:

XmlConfigWithBeanFactory.java

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

import org.springframework.core.io.FileSystemResource;

public class XmlConfigWithBeanFactory {

public static void main(String[] args) {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader rdr = new XmlBeanDefinitionReader(factory);
    rdr.loadBeanDefinitions(new FileSystemResource(
            "src/xmlBeanFactory.xml"));
    Oracle oracle = (Oracle) factory.getBean("oracle");
    System.out.println(oracle.defineMeaningOfLife());
    }
}

Oracle.java

public interface Oracle {

    public String defineMeaningOfLife();
}

BookwormOracle.java

public class BookwormOracle implements Oracle {

    public String defineMeaningOfLife() {
        return "Encyclopedias are a waste of money - use the Internet";
    }

}

xmlBeanFactory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- oracle bean used for a few examples -->
<bean id="oracle" name="wiseworm" class="BookwormOracle"/>
</beans>

If you want i can also post the maven pom.xml, but i think there is no error all packages are loaded and linked.

I am happy for any hint google and other pages cant help me. Here is a other example what i am trying to do : http://www.devdaily.com/blog/post/java/load-spring-application-context-file-java-swing-application

And even this post Java can't find method main did not help me

Do i have to start this application as Java Application or AspectJ/JavaApplication. Is it not possible to start a console application that way from eclipse ? Do i need to publish my files to a server ( "add to server" but this isnt working too)?

Whats confused me most is, that i see the main method :) and it is the same syntax as any hello world application.

Thanks so far...

4

2 に答える 2

1

すべてのJavaファイルとXMLファイルを使用して、システムで実行しました。2つのエラーを修正した後、完全に機能しています。次回質問を投稿するときは、表示される例外またはエラーメッセージを投稿してください。

プロジェクトを実行したときに2つのエラーが発生しました

最初のエラー:

これは、「commons-logging」jarファイルがないことに関連していました。

jarファイルをダウンロードして、ビルドパスの下に配置しました。

ここで解決策を見つけることができます。また、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>org.test.testapp</groupId>
  <artifactId>testapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>
</dependencies>
</project>

2番目のエラー:

xmlBeanFactory.xmlは別のSpringバージョンを参照していました。Spring-2.5.jarに変更し、ビルドパスを更新しました。

これで、プロジェクトが実行されます。

于 2012-08-08T10:30:55.173 に答える
1

Right click on "XmlConfigWithBeanFactory.java" in the package explorer, and select "Run as Java Application". That should work.

于 2012-08-08T09:26:14.090 に答える