@RequestMapping("/cmd2")
public String cmd2() throws IOException {
//CMDUtil.excuteCMDCommand("copy G:\\workspase\\pulldata\\drugshop-1\\target\\drugshop-1-0.0.1-SNAPSHOT.jar G:\\workspase\\pulldata\\drugshop-1");
//CMDUtil.excuteCMDCommand("ping www.baidu.com");
return CMDUtil.excuteCMDCommand("net stop zyyfrpc & ren G:\\workspase\\pulldata\\drugshop-1\\target\\drugshop-1-0.0.1-SNAPSHOT.jar xx.jar & ren G:\\workspase\\pulldata\\drugshop-1\\target\\drugshop-1-0.0.1-SNAPSHOT3.jar drugshop-1-0.0.1-SNAPSHOT.jar & net start zyyfrpc");
/*
* netrestart.bat
* net stop zyyfrpc & net start zyyfrpc
*/
}
@RequestMapping("/cmd3")
public String cmd3() throws IOException {
String newfile="drugshop-1-0.0.1-SNAPSHOT3.jar"; //新下载的文件
File f= JarTool.getPureFile(); //当前jar文件
String filename =f.getName(); //当前jar文件的文件名
String path = JarTool.getPureDir(); //当前jar文件所在目录
String newfilefullpath=path+"\\"+newfile; //新下载的jar文件完整路径
return CMDUtil.excuteCMDCommand("net stop zyyfrpc & ren "+f.getAbsolutePath()+" xx.jar & ren "+newfilefullpath+" "+filename+" & net start zyyfrpc");
}
获取jar路径的工具类
import java.io.File;
/**
* 获取打包后jar的路径信息
* @author Administrator
* 2011-01-16 13:53:12
*/
public class JarTool {
//获取jar绝对路径
public static String getJarPath(){
File file = getFile();
if(file==null)return null;
return file.getAbsolutePath();
}
//获取jar目录
public static String getJarDir() {
File file = getFile();
if(file==null)return null;
return getFile().getParent();
}
//获取jar包名
public static String getJarName() {
File file = getFile();
if(file==null)return null;
return getFile().getName();
}
public static File getFile() {
//关键是这行...
String path = JarTool.class.getProtectionDomain().getCodeSource()
.getLocation().getFile();
try{
path = java.net.URLDecoder.decode(path, "UTF-8");//转换处理中文及空格
}catch (java.io.UnsupportedEncodingException e){
return null;
}
return new File(path);
}
public static String getPurePath()
{
String jardir=getJarDir();
return jardir.substring(6,jardir.lastIndexOf("!"));
//return path.replace("target/classes/", "");
}
public static File getPureFile()
{
String jardir=getJarDir();
return new File(jardir.substring(6,jardir.lastIndexOf("!")));
//return path.replace("target/classes/", "");
}
public static String getPureFileName()
{
File f= getPureFile();
return f.getName();
//return path.replace("target/classes/", "");
}
public static String getPureDir()
{
File f= getPureFile();
return f.getAbsolutePath().substring(0,f.getAbsolutePath().lastIndexOf("\\"));
//return path.replace("target/classes/", "");
}
}