導航:首頁 > 程序命令 > java的windows命令

java的windows命令

發布時間:2022-12-16 08:29:47

java啟動windows命令

這是摘自JAVA NB 網 http://www.javanb.com 上的例子。

如何用java啟動windows命令行程序: http://www.javanb.com/java/1/17740.html

先請編譯和運行下面程序:

import java.util.*;
import java.io.*;

public class BadExecJavac2
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t){
t.printStackTrace();
}
}
}

我們知道javac命令,當不帶參數運行javac 程序時,它將輸出幫助說明,為什麼上面程序不產生任何輸出並掛起,永不完成呢?java文檔上說,由於有些本地平台為標准輸入和輸出流所提供的緩沖區大小有限,如果不能及時寫入子進程的輸入流或者讀取子進程的輸出流,可能導致子進程阻塞,甚至陷入死鎖。所以,上面的程序應改寫為:

import java.util.*;
import java.io.*;

public class MediocreExecJavac
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t){
t.printStackTrace();
}
}
}

下面是正確的輸出:

D:\java>java MediocreExecJavac

Usage: javac 〈OPTIONs>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath Specify where to find user class files
-cp Specify where to find user class files
-sourcepath Specify where to find input source files
-bootclasspath Override location of bootstrap class files
-extdirs Override location of installed extensions
-endorseddirs Override location of endorsed standards path
-d Specify where to place generated class files
-encoding Specify character encoding used by source files
-source Provide source compatibility with specified release

-target Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-X Print a synopsis of nonstandard options
-J Pass directly to the runtime system

Process exitValue: 2

D:\java>

下面是一個更一般的程序,它用兩個線程同步清空標准錯誤流和標准輸出流,並能根據你所使用的windows操作系統選擇windows命令解釋器command.com或cmd.exe,然後執行你提供的命令。

import java.util.*;
import java.io.*;

class StreamGobbler extends Thread
{
InputStream is;
String type; //輸出流的類型ERROR或OUTPUT

StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}

public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
{
System.out.println(type + ">" + line);
System.out.flush();
}
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}

public class GoodWindowsExec
{
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec ");
System.exit(1);
}

try
{
String osName = System.getProperty("os.name" );
System.out.println("osName: " + osName);
String[] cmd = new String[3];

if(osName.equals("Windows XP") ||osName.equals("Windows 2000"))
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
}
else if( osName.equals( "Windows 98" ) )
{
cmd[0] = "command.com" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
}

Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]+ " " + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");

// kick them off
errorGobbler.start();
outputGobbler.start();

// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);

} catch (Throwable t){
t.printStackTrace();
}
}
}
下面是一個測試結果:

D:\java>java GoodWindowsExec " Test.java Test1.java"
osName: Windows XP
Execing cmd.exe /C Test.java Test1.java
OUTPUT>已復制 1 個文件。
ExitValue: 0

D:\java>

下面的測試都能通過(windows xp+jdk1.5)

D:\java>java GoodWindowsExec dir

D:\java>java GoodWindowsExec Test.java

D:\java>java GoodWindowsExec regedit.exe

D:\java>java GoodWindowsExec NOTEPAD.EXE

D:\java>java GoodWindowsExec first.ppt

D:\java>java GoodWindowsExec second.doc

function TempSave(ElementID) { CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value); CommentsPersistDiv.save("CommentXMLStore"); } function Restore(ElementID) { CommentsPersistDiv.load("CommentXMLStore"); document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent"); }

❷ 如何在windows命令行窗口運行Java程序

你好,
先配置環境變數,確保Java安裝程序的\bin子目錄在path變數中;
用javac命令把源文件(即擴展名為java的文件)編譯成位元組碼文件;
用java命令運行對應的位元組碼文件。

❸ 如何在windows命令行窗口運行Java程序

在安裝了jdk的電腦上都是可以運行Java程序的,當運行測試一些小程序的時候就不必在IDE中了。下面用一個簡單的HelloWorld程序看一下運行過程。。
工具/原料
確定配置好了Java運行的環境。
方法/步驟
1
新建一個文本文件,就用windows的記事本吧。
2
打開記事本,輸入一段代碼。
3
保存的時候把後綴名修改為.java。
4
打開運行,在運行輸入欄中輸入cmd。打開命令提示窗口。
5
先轉到.java文件所在的位置。這里是在d盤的根目錄。
6
執行以下一句命令之後,可以看見在.java的文件夾裡面生成了一個為.class的類文件。
7
執行以下命令之後,就可以看到結果了。。
END
注意事項
確信要配置好Java運行環境。否則不能運行。

❹ windows下如何用java命令運行jar包

step1:用eclipse導出(也可以用jar命令)jar包,並指定Main-Class,比如Main-Class:com.skymobi.csj.CsjMain step2:編寫bat step2.1:添加classpath,要將所有依賴的jar和properties用絕對路徑加入,注意必須絕對路徑,然後還要加上自己要運行的jar包 set classpath=%classpath%;c:\test\skyopi.properties;c:\test\skyopi-1.0.6.jar;c:\test\slf4j-api-1.5.3.jar;c:\test\slf4j-log4j12-1.5.3.jar;c:\test\commons-logging-1.1.1.jar; c:\test\log4j-1.2.15.jar;c:\test\commons-httpclient-3.1.jar;c:\test\mole-framwork-1.5.jar;c:\test\mina-core-2.0.0-RC1.jar;c:\test\log4j.properties; c:\test\csj_skyserver.jar step2.2:添加執行腳本 java com.skymobi.csj.CsjMain MoleID=0xA000 MoleInstID=0xA001 step3: 將寫好的腳步和依賴的所有jar和配置放到c:/test下(如果這個地址改了,上面的classpath里要做相應的改變) step4.:用你的滑鼠雙擊bat,hava fun ^-^ 注意:java -classpath 。。。。-jarcsj_skyserver.jar arg0 證明無法調用依賴的其他包

❺ 怎麼使用java調用windows下dos命令

1. 使用Process類操作

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.SequenceInputStream;

public class Processing
{
public static void main ( String[] args ) throws InterruptedException
{
try
{
// open cmd
// Process process = Runtime.getRuntime ().exec (new String[] {
// "cmd", "/c", "dir" }, null, new File ("e:/"));
Process process = Runtime.getRuntime ().exec ("cmd");
SequenceInputStream sis = new SequenceInputStream (process.getInputStream (), process.getErrorStream ());
InputStreamReader isr = new InputStreamReader (sis, "GBK");
BufferedReader br = new BufferedReader (isr);
// next command
OutputStreamWriter osw = new OutputStreamWriter (process.getOutputStream ());
BufferedWriter bw = new BufferedWriter (osw);
bw.write ("cd C:/WINDOWS/audio/");
bw.newLine ();
bw.write ("c:");
bw.newLine ();
bw.write ("ffmpeg -i keyboard.wav keyboard.mp3");
bw.newLine ();
bw.flush ();
bw.close ();
osw.close ();
// read
String line = null;
while (null != ( line = br.readLine () ))
{
System.out.println (line);
}
// wait for termination
/*process.waitFor ();
process.exitValue ();*/
process.destroy ();
br.close ();
isr.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
// catch (InterruptedException e)
// {
// e.printStackTrace ();
// }
}
}

2. 使用ProcessBuilder類操作。

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class TestProcessBuilder
{
public static void main ( String[] args ) throws Exception
{
ProcessBuilder builder = new ProcessBuilder ("cmd", "/c", "ipconfig /all");
// builder.directory (new File ("C:\\WINDOWS\\audio"));
Process process = builder.start ();
InputStream is = process.getInputStream ();
InputStreamReader isr = new InputStreamReader (is, "GBK");
BufferedReader br = new BufferedReader (isr);
LinkedList<String> list = new LinkedList<String> ();
String line;
String regex = ".*Ethernet adapter(.*)\\:.*";
while (null != ( line = br.readLine () ))
{
if (!"".equals (line))
{
if (line.matches (regex))
{
list.add (line.replaceAll (regex, "$1"));
}
System.out.println (line);
}
}
br.close ();
isr.close ();
is.close ();
process.destroy ();
System.out.println (list);
}
}
3. 使用Runtime操作。

❻ 如何用windows 命令行編譯java

你首先得安裝jdk,安裝好jdk後,在命令窗口輸入java -version回車,如果下面顯示了java version "1.x.x.x"的版本信息就說明安裝成功了,至於jdk如何安裝,exe自動配置版的,也有手動配置版的,可以自行網路有很多教程。不懂的問題可以繼續追問

❼ java 怎麼調用windows外部命令

可以使用Java Process類,下面是一些例子:

Process類是一個抽象類,方法都是抽象的,它封裝了一個進程,也就是一個可執行的程序該類提供進程的輸入、執行輸出到進程、等待進程的完成和檢查進程的退出狀態及銷毀進程的方法
ProcessBuilder.start()和Runtime.exec方法創建一個本機進程並返回Process子類的一個實例,該實例可以控制進程並獲取相關的信息
其它的概要請參考JDK文檔
下面就開始舉幾個簡單的示例:
(1)執行簡單的DOS命令,如打開一個記事本

Java代碼
packagecom.iwtxokhtd.other;

importjava.io.IOException;

publicclassProcessTest{

publicstaticvoidmain(String[]args){
try{
Processproc=Runtime.getRuntime().exec("notepad");
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}
[java]viewplain
packagecom.iwtxokhtd.other;

importjava.io.IOException;

publicclassProcessTest{

publicstaticvoidmain(String[]args){
try{
Processproc=Runtime.getRuntime().exec("notepad");
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}

(2)使用它的其它構造方法執行相關的命令,如下例:

Java代碼
packagecom.iwtxokhtd.other;

importjava.io.IOException;

publicclassProcessTest{

publicstaticvoidmain(String[]args){
try{

StringexeFullPathName="C:/ProgramFiles/InternetExplorer/IEXPLORE.EXE";
Stringmessage="www.google.com";
String[]cmd={exeFullPathName,message};
Processproc=Runtime.getRuntime().exec(cmd);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}
[java]viewplain
packagecom.iwtxokhtd.other;

importjava.io.IOException;

publicclassProcessTest{

publicstaticvoidmain(String[]args){
try{

StringexeFullPathName="C:/ProgramFiles/InternetExplorer/IEXPLORE.EXE";
Stringmessage="www.google.com";
String[]cmd={exeFullPathName,message};
Processproc=Runtime.getRuntime().exec(cmd);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}

執行上述命令可以打開Google網站
(3)列出系統正在運行的所有進程信息

Java代碼
packagecom.iwtxokhtd.other;

importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;

publicclassListAllProcessTest{

//列出所有的進程信息
publicstaticvoidmain(String[]args){
BufferedReaderbr=null;
try{
Processproc=Runtime.getRuntime().exec("tasklist");
br=newBufferedReader(newInputStreamReader(proc.getInputStream()));
@SuppressWarnings("unused")
Stringline=null;
System.out.println("列印所有正在運行的進程信息");
while((line=br.readLine())!=null){
System.out.println(br.readLine());
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(br!=null){
try{
br.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}


}

}
[java]viewplain
packagecom.iwtxokhtd.other;

importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;

publicclassListAllProcessTest{

//列出所有的進程信息
publicstaticvoidmain(String[]args){
BufferedReaderbr=null;
try{
Processproc=Runtime.getRuntime().exec("tasklist");
br=newBufferedReader(newInputStreamReader(proc.getInputStream()));
@SuppressWarnings("unused")
Stringline=null;
System.out.println("列印所有正在運行的進程信息");
while((line=br.readLine())!=null){
System.out.println(br.readLine());
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(br!=null){
try{
br.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}


}

}

(4)判斷一個具體的進程是否正在運行,如下例:
Java代碼
packagecom.iwtxokhtd.other;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
publicclassFindProcessExeTest
{
publicstaticvoidmain(String[]args){

if(findProcess("QQ.exe")){
System.out.println("------判斷指定的進程是否在運行------");
System.out.println("QQ.exe該進程正在運行!");
}else{
System.out.println("------判斷指定的進程是否在運行------");
System.out.println("QQ.exe該進程沒有在運行!");
}

}
(StringprocessName){
BufferedReaderbr=null;
try{

//下面這句是列出含有processName的進程圖像名
Processproc=Runtime.getRuntime().exec("tasklist/FI/"IMAGENAMEeq"+processName+"/"");
br=newBufferedReader(newInputStreamReader(proc.getInputStream()));
Stringline=null;
while((line=br.readLine())!=null){
//判斷指定的進程是否在運行
if(line.contains(processName)){
returntrue;
}
}

returnfalse;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}finally{
if(br!=null){
try{
br.close();
}catch(Exceptionex){
}
}

}
}
}
[java]viewplain
packagecom.iwtxokhtd.other;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
publicclassFindProcessExeTest
{
publicstaticvoidmain(String[]args){

if(findProcess("QQ.exe")){
System.out.println("------判斷指定的進程是否在運行------");
System.out.println("QQ.exe該進程正在運行!");
}else{
System.out.println("------判斷指定的進程是否在運行------");
System.out.println("QQ.exe該進程沒有在運行!");
}

}
(StringprocessName){
BufferedReaderbr=null;
try{

//下面這句是列出含有processName的進程圖像名
Processproc=Runtime.getRuntime().exec("tasklist/FI/"IMAGENAMEeq"+processName+"/"");
br=newBufferedReader(newInputStreamReader(proc.getInputStream()));
Stringline=null;
while((line=br.readLine())!=null){
//判斷指定的進程是否在運行
if(line.contains(processName)){
returntrue;
}
}

returnfalse;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}finally{
if(br!=null){
try{
br.close();
}catch(Exceptionex){
}
}

}
}
}

其它的用法可以參考JDK文檔,這里就不一一舉例,畢竟它用得不多。

❽ 如何在windows命令行窗口運行Java程序

1、首先,運行Java程序,需要安裝好jdk
2、在windows命令行窗口輸入java和javac驗證環境變數path是否配置好
3、編寫好正確的java小程序
4、在windows命令行進入到程序所在路徑下,javac編譯,會在原有程序下新生成一個.class文件,java運行.class文件,就可以成功運行Java程序啦

❾ 編譯java程序的命令是什麼,運行java應用程序的命令是什麼

當前默認目錄為C盤Users文件夾下的Administrator文件夾。一般而言,我們習慣改變當前目錄。由於windows有磁碟分區,若要跳到其他磁碟,例如E盤,有幾種方法:

1、輸入命令: pushd 路徑(此命令可將當前目錄設為所希望的任一個已存在的路徑)

2、輸入命令: e: 轉移到e盤,然後再輸入 cd 轉移到所希望的已知路徑。

希望在windows命令行下使用javac、java、javap等命令,那麼當前電腦必須安裝了jdk,並且將jdk的bin目錄添加到環境變數path下了。

拓展資料:

Java是一種編程語言,被特意設計用於互聯網的分布式環境。Java具有類似於C++語言的「形式和感覺」,但它要比C++語言更易於使用,而且在編程時徹底採用了一種「以對象為導向」的方式。

使用Java編寫的應用程序,既可以在一台單獨的電腦上運行,也可以被分布在一個網路的伺服器端和客戶端運行。另外,Java還可以被用來編寫容量很小的應用程序模塊或者applet,做為網頁的一部分使用。applet可使網頁使用者和網頁之間進行互動式操作。

閱讀全文

與java的windows命令相關的資料

熱點內容
linux安裝deb包 瀏覽:521
電腦盤文件夾如何平鋪 瀏覽:267
相機卡滿了沒文件夾 瀏覽:751
如何批量快速壓縮視頻 瀏覽:432
我的世界如何加入ice伺服器 瀏覽:873
兄弟cnc編程說明書 瀏覽:204
php閃電入門教程學習 瀏覽:152
金岳霖邏輯pdf 瀏覽:938
linuxtomcat線程 瀏覽:77
pboc長度加數據加密 瀏覽:187
英雄聯盟國際服手游怎麼下安卓 瀏覽:297
程序員的思路 瀏覽:234
只能用命令獲得的四種方塊 瀏覽:358
怎麼用命令方塊防止開創造 瀏覽:807
掃描版的pdf 瀏覽:790
編程貓怎樣做3d游戲 瀏覽:207
怎麼查找雲伺服器上的ftp 瀏覽:156
我的世界伺服器如何注冊賬號 瀏覽:935
統計英文字元python 瀏覽:424
linux信息安全 瀏覽:910