Runtime.getRuntime().exec("cmd /c net send localhost aaa");
cmd 下应该可以返回 “消息已经送到 LOCALHOST。”这样的信息
在java里如何得到??
我觉得这蛮重要的
4 个解决方案
#1
java.lang.Process process = Runtime.getRuntime().exec("cmd /c net send localhost aaa");
java.io.OutputStream os = process.getOutputStream();
读取Process的输出流,就是输出信息。
java.io.OutputStream os = process.getOutputStream();
读取Process的输出流,就是输出信息。
#2
import java.io.*;
/**
* <p>Title: newman的学习资料</p>
* <p>Description: 没有最好只有更好!</p
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Shanghai University</p>
* @author Newman.Nch
* @version 1.0
*/
public class MsDosOutput {
public MsDosOutput() {
String s="";
}
/**
* 获取文本
*/
public void inputtest(){
try {
String cmd = "cmd /c dir f:\\";
String cmd2 = "net view";
//String cmd3="cmd /c F:\\my_bag\\JBuilder6.0\\DefaultProject\\classes\\defaultproject\\run.bat";
Process child = Runtime.getRuntime().exec(cmd);
InputStream child_in = child.getInputStream();
System.out.println (cmd);
BufferedReader in=new BufferedReader(new InputStreamReader(child_in));
String szstr1="";
int count=0;
while ((szstr1=in.readLine())!=null){
System.out.println(szstr1);
count++;
}
System.out.print("count: "+count);
child_in.close();
in.close() ;
child.destroy() ;
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
MsDosOutput ms=new MsDosOutput();
ms.inputtest();
}
}
/**
* <p>Title: newman的学习资料</p>
* <p>Description: 没有最好只有更好!</p
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Shanghai University</p>
* @author Newman.Nch
* @version 1.0
*/
public class MsDosOutput {
public MsDosOutput() {
String s="";
}
/**
* 获取文本
*/
public void inputtest(){
try {
String cmd = "cmd /c dir f:\\";
String cmd2 = "net view";
//String cmd3="cmd /c F:\\my_bag\\JBuilder6.0\\DefaultProject\\classes\\defaultproject\\run.bat";
Process child = Runtime.getRuntime().exec(cmd);
InputStream child_in = child.getInputStream();
System.out.println (cmd);
BufferedReader in=new BufferedReader(new InputStreamReader(child_in));
String szstr1="";
int count=0;
while ((szstr1=in.readLine())!=null){
System.out.println(szstr1);
count++;
}
System.out.print("count: "+count);
child_in.close();
in.close() ;
child.destroy() ;
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
MsDosOutput ms=new MsDosOutput();
ms.inputtest();
}
}
#3
Process process = Runtime.getRuntime().exec("net send localhost aaa");
不要加"cmd /c"
!
不要加"cmd /c"
!
#4
谢谢!
#1
java.lang.Process process = Runtime.getRuntime().exec("cmd /c net send localhost aaa");
java.io.OutputStream os = process.getOutputStream();
读取Process的输出流,就是输出信息。
java.io.OutputStream os = process.getOutputStream();
读取Process的输出流,就是输出信息。
#2
import java.io.*;
/**
* <p>Title: newman的学习资料</p>
* <p>Description: 没有最好只有更好!</p
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Shanghai University</p>
* @author Newman.Nch
* @version 1.0
*/
public class MsDosOutput {
public MsDosOutput() {
String s="";
}
/**
* 获取文本
*/
public void inputtest(){
try {
String cmd = "cmd /c dir f:\\";
String cmd2 = "net view";
//String cmd3="cmd /c F:\\my_bag\\JBuilder6.0\\DefaultProject\\classes\\defaultproject\\run.bat";
Process child = Runtime.getRuntime().exec(cmd);
InputStream child_in = child.getInputStream();
System.out.println (cmd);
BufferedReader in=new BufferedReader(new InputStreamReader(child_in));
String szstr1="";
int count=0;
while ((szstr1=in.readLine())!=null){
System.out.println(szstr1);
count++;
}
System.out.print("count: "+count);
child_in.close();
in.close() ;
child.destroy() ;
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
MsDosOutput ms=new MsDosOutput();
ms.inputtest();
}
}
/**
* <p>Title: newman的学习资料</p>
* <p>Description: 没有最好只有更好!</p
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Shanghai University</p>
* @author Newman.Nch
* @version 1.0
*/
public class MsDosOutput {
public MsDosOutput() {
String s="";
}
/**
* 获取文本
*/
public void inputtest(){
try {
String cmd = "cmd /c dir f:\\";
String cmd2 = "net view";
//String cmd3="cmd /c F:\\my_bag\\JBuilder6.0\\DefaultProject\\classes\\defaultproject\\run.bat";
Process child = Runtime.getRuntime().exec(cmd);
InputStream child_in = child.getInputStream();
System.out.println (cmd);
BufferedReader in=new BufferedReader(new InputStreamReader(child_in));
String szstr1="";
int count=0;
while ((szstr1=in.readLine())!=null){
System.out.println(szstr1);
count++;
}
System.out.print("count: "+count);
child_in.close();
in.close() ;
child.destroy() ;
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) {
MsDosOutput ms=new MsDosOutput();
ms.inputtest();
}
}
#3
Process process = Runtime.getRuntime().exec("net send localhost aaa");
不要加"cmd /c"
!
不要加"cmd /c"
!
#4
谢谢!