You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过CMD运行Maven Web应用?嵌入式Tomcat视频网站部署求助

Hey Karan,我帮你一步步梳理这个问题的解决思路,先搞定本地CMD运行的卡点,再推进到服务器部署和域名访问的环节~

一、先解决CMD中mvn exec运行失败的问题

首先得确认你嵌入式Tomcat的配置和Maven插件是否正确,毕竟IntelliJ能跑说明代码逻辑没问题,大概率是Maven执行的配置遗漏了细节:

1. 检查pom.xml里的exec-maven-plugin配置

确保插件指定了正确的启动主类,示例配置如下:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!-- 替换成你自己的启动主类全路径,比如 com.example.TomcatBootstrap -->
        <mainClass>你的主类包路径.主类名</mainClass>
    </configuration>
</plugin>

2. 确认嵌入式Tomcat的主类配置(关键!)

因为你用了JSP,嵌入式Tomcat默认不支持JSP解析,必须手动配置Jasper依赖和资源路径,主类示例代码如下:

import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import java.io.File;

public class TomcatBootstrap {
    public static void main(String[] args) throws Exception {
        Tomcat tomcat = new Tomcat();
        // 自定义端口,默认8080,服务器部署时可以改
        tomcat.setPort(8080);

        // 指定webapp目录,对应项目里的src/main/webapp
        String webappDir = "src/main/webapp/";
        StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDir).getAbsolutePath());

        // 配置JSP支持,必须添加这部分
        File classesDir = new File("target/classes");
        WebResourceRoot resources = new StandardRoot(ctx);
        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes",
                classesDir.getAbsolutePath(), "/"));
        ctx.setResources(resources);

        tomcat.start();
        tomcat.getServer().await();
    }
}

3. 检查pom.xml的依赖完整性

必须包含嵌入式Tomcat核心、JSP解析依赖,示例如下:

<dependencies>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>9.0.80</version> <!-- 选稳定版本即可 -->
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>9.0.80</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

4. 正确执行mvn命令

在CMD里进入项目根目录,执行:

mvn clean compile exec:java

如果还是报错,把控制台的错误信息贴出来排查,但按上面的配置,基本能解决本地CMD运行的问题。


二、部署到远程服务器+域名访问

搞定本地运行后,接下来是服务器部署,推荐打包成可执行Fat Jar,方便直接运行:

1. 配置Maven打包插件(生成Fat Jar)

在pom.xml里添加maven-assembly-plugin,把所有依赖打包到一个Jar里:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <!-- 同样替换成你的启动主类全路径 -->
                <mainClass>你的主类包路径.主类名</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-fat-jar</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

执行mvn clean package,target目录下会生成带-jar-with-dependencies后缀的可执行Jar。

2. 上传Jar到远程服务器

用scp命令(或者FTP工具)把Jar传到服务器:

scp target/你的项目名-jar-with-dependencies.jar 服务器用户名@服务器IP:/服务器上的存放路径

3. 在服务器后台运行Jar

直接运行的话,关闭SSH会话程序就会停止,所以用nohup后台运行:

# 后台运行,输出日志到app.log
nohup java -jar 你的项目名-jar-with-dependencies.jar > app.log 2>&1 &

可以用ps aux | grep java查看程序是否在运行。

4. 域名绑定与端口配置

方案1:直接用80端口(需要root权限)

把Tomcat主类里的端口改成80,然后用sudo java -jar ...运行,这样访问域名http://你的域名.com就能直接打开网站。

方案2:用Nginx反向代理(推荐)

更灵活,还能轻松配置HTTPS:

  1. 服务器上安装Nginx,然后新建配置文件/etc/nginx/sites-available/你的域名.conf:
server {
    listen 80;
    server_name 你的域名.com;

    location / {
        # 指向Tomcat的端口,比如8080
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
  1. 启用配置:
ln -s /etc/nginx/sites-available/你的域名.conf /etc/nginx/sites-enabled/
  1. 重启Nginx:
sudo systemctl restart nginx
  1. (可选)配置HTTPS:用Let's Encrypt的certbot工具生成免费SSL证书,自动配置Nginx,命令如下:
sudo certbot --nginx -d 你的域名.com

最后提醒下:视频文件放在src/main/webapp/videos目录下,JSP里的视频路径写成<video src="/videos/你的视频.mp4" controls></video>,这样本地和服务器都能正常访问。

内容的提问来源于stack exchange,提问作者Karan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:43:32