在window下, Java调用执行bat脚本
参考博客: https://www.cnblogs.com/jing1617/p/6430141.html
最近一段时间用到了Java去执行window下的bat脚本, 这里简单记录一下:
我这里是先判断bat脚本是否存在, 然后去决定是否执行bat脚本,
直接上代码:
下面是我测试的bat脚本, 就输出一句话, 把文件命令为PostStartupScript.bat:
echo "hello word"
package com.test; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; public class Test { public static void main(String[] args) {
// TODO Auto-generated method stub
String batPath = "C:/Philips/SIServer/PostStartupScript.bat"; // 把你的bat脚本路径写在这里
File batFile = new File(batPath);
boolean batFileExist = batFile.exists();
System.out.println("batFileExist:" + batFileExist);
if (batFileExist) {
callCmd(batPath);
}
} private static void callCmd(String locationCmd){
StringBuilder sb = new StringBuilder();
try {
Process child = Runtime.getRuntime().exec(locationCmd);
InputStream in = child.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
String line;
while((line=bufferedReader.readLine())!=null)
{
sb.append(line + "\n");
}
in.close();
try {
child.waitFor();
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("sb:" + sb.toString());
System.out.println("callCmd execute finished");
} catch (IOException e) {
System.out.println(e);
}
}
}
运行结果如下:
batFileExist:true
sb:
D:\TestJava>echo "hello word"
"hello word" callCmd execute finished
这里是在不打开任何窗口下运行的, 非常适合那些在后台需要执行bat脚本的程序.
如果想让程序打开窗口去运行bat脚本, 可以使用如下的命令:
java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口.
cmd /k dir 是执行完dir命令后不关闭命令窗口.
cmd /c start dir 会打开一个新窗口后执行dir指令, 原窗口会关闭.
cmd /k start dir 会打开一个新窗口后执行dir指令, 原窗口不会关闭. 例如下图, 输入 cmd /k start C:/Philips/SIServer/PostStartupScript.bat
然后会弹出新的窗口, 执行bat脚本.
在window下, Java调用执行bat脚本的更多相关文章
- java 调用windows bat脚本
当我们需要在java程序中调用外部程序,我们可用通过Runtime.exec()调用来完成. The class java.lang.Runtime features a static method ...
- Windows下Java调用BAT批处理不弹出cmd窗口
常规Windows下Java调用BAT方式肯定会弹出cmd窗口 Runtime.getRuntime().exec("cmd.exe /C start D:\\test.bat") ...
- Windows 2008任务计划执行bat脚本失败返回0x1
测试环境: C:\>systeminfo | findstr /c:"OS Name"OS Name: Microsoft Windows ...
- java 调用bash shell脚本阻塞的小问题的解决
java 调用bash shell脚本阻塞的小问题的解决 背景 使用java实现的web端,web端相应用户的界面操作,使用java调用bash实现的shell脚本进行实际的操作,操作完成返回执行结 ...
- JAVA远程执行Shell脚本类
1.java远程执行shell脚本类 package com.test.common.utility; import java.io.IOException; import java.io.Input ...
- linux下java调用.so文件的方法1: JNI
摘自http://blog.163.com/squall_smile/blog/static/6034984020129296931793/ https://my.oschina.net/simabe ...
- 通过Jekins执行bat脚本始终无法完成
问题描述 最近在研究Devops工作流,中间有一个环节是自动发布版本的,我们使用PipeLine调用Jekins任务,最终执行bat脚本,但在执行Jekins任务的时候,任务总是完成不了,导致DBA在 ...
- windows和linux环境下java调用C++代码-JNI技术
最近部门做安卓移动开发的需要调C++的代码,困难重重,最后任务交给了我,查找相关资料,没有一个教程能把不同环境(windows,linux)下怎么调用说明白的,自己在实现的过程中踩了几个坑,在这里总结 ...
- windows开机自动执行bat脚本
一.以windows下备份sql数据库为例,开机自动执行.bat脚本 1.新建dump.bat文件,文件中的代码如下: set YYYYmmdd=%date:~0,4%%date:~5,2%%date ...
随机推荐
- 【转】eclipse中window->preference选项中没有tomcat的解决方法
eclipse中window->preference选项中没有tomcat的解决方法 2011-09-09 13:46:35| 分类: eclipse|字号 订阅 其实一共有好几种方法,这只是 ...
- Dijkstra 路径规划 C#
示例无向图如下:(起始点为v0) 邻接矩阵为: 注意:其中没有连接的边和自己到自己的点权值用10000表示. 代码: static void Main(string[] args) { , , , , ...
- vim 设置TAB宽度、显示行号、自动缩进、自动换行宽度
一.vim ~/.vimrc 二.添加如下几行:(括号中的不是,是我添加的) set shiftwidth=4 (表示每一级缩进的长度)set softtabstop=4 ...
- LeetCode: 455 Assign Cookies(easy)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
- KOL运营之——如何与网文作者高效地约稿?
本文来自网易云社区,转载务必请注明出处. 随着网络文学的发展,影响力逐渐扩大,越来越多的同事在工作中遇到需要和这些作者打交道的时候.对于作者这个群体,很多时候都是只闻其书,不见其人.要跟这样的群体打交 ...
- MongoDB的安装避坑(踩坑)
下载 可以去官网下载:https://www.mongodb.com/download-center/community 安装 下载完了就可以使用安装包安装:我下载的mongodb版本是:v4.0.9 ...
- 2017-10-3 清北刷题冲刺班a.m
P99zhx a [问题描述]你是能看到第一题的 friends 呢.——hja怎么快速记单词呢?也许把单词分类再记单词是个不错的选择.何大爷给出了一种分单词的方法,何大爷认为两个单词是同一类的当这两 ...
- Mol Cell Proteomics. |赵赟| 全面地分析个人尿蛋白质组学的变化揭示出不同的性别变化
大家好,本周分享的是发表在Molecular & Cellular Proteomics上的一篇关于人的尿蛋白质组学的文章,题目是Comprehensive analysis of indiv ...
- mongodb you can't add a second
问题信息: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'createTime' expre ...
- Jmeter3.2源码编译环境搭建(转)
1.下载jmeter3.2源码 https://github.com/apache/jmeter/tree/v3_2 https://blog.csdn.net/fly_to_higher/artic ...