導航:首頁 > 編程語言 > 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相關的資料

熱點內容
程序員真的累嗎 瀏覽:323
學信網app為什麼刷臉不了 瀏覽:871
天蠍vs程序員 瀏覽:991
單片機下載口叫什麼 瀏覽:186
程序員的道 瀏覽:924
雲伺服器不實名違法嗎 瀏覽:556
怎樣查看文件夾圖片是否重復 瀏覽:993
文件怎麼導成pdf文件 瀏覽:806
打開sql表的命令 瀏覽:101
安卓手機如何面部支付 瀏覽:37
天元數學app為什麼登錄不上去 瀏覽:822
明日之後為什麼有些伺服器是四個字 瀏覽:102
安卓系統l1是什麼意思 瀏覽:24
伺服器一直崩應該用什麼指令 瀏覽:923
cm202貼片機編程 瀏覽:729
php構造函數帶參數 瀏覽:179
解壓電波歌曲大全 瀏覽:345
為啥文件夾移到桌面成word了 瀏覽:860
命令符的安全模式是哪個鍵 瀏覽:760
編程中學 瀏覽:957