简体   繁体   中英

How start Spring project from cmd?

I want to start project from cmd

The main method is:

package com.easytrip;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@EnableCaching
@SpringBootApplication
public class EasyTripApplication {
    public static void main(String[] args) {
        SpringApplication.run(EasyTripApplication.class, args);
    }
}

But when I try to run it by:

javac EasyTripApplication.java

It throw next:

EasyTripApplication.java:3: error: package org.springframework.boot does not exist
import org.springframework.boot.SpringApplication;
                               ^
EasyTripApplication.java:4: error: package org.springframework.boot.autoconfigure does not exist
import org.springframework.boot.autoconfigure.SpringBootApplication;
                                             ^
EasyTripApplication.java:5: error: package org.springframework.cache.annotation does not exist
import org.springframework.cache.annotation.EnableCaching;
                                           ^
EasyTripApplication.java:7: error: cannot find symbol
@EnableCaching
 ^
symbol: class EnableCaching
EasyTripApplication.java:8: error: cannot find symbol
@SpringBootApplication
 ^
  symbol: class SpringBootApplication
EasyTripApplication.java:11: error: cannot find symbol
        SpringApplication.run(EasyTripApplication.class, args);
        ^
  symbol:   variable SpringApplication
  location: class EasyTripApplication
6 errors

I know that I must at first compile all external libraries, but how?

You can package it in a jar using mvn clean package and then run it with java -jar your-application.jar .

Alternatively, you can use the spring-boot-maven-plugin as a dependency and use mvn spring-boot:run .

If your application does not currently use Maven or Gradle, I urge you to consider using it as it will make your life significantly easier.

As you're doing a Spring boot application you should be using either a Maven or Gradle to build the application.

Both of these tools will download all the required dependencies and produce an executable file you can run.

I'd recommend you start at https://projects.spring.io/spring-boot/ It'll also be easier if you download the Spring Tool Suite which gives you a configured Eclipse environment as a head start.

you need to set class path for all the libraries. Put all the jar file in to a folder like c:/java/lib and execute the javac command like below:-

javac -cp c:\java\lib\example1.jar;c:\java\lib\example2.jar  EasyTripApplication.java

Now while running the program

 java -cp c:\java\lib\example1.jar;c:\java\lib\example2.jar  EasyTripApplication

Also you can do it like below also Windows

CP="c:\java\lib\example1.jar;c:\java\lib\example2.jar"

javac -cp %CP% EasyTripApplication.java

In Unix/Linux

 CP="/home/java/lib/example1.jar:/home/java/lib/example2.jar"

 javac -cp "$CP" EasyTripApplication.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM