当前位置: 技术问答>java相关
关于在java中调用操作系统命令的奇怪问题,100分送上。
来源: 互联网 发布时间:2015-11-09
本文导语: 我在编写一个执行操作系统命令的时候遇到了以下问题:当我第一次运行 class PT { String strcmd=null; Runtime r; Process p; BufferedReader br=null; StringBuffer sb=new StringBuffer(); String inline; String retu; public Strin...
我在编写一个执行操作系统命令的时候遇到了以下问题:当我第一次运行
class PT
{
String strcmd=null;
Runtime r;
Process p;
BufferedReader br=null;
StringBuffer sb=new StringBuffer();
String inline;
String retu;
public String cmd(String str) throws IOException
{
//br=null;
//strcmd=null;
if(str!=null && str!="") strcmd="cmd /c "+str;//strcmd就是运行的命令
//通过strcmd是否为空进行判断是否要运行系统命令,但问题在于当strcmd不是空的时候,同样会调用strcmd==null 里面的语句,why?
if(strcmd!=null)
{
System.out.println(strcmd);
r=Runtime.getRuntime();
p=r.exec(strcmd);
br=new BufferedReader(new InputStreamReader(p.getInputStream()));
while(null!=(inline=br.readLine())){
sb.append(inline).append("n");
}
retu=sb.toString();//将命令结果返回
sb=null;
return(retu);
}
else
{
System.out.println(strcmd);
return("111");
}
}
}
第一次输入 cd 的显示结果
cmd /c cd
E:MYLEARNMY
第二次输入cd 的结果:多了一个 null值,但为什么会产生这个值,实在不明白
cmd /c cd
E:MYLEARNMY
null
111
第三次输入cd 的结果:多了两个 null值,但为什么会产生这个值,实在不明白
cmd /c cd
E:MYLEARNMY
null
111
null
111
第四次:多了三个 null值,
cmd /c cd
E:MYLEARNMY
null
111
null
111
null
111
class PT
{
String strcmd=null;
Runtime r;
Process p;
BufferedReader br=null;
StringBuffer sb=new StringBuffer();
String inline;
String retu;
public String cmd(String str) throws IOException
{
//br=null;
//strcmd=null;
if(str!=null && str!="") strcmd="cmd /c "+str;//strcmd就是运行的命令
//通过strcmd是否为空进行判断是否要运行系统命令,但问题在于当strcmd不是空的时候,同样会调用strcmd==null 里面的语句,why?
if(strcmd!=null)
{
System.out.println(strcmd);
r=Runtime.getRuntime();
p=r.exec(strcmd);
br=new BufferedReader(new InputStreamReader(p.getInputStream()));
while(null!=(inline=br.readLine())){
sb.append(inline).append("n");
}
retu=sb.toString();//将命令结果返回
sb=null;
return(retu);
}
else
{
System.out.println(strcmd);
return("111");
}
}
}
第一次输入 cd 的显示结果
cmd /c cd
E:MYLEARNMY
第二次输入cd 的结果:多了一个 null值,但为什么会产生这个值,实在不明白
cmd /c cd
E:MYLEARNMY
null
111
第三次输入cd 的结果:多了两个 null值,但为什么会产生这个值,实在不明白
cmd /c cd
E:MYLEARNMY
null
111
null
111
第四次:多了三个 null值,
cmd /c cd
E:MYLEARNMY
null
111
null
111
null
111
|
class cmdl implements KeyListener
{
int keycode;
public void keyPressed(KeyEvent e)
{
keycode=e.getKeyCode();
if(keycode==10)
{
cmdLine.addActionListener(new cmdb()); //一个回车就会加一个cmdb(),所以有那种错误。把cmdb()中的内容放到这就可以了。
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
{
int keycode;
public void keyPressed(KeyEvent e)
{
keycode=e.getKeyCode();
if(keycode==10)
{
cmdLine.addActionListener(new cmdb()); //一个回车就会加一个cmdb(),所以有那种错误。把cmdb()中的内容放到这就可以了。
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
|
这是文本框(3次后)的
E:
E:
E:
这是系统终端的
E:
Before Set E:
After Set E:
E:
E:
Before Set E:
E:
After Set E:
E:
E:
E:
E:
Before Set E:
E:
E:
After Set E:
E:
E:
不觉得有任何奇怪啊,不知你想说什么,这是我改成我得习惯后的代码:
//MenuDemo.java
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Terminal extends JFrame {
JMenuBar menu=new JMenuBar();
JMenu mnuFile=new JMenu("Îļþ");
JMenu mnuEdit=new JMenu("±à¼");
JMenu mnuSet =new JMenu("ÉèÖÃ");
JMenu mnuHelp=new JMenu("°ïÖú");
JMenuItem mnuFileNew =new JMenuItem("н¨");
JMenuItem mnuFileOpen =new JMenuItem("´ò¿ª");
JMenuItem mnuFileSave =new JMenuItem("±£´æ");
JMenuItem mnuFileSaveAs=new JMenuItem("Áí´æΪ");
JMenuItem mnuFilePrint =new JMenuItem("´òÓ¡");
JMenuItem mnuFileExit =new JMenuItem("Í˳ö");
JMenuItem mnuCopy =new JMenuItem("¸´ÖÆ");
JMenuItem mnuCut =new JMenuItem("¼ôÇÐ");
JMenuItem mnuPaste=new JMenuItem("Õ³Ìù");
JMenuItem mnuBackColor=new JMenuItem("±³¾°ÉèÖÃ");
JMenuItem mnuFontset =new JMenuItem("×ÖÌåÉèÖÃ");
JMenuItem mnusubHelp =new JMenuItem("°ïÖú");
JMenuItem mnuAbout =new JMenuItem("¹ØÓÚ");
JLabel cmdLabel=new JLabel("ÃüÁîÐÐ:");
JTextArea resArea=new JTextArea(15,20);
JTextField cmdLine1=new JTextField(5);
JTextField cmdLine=new JTextField(35);
JButton cmdButton=new JButton("go");
JPanel cmdPanel=new JPanel();
JScrollPane ps=new JScrollPane();
public Terminal() {
this("Systemcommand Terminal");
}
public Terminal(String str) {
super(str);
Container content=getContentPane();
content.setLayout(new BorderLayout());
//Ìí¼Ó´°Ìå×é¼þ
cmdPanel.add(cmdLabel);
cmdPanel.add(cmdLine1);
cmdPanel.add(cmdLine);
cmdPanel.add(cmdButton);
ps=new JScrollPane(resArea);
content.add(ps,BorderLayout.CENTER);
content.add(cmdPanel,BorderLayout.SOUTH);
//ÒÔÏÂÊÇÌí¼Ó²Ëµ¥ÃüÁî
this.setJMenuBar(menu);
mnuFile.add(mnuFileNew);
mnuFile.add(mnuFileOpen);
mnuFile.add(mnuFileSave);
mnuFile.add(mnuFileSaveAs);
mnuFile.addSeparator();
mnuFile.add(mnuFilePrint);
mnuFile.add(mnuFileExit);
mnuEdit.add(mnuCopy);
mnuEdit.add(mnuCut);
mnuEdit.add(mnuPaste);
mnuSet.add(mnuBackColor);
mnuSet.add(mnuFontset);
mnuHelp.add(mnusubHelp);
mnuHelp.add(mnuAbout);
menu.add(mnuFile);
menu.add(mnuEdit);
menu.add(mnuSet);
menu.add(mnuHelp);
//¿ªÊ¼Ê¼þÕìÌý
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
cmdButton.addActionListener(new CmdProccessor());
cmdLine.addKeyListener(new CmdLine());
}
class CmdProccessor implements ActionListener {
String strLine;
String strArea;
String strRes;
PT ptcmd=new PT();
public void actionPerformed(ActionEvent e) {
cmdLine.selectAll();
resArea.selectAll();
strLine=cmdLine.getSelectedText();
strArea=resArea.getSelectedText();
if(strLine==null) strLine="";
if(strArea==null) strArea="";
try {
strRes=ptcmd.cmd(strLine);
} catch(IOException IOE) {
System.out.println("error");
}
System.out.println("Before Set "+strRes);
resArea.setText(strRes);
System.out.println("After Set "+strRes);
cmdLine.setText("");
}
}
class CmdLine implements KeyListener {
public void keyPressed(KeyEvent e) {
int keycode=e.getKeyCode();
if(keycode==10) {
cmdLine.addActionListener(new CmdProccessor());
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
public static void main(String args[]) {
Terminal ter=new Terminal();
ter.pack();
ter.show();
}
}
//ÏÂÃæÊǵ÷ÓÃϵͳÃüÁîµÄº¯Êý
class PT {
String strCmd=null;
Process p;
BufferedReader br=null;
StringBuffer sb=new StringBuffer();
String inLine;
public String cmd(String str) throws IOException {
//br=null;
//strCmd=null;
if(str!=null && str!="") strCmd="cmd /c "+str;
if(strCmd!=null) {
// System.out.println(strCmd);
p=Runtime.getRuntime().exec(strCmd);
br=new BufferedReader(new InputStreamReader(p.getInputStream()));
while(null!=(inLine=br.readLine())) {
sb.append(inLine).append("n");
}
System.out.println(sb.toString());
return(sb.toString());
} else {
System.out.println(strCmd);
return("111");
}
}
}
E:
E:
E:
这是系统终端的
E:
Before Set E:
After Set E:
E:
E:
Before Set E:
E:
After Set E:
E:
E:
E:
E:
Before Set E:
E:
E:
After Set E:
E:
E:
不觉得有任何奇怪啊,不知你想说什么,这是我改成我得习惯后的代码:
//MenuDemo.java
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Terminal extends JFrame {
JMenuBar menu=new JMenuBar();
JMenu mnuFile=new JMenu("Îļþ");
JMenu mnuEdit=new JMenu("±à¼");
JMenu mnuSet =new JMenu("ÉèÖÃ");
JMenu mnuHelp=new JMenu("°ïÖú");
JMenuItem mnuFileNew =new JMenuItem("н¨");
JMenuItem mnuFileOpen =new JMenuItem("´ò¿ª");
JMenuItem mnuFileSave =new JMenuItem("±£´æ");
JMenuItem mnuFileSaveAs=new JMenuItem("Áí´æΪ");
JMenuItem mnuFilePrint =new JMenuItem("´òÓ¡");
JMenuItem mnuFileExit =new JMenuItem("Í˳ö");
JMenuItem mnuCopy =new JMenuItem("¸´ÖÆ");
JMenuItem mnuCut =new JMenuItem("¼ôÇÐ");
JMenuItem mnuPaste=new JMenuItem("Õ³Ìù");
JMenuItem mnuBackColor=new JMenuItem("±³¾°ÉèÖÃ");
JMenuItem mnuFontset =new JMenuItem("×ÖÌåÉèÖÃ");
JMenuItem mnusubHelp =new JMenuItem("°ïÖú");
JMenuItem mnuAbout =new JMenuItem("¹ØÓÚ");
JLabel cmdLabel=new JLabel("ÃüÁîÐÐ:");
JTextArea resArea=new JTextArea(15,20);
JTextField cmdLine1=new JTextField(5);
JTextField cmdLine=new JTextField(35);
JButton cmdButton=new JButton("go");
JPanel cmdPanel=new JPanel();
JScrollPane ps=new JScrollPane();
public Terminal() {
this("Systemcommand Terminal");
}
public Terminal(String str) {
super(str);
Container content=getContentPane();
content.setLayout(new BorderLayout());
//Ìí¼Ó´°Ìå×é¼þ
cmdPanel.add(cmdLabel);
cmdPanel.add(cmdLine1);
cmdPanel.add(cmdLine);
cmdPanel.add(cmdButton);
ps=new JScrollPane(resArea);
content.add(ps,BorderLayout.CENTER);
content.add(cmdPanel,BorderLayout.SOUTH);
//ÒÔÏÂÊÇÌí¼Ó²Ëµ¥ÃüÁî
this.setJMenuBar(menu);
mnuFile.add(mnuFileNew);
mnuFile.add(mnuFileOpen);
mnuFile.add(mnuFileSave);
mnuFile.add(mnuFileSaveAs);
mnuFile.addSeparator();
mnuFile.add(mnuFilePrint);
mnuFile.add(mnuFileExit);
mnuEdit.add(mnuCopy);
mnuEdit.add(mnuCut);
mnuEdit.add(mnuPaste);
mnuSet.add(mnuBackColor);
mnuSet.add(mnuFontset);
mnuHelp.add(mnusubHelp);
mnuHelp.add(mnuAbout);
menu.add(mnuFile);
menu.add(mnuEdit);
menu.add(mnuSet);
menu.add(mnuHelp);
//¿ªÊ¼Ê¼þÕìÌý
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
cmdButton.addActionListener(new CmdProccessor());
cmdLine.addKeyListener(new CmdLine());
}
class CmdProccessor implements ActionListener {
String strLine;
String strArea;
String strRes;
PT ptcmd=new PT();
public void actionPerformed(ActionEvent e) {
cmdLine.selectAll();
resArea.selectAll();
strLine=cmdLine.getSelectedText();
strArea=resArea.getSelectedText();
if(strLine==null) strLine="";
if(strArea==null) strArea="";
try {
strRes=ptcmd.cmd(strLine);
} catch(IOException IOE) {
System.out.println("error");
}
System.out.println("Before Set "+strRes);
resArea.setText(strRes);
System.out.println("After Set "+strRes);
cmdLine.setText("");
}
}
class CmdLine implements KeyListener {
public void keyPressed(KeyEvent e) {
int keycode=e.getKeyCode();
if(keycode==10) {
cmdLine.addActionListener(new CmdProccessor());
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
public static void main(String args[]) {
Terminal ter=new Terminal();
ter.pack();
ter.show();
}
}
//ÏÂÃæÊǵ÷ÓÃϵͳÃüÁîµÄº¯Êý
class PT {
String strCmd=null;
Process p;
BufferedReader br=null;
StringBuffer sb=new StringBuffer();
String inLine;
public String cmd(String str) throws IOException {
//br=null;
//strCmd=null;
if(str!=null && str!="") strCmd="cmd /c "+str;
if(strCmd!=null) {
// System.out.println(strCmd);
p=Runtime.getRuntime().exec(strCmd);
br=new BufferedReader(new InputStreamReader(p.getInputStream()));
while(null!=(inLine=br.readLine())) {
sb.append(inLine).append("n");
}
System.out.println(sb.toString());
return(sb.toString());
} else {
System.out.println(strCmd);
return("111");
}
}
}