首页 > 系统相关 >JAVA执行bat文件和shell脚本文件

JAVA执行bat文件和shell脚本文件

时间:2023-02-28 18:13:49浏览次数:32  
标签:文件 bat shell java String io new println import


1。 bat文件

import java.io.IOException;
import java.io.InputStream;


public class callbat {


public static void main(String args[]){
callCmd("C:/run.bat");
}
public static void callCmd(String locationCmd){
try {
Process child = Runtime.getRuntime().exec("cmd.exe /C start "+locationCmd);
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1) {
}
in.close();
try {
child.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("done");
} catch (IOException e) {
e.printStackTrace();
}
}
}





2.shell文件



import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class callshell {

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

Runtime rt=Runtime.getRuntime();
String command="/export/home/xlg/solarischk.sh";
Process pcs=rt.exec(command);
PrintWriter outWriter=new PrintWriter(new File("/export/home/zjg/show.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(pcs.getInputStream()));
String line=new String();
while((line = br.readLine()) != null)
{
System.out.println(line);
outWriter.write(line);
}
try{
pcs.waitFor();
}
catch(InterruptedException e){
System.err.println("processes was interrupted");
}
br.close();
outWriter.flush();
outWriter.close();
int ret=pcs.exitValue();
System.out.println(ret);
System.out.println("执行完毕!");
}

————————————————
版权声明:本文为CSDN博主「iteye_13851」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/iteye_13851/article/details/82646802

标签:文件,bat,shell,java,String,io,new,println,import
From: https://www.cnblogs.com/telwanggs/p/17165481.html

相关文章

  • Powershell 创建Hyper-v 虚拟机
    functionCreateVM{Param(   [Parameter(Mandatory=$true)]   [string]$VMName,   #cancreateaVMusinganexistingVHDtemplate,ifnotcreatethenewo......
  • shell-实现一些小功能脚本
    实现免密登录vim./passwdpasswd="xxxxxxxx"#!/bin/bashsource./passwdfunctionsecret_free_login(){ls/root/.ssh/id_rsa.pub&>/dev/null......
  • 【Vim】禁止 Vim 生成临时文件
    解决方法:在~/.vimrc内增加如下内容,并重启vim编辑器:setnobackup"nobackupfilessetnoswapfile"noswapfilessetnowritebackup"onlyincaseyo......
  • shell-对于变量的使用的一些脚本实现方式
    for循环简单实现#!/bin/bashcd/root/health-check/>/tmp/bomp-check-result.jsonforiin$(ls./check*.sh)dobash./$idone通过端口判断kubel......
  • JS文件夹上传解决方案
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持......
  • C#文件转Byte存储到SQLServer数据库
    场景:临时不同的系统,相同的数据库,通用文件读取1.数据库字段采用:varbinary(max)类型2.代码注:这里获取文件是通过先将byte[]数据先存到当前服务器对应项目的文件夹中(加了......
  • java文件的操作
    读Stringfilename="文件路径\\目标文件+文件后缀名"; java.io.Filefile=newjava.io.File(filename);\\创建一个文件类并赋予路径java.io.FileInputStreamfis=......
  • JS通过后台返回的Url下载文件
    需求:JS下载后台返回的url场景:用过window.open和window.location.href,效果不理想,都是打开一个新的网页(碰到txt和png是直接打开,不下载) 上代码:functiondownFile(id,file......
  • 【MyBatis】测试链接数据库查询用户
    配置文件访问:【MyBatis】配置Mybatis项目-小鼻涕孩-博客园(cnblogs.com)数据表数据:Test.java:1importorg.apache.ibatis.io.Resources;2importorg.apache.......
  • Hive加载HDFS文件注意事项
      Flink读取kafka数据写入hdfs,生成了带下划线的文件名,创建hive表指定该路径,发现数据读取不出来。    查阅源码,原来是会忽悠带下划线和.的文件。   ......