如何在没有IDE的情况下编译这个基本的Spring应用程序?

时间:2015-06-29 12:50:20

标签: java compilation

这是我的目录布局:

~ koraytugay$ ls -1 biz/tugay/hellospring/
Bike.java
Car.java
Vehicle.java
VehicleApp.java
VehicleService.java
beans.xml

所以我在根文件夹中,我的.java文件位于 biz / tugay / hellospring /

VehicleApp代码:

package biz.tugay.hellospring;
/* User: koray@tugay.biz Date: 29/06/15 Time: 15:16 */

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class VehicleApp {

    public static void main(String[] args) {
        ApplicationContext applicationContext
                = new ClassPathXmlApplicationContext("biz/tugay/hellospring/beans.xml");
        VehicleService vehicleService = (VehicleService) applicationContext.getBean("vehicleService");
        vehicleService.driver();
    }

}

和VehicleService:

package biz.tugay.hellospring;
/* User: koray@tugay.biz Date: 29/06/15 Time: 15:10 */

public class VehicleService {

    private Vehicle vehicle;

    public void setVehicle(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    public void driver(){
        System.out.println(vehicle.drive());
    }

}

同样在我的主目录中,我有以下jar文件:

~ koraytugay$ ls -1 *.jar
spring-aop-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-security-core-3.2.5.RELEASE.jar

我尝试了几种变化但是我没有成功。一个例子:

~ koraytugay$ javac -cp .:/biz/tugay/hellospring biz/tugay/hellospring/VehicleApp.java
biz/tugay/hellospring/VehicleApp.java:4: error: package org.springframework.context does not exist
import org.springframework.context.ApplicationContext;
                                  ^
biz/tugay/hellospring/VehicleApp.java:5: error: package org.springframework.context.support does not exist
import org.springframework.context.support.ClassPathXmlApplicationContext;
                                          ^
biz/tugay/hellospring/VehicleApp.java:10: error: cannot find symbol
        ApplicationContext applicationContext
        ^
  symbol:   class ApplicationContext
  location: class VehicleApp
biz/tugay/hellospring/VehicleApp.java:11: error: cannot find symbol
                = new ClassPathXmlApplicationContext("biz/tugay/hellospring/beans.xml");
                      ^
  symbol:   class ClassPathXmlApplicationContext
  location: class VehicleApp
4 errors

我正在执行javac的文件夹中有.jar文件。为什么编译器无法找到ClassPathXmlApplicationContext?

1 个答案:

答案 0 :(得分:1)

您需要指定每个jar

java -cp ./spring-aop-3.2.5.RELEASE.jar ; ./spring-beans-3.2.5.RELEASE.jar ;./spring-context-3.2.5.RELEASE.jar ; ./spring-security-core-3.2.5.RELEASE.jar

或使用外卡

java -cp *.jar; /otherpath

但这仅适用于java 6 up