Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBootWar从maven运行,而不是从命令行运行?_Java_Spring_Maven_Tomcat - Fatal编程技术网

Java SpringBootWar从maven运行,而不是从命令行运行?

Java SpringBootWar从maven运行,而不是从命令行运行?,java,spring,maven,tomcat,Java,Spring,Maven,Tomcat,,但答案(虽然有趣)基本上是说“不要使用战争包装”,这在这里不是一个选项 我有一个重新打包的Spring Boot WAR,它使用嵌入式Tomcat(Spring Boot 1.5.3.RELEASE,嵌入式Tomcat 8.5.14)。使用mvn spring boot:run命令重新打包的应用程序运行良好,但当我尝试使用'java-jar target\mywar.war'运行它时,我得到以下结果: java.io.FileNotFoundException: file:\C:\work\s

,但答案(虽然有趣)基本上是说“不要使用战争包装”,这在这里不是一个选项

我有一个重新打包的Spring Boot WAR,它使用嵌入式Tomcat(Spring Boot 1.5.3.RELEASE,嵌入式Tomcat 8.5.14)。使用
mvn spring boot:run
命令重新打包的应用程序运行良好,但当我尝试使用'java-jar target\mywar.war'运行它时,我得到以下结果:

java.io.FileNotFoundException: file:\C:\work\spring-boot\target\spring-boot-mywar.war!\WEB-INF\classes!\mywar.war (The filename, directory name, or volume label syntax is incorrect)
这是在Spring Boot尝试加载“包装”war的上下文时造成的:

实际错误发生在嵌入式Tomcat类中:

private URL getWebappConfigFileFromJar(File docBase, String contextName) {
    URL result = null;
    try (JarFile jar = new JarFile(docBase)) {
        JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
        if (entry != null) {
            result = UriUtil.buildJarUrl(docBase, Constants.ApplicationContextXml);
        }
    } catch (IOException e) {
        Logger.getLogger(getLoggerName(getHost(), contextName)).log(Level.WARNING,
                "Unable to determine web application context.xml " + docBase, e);
    }
    return result;
}
操作
newjarfile(docBase)
抛出
FileNotFoundException

当使用Maven
spring-boot:run
目标时,这很好,因此我觉得基本结构是合理的——我认为存在一些类路径问题或是一些阻碍它工作的因素


有人建议在使用WAR打包时在命令行上复制
spring boot:run
的环境吗?

对于spring boot应用程序,您可以使用
mvn spring boot:run
,使用
java-jar包.WAR
运行,或者将WAR包放在tomcat webapps中。每种方法都应该可以运行你的应用程序


所以我认为你的项目有一些问题。确保pom文件中有
springbootmaven插件
。然后,当您使用
mvn包时,您应该会看到一些关于重新打包的日志。如果您取消打包war文件,您应该会看到一些目录,如:“META-INFO”和“BOOT-INF”。

在探索插件的实际spring BOOT:run目标时,发现它重建了项目,并从
target/classes
文件夹中编译的类启动应用程序。它似乎根本没有使用WAR文件:

[INFO] --- spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) @ spring-boot-app ---
11:58:55.526 [main] INFO app.MyApplication - Beginning run() of MyApplication
2017-05-22 11:58:55.791 DEBUG 4140 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application started with classpath: [file:/C:/work/myapp/target/classes/....[SNIP]
WAR不会运行的原因是内部包含的应用程序WAR文件没有被提取并写入嵌入式Tomcat上下文。手动提取“内部战争”并将其写入上下文位置解决了问题:

/**
 * Utility method which exports and unpacks the WAR file into the Tomcat context directory
 * @param warName
 * @param contextPath
 * @return
 * @throws IOException
 * @throws URISyntaxException
 */
private static String exportWar(String warName, String contextPath) throws IOException, URISyntaxException {

    log.info("Beginning export WAR");
    try {
        UnzipUtility unzipUtility = new UnzipUtility();
        unzipUtility.unzip(warName, contextPath);
    } catch (IOException ex) {
        throw ex;
    }
    return contextPath + warName;
}
以下是基于codejava.net中的一个示例的不可计算性:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * This utility extracts files and directories of a standard zip file to
 * a destination directory.
 * @author www.codejava.net
 *
 */
public class UnzipUtility {
    /**
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;

    public void unzip(String zipFileName, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(zipFileName));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                ensureParentExists(filePath);
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }
    /**
     * Extracts a zip entry (file entry)
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }

    private void ensureParentExists(String filePath) {
        File parent = new File(filePath).getParentFile();
        if ( parent != null && !parent.exists()) {
            // parent of parent - recursive
            ensureParentExists(parent.getPath());
            // make this dir
            parent.mkdir();
        }
    }
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * This utility extracts files and directories of a standard zip file to
 * a destination directory.
 * @author www.codejava.net
 *
 */
public class UnzipUtility {
    /**
     * Size of the buffer to read/write data
     */
    private static final int BUFFER_SIZE = 4096;

    public void unzip(String zipFileName, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        ZipInputStream zipIn = new ZipInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(zipFileName));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                ensureParentExists(filePath);
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }
    /**
     * Extracts a zip entry (file entry)
     * @param zipIn
     * @param filePath
     * @throws IOException
     */
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }

    private void ensureParentExists(String filePath) {
        File parent = new File(filePath).getParentFile();
        if ( parent != null && !parent.exists()) {
            // parent of parent - recursive
            ensureParentExists(parent.getPath());
            // make this dir
            parent.mkdir();
        }
    }
}