java 可运行 目录 war 包_java -jar *.war 可运行war包方法

import java.io.File;

import java.net.URL;

import java.security.ProtectionDomain;

import org.eclipse.jetty.server.Server;

import org.eclipse.jetty.webapp.WebAppContext;

public class CompareLuncher {

public static void main(String[] args) throws Exception {

String currentPath=new File("").getAbsolutePath();

//如果没有work目录,则创建,jetty默认解压路径

File work=new File(currentPath+"\\work");

if(!work.exists()){

work.mkdir();

}

Server server =null;

Integer port=8090;

server=new Server(port);

ProtectionDomain domain = CompareLuncher.class.getProtectionDomain();

URL location = domain.getCodeSource().getLocation();

WebAppContext webapp = new WebAppContext();

webapp.setContextPath("/");

webapp.setWar(location.toExternalForm());

server.setHandler(webapp);

server.start();

server.join();

//启动部署包的时候可以用这个

// // Server server = new Server(8080);

// // WebAppContext context = new WebAppContext();

// // context.setContextPath("/compare");

// // context.setWar("F:/compare.war");

// // server.setHandler(context);

// // server.start();

// // server.join();

//eclipse 测试的时候启用如下代码,debug模式下可以直接看到修改效果

//       Server server = new Server(8090);

//       ResourceHandler resourceHandler = new ResourceHandler();

//       resourceHandler.setDirectoriesListed(true);

//

//       server.setSendServerVersion(true);

//       server.setStopAtShutdown(true);

//       server.setHandler(getWebAppContext());

//       server.start();

//       server.join();

}

private static WebAppContext getWebAppContext() {

String path = CompareLuncher.class.getResource("/").getFile()

.replaceAll("/target/(.*)", "")

+ "/src/main/webapp";

//      System.out.println(path);

String path2 = new File("").getAbsolutePath() + "\\src\\main\\webapp";

// System.out.println();

return new WebAppContext(path2, "/");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值