導航:首頁 > 編程語言 > javabat

javabat

發布時間:2022-01-18 22:47:21

java 運行bat文件並彈出CMD窗口

publicstaticvoidmain(String[]args)throwsIOException{
Desktop.getDesktop().open(newFile("d:/abc.bat"));
}

最簡單的

Runtime.getRuntime().exec("cmd.exe/CstartD:\abc.bat");

解決不彈框只需要「start」後面加一個參數「/b」就行:

⑵ java編程 批處理

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Stack;

public class CheckFilebatch {
private static final String LOG_FILE = "F:/CheckFilebatch.log"; //報錯的信息寫在這個文件里。
private static final String FILE_HOME = "F:/niceStore /"; //要操作的物理目錄

public static void main(String[] args) {
try {
ArrayList<String> fileListInDB = new ArrayList<String>();
FileWriter fw = new FileWriter(LOG_FILE);

// TODO
// 取得DB連接,執行sql「SELECT DISTINCT FILEPATH FROM ARR-FILE」,得到一個ResultSet
// 從DB中取出文件信息,放入fileListInDB 中,
ResultSet rs = null;
while (rs.next()) {
String path = rs.getString("File_Column");
File f = new File(path);
if (!f.exists()) {
// 不合法的報出錯誤信息。
System.out.println("file not exists: " + path);
fw.write("file not exists: " + path + "\n");
} else {
// 合法的,轉化為路徑名的規范路徑名字元串,加入到fileListInDB中
fileListInDB.add(f.getCanonicalPath());
}
}
rs.close();
//
fw.write("\n\n\n\n");

//下面開始遍歷物理目錄
File home = new File(FILE_HOME);
Stack<File> tmpStack = new Stack<File>();
tmpStack.add(home);
while (!tmpStack.isEmpty()) {
File tmp = tmpStack.pop();
File[] childs = tmp.listFiles();
for (int i = 0; i < childs.length; i++) {
File child = childs[i];
if (child.isDirectory()) {
tmpStack.push(child);
} else {
if (fileListInDB.contains(child.getCanonicalPath())) {
fileListInDB.remove(child.getCanonicalPath());
} else {
// 刪除文件。
child.delete();
// 這里的操作如果把它移動到另一個目錄里,作為備份,更好些。
fw.write("delete file: " + child.getCanonicalPath());
}
}
}
}
// 這個程序只考慮文件,不考慮目錄
fw.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

}
}

⑶ java.bat是什麼意思有什麼作用怎麼用

.bat一般是批處理程序的後綴。所謂批處理就是把在Windows操作系統下DOS界面的命令,按執行順序寫成一個.bat文件,然後雙擊這個文件就可以執行所有寫入的DOS命令。
比如你寫個 shutdown -s 然後保存為.bat雙擊就會關機,跟在DOS界面輸入這個命令效果一樣。

⑷ java調用bat文件傳入參數,急,急,急!

java好像不能直接給bat文件傳參數,不過你可以先生成一個你需要的bat文件,再去執行這個bat文件,我就是這么做的,給你寫了個例子,你參考下(你先在d盤下建一個text.txt)
public class DelFile {
public void creatBat(String command){
FileWriter fw=null;
try {
fw=new FileWriter("D:\\del.bat");
fw.write(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}finally{
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
}
}
}

private String execute(String batname){
Process process;
String line = null;
StringBuffer sb=new StringBuffer();
try {
process = Runtime.getRuntime().exec(batname);
InputStream fis = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
if(process.waitFor()!=0){
System.out.println("fail");
return "fail";
}
System.out.println(batname+" run successful!");
return "success";
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DelFile df=new DelFile();
df.creatBat("del D:\\text.txt");
df.execute("D:\\del.bat");
}

}

⑸ 怎麼在java中運行bat命令

@echo off

echo 正在加密,請稍後....

echo path:%~dp0

set base=%~dp0

set class=%base%in

set libs=%base%lib

set
class_path=%class%;%libs%c3p0-0.9.1.2.jar;%libs%mysql-connector-java-5.0.8-bin.jar;

java -classpath %class_path% com.test.Main

@pause

文件結構圖:

classes目錄是java編譯後的class目錄

libs目錄存放的所要使用到的jar

startNet.bat為我們啟動的bat文件

⑹ bat運行java文件

在window下bat批處理文件調用實現java方法。
代碼如下:

@echo off

echo 正在加密,請稍後....
echo path:%~dp0

set base=%~dp0

set class=%base%\bin
set libs=%base%\lib

set class_path=%class%;%libs%\c3p0-0.9.1.2.jar;%libs%\mysql-connector-java-5.0.8-bin.jar;

java -classpath %class_path% com.test.Main
@pause

classes目錄是java編譯後的class目錄

libs目錄存放的所要使用到的jar

startNet.bat啟動的bat文件

⑺ Java打開bat


importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.io.OutputStreamWriter;
importjava.util.LinkedList;
publicclassCommand{
privatejava.lang.Processp;
privateInputStreamis;
privateOutputStreamos;
privateBufferedWriterbw;
privateBufferedReaderbr;
privateProcessBuilderpb;
privateInputStreamstdErr;
publicCommand(){
}
//獲取Process的輸入,輸出流
publicvoidsetCmd(Stringcmd){
try{
p=Runtime.getRuntime().exec(cmd);
os=p.getOutputStream();
is=p.getInputStream();
stdErr=p.getErrorStream();
}catch(IOExceptione){
System.err.println(e.getMessage());
}
}
//向Process輸出命令
publicvoidwriteCmd(Stringcmd){
try{
bw=newBufferedWriter(newOutputStreamWriter(os));
bw.write(cmd);
bw.newLine();
bw.flush();
bw.close();
}catch(Exceptione){
e.printStackTrace();
}
}
//讀出Process執行的結果
publicStringreadCmd(){
StringBuffersb=newStringBuffer();
br=newBufferedReader(newInputStreamReader(is));
Stringbuffer=null;
try{
while((buffer=br.readLine())!=null){
sb.append(buffer+" ");
}
System.out.println(p.waitFor());
}catch(Exceptione){
e.printStackTrace();
}
returnsb.toString();
}
//將命令一股腦塞入list中
publicLinkedList<String>doCmd(LinkedList<String>lists){
LinkedList<String>list=newLinkedList<String>();
for(Strings:lists){
writeCmd(s);
list.add(readCmd());
}
returnlist;
}
publicstaticvoidmain(String[]args){
Commandcmd=newCommand();
cmd.setCmd("cmd");
cmd.writeCmd("d:\stock.xmld:\backStock.xml");
System.out.println(cmd.readCmd());
//LinkedList<String>list=newLinkedList<String>();
//list.add("dir/b");
//list=cmd.doCmd(list);
//for(Strings:list){
//System.out.print(s);
//}
}
}

⑻ Java中如何調用bat,並傳入參數

如果能動態指定bat文件中參數更好。例如: 1.bat中> java -cp Chart2D這里有調用windows程序的例子,你可以參考一下,就在調用的地方吧你的bat文件

⑼ java的bat是什麼

.bat一般是批處理程序的後綴。

是把在Windows操作系統下DOS界面的命令, 按執行順序寫成一個.bat文件,然後雙擊這個 文件就可以執行所有寫入的DOS命令。 比如你寫個 shutdown -s 然後保存為.bat雙擊 就會關機,跟在DOS界面輸入這個命令效果一 樣。

閱讀全文

與javabat相關的資料

熱點內容
證據提取命令視頻 瀏覽:353
java的學習心得 瀏覽:96
prof命令 瀏覽:279
手機加密文件密碼怎麼解開 瀏覽:283
賈躍亭程序員完整視頻 瀏覽:958
怎樣把兩個文件夾打包發送 瀏覽:378
單片機教程資料 瀏覽:982
仿大眾點評系統源碼python 瀏覽:425
手機網路伺服器連接不上是怎麼回事 瀏覽:155
電腦為什麼一直要解壓 瀏覽:530
淘客優惠券網站源碼 瀏覽:555
word轉成pdf在線 瀏覽:775
手機暴力解壓教程 瀏覽:130
解壓小視頻第二期 瀏覽:364
裝機自帶軟體找不到軟體文件夾 瀏覽:330
仙境之路伺服器地址ip 瀏覽:708
華為服務app是什麼東西 瀏覽:180
關於單片機的視頻 瀏覽:592
淘寶直播app緩存怎麼清理 瀏覽:555
android可以刷機嗎 瀏覽:350